gpt4 book ai didi

mysql - 映射一个@Lob 值的 Map

转载 作者:可可西里 更新时间:2023-11-01 06:45:20 26 4
gpt4 key购买 nike

这是我模型的一部分:

@Entity
public class Entry
{
@Id @GeneratedValue
private long identifier;

@ElementCollection
@Column(nullable = false)
private Map<String, String> titles;

@ElementCollection
@Column(nullable = false)
@Lob
private Map<String, String> contents;

// Getters and setters, other fields and methods
}

我使用@Lob 注释是因为 map “内容”的值可能很大。请注意,我不关心映射“内容”的键如何映射到数据库。我只是找不到一种方法来指定 @Lob 注释应该只应用于 map 的值。

虽然 Entry.titles 毫无问题地映射到数据库,但 Entry.contents 却没有。没有创建数据库表并且 MySQL/Hibernate 提示:

Unsuccessful: create table myblog.Entry_contents (Entry_identifier bigint not null, contents longtext not null, contents_KEY longtext, primary key (Entry_identifier, contents_KEY)) type=InnoDB
BLOB/TEXT column 'contents_KEY' used in key specification without a key length

欢迎任何想法!

最佳答案

这绝对是 Hibernate 中的一个错误。 JPA 2.0 规范明确指出,在这种情况下,应将 @Lob 应用于映射值:

The Lob annotation may be used in conjunction with the Basic annotation or with the ElementCollection[100] annotation when the element collection value is of basic type.
...

[100] If the element collection is a Map, this applies to the map value.

明显的解决方法包括使用 @MapKeyColumn(columnDefinition = "...") 定义列类型或使用 @Embeddable 作为值的包装器。

此外,此错误似乎未报告,请随时报告:Hibernate JIRA .

关于mysql - 映射一个@Lob 值的 Map,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4775752/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com