映射@Lob值映射

时间:2011-01-23 18:57:41

标签: mysql hibernate jpa annotations mysql-error-1170

这是我模特的一部分:

@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“contents”的值可能很大。请注意,我不关心映射“contents”的键如何映射到数据库。我只是找不到一种方法来指定@Lob注释只应用于地图的值。

虽然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

感谢任何想法!

2 个答案:

答案 0 :(得分:5)

这绝对是Hibernate中的一个错误。 JPA 2.0规范明确规定在这种情况下@Lob应该应用于地图值:

  

Lob注释可以与Basic注释一起使用   当元素集合值为basic时,或者使用ElementCollection [100]注释   类型。
  ...

     

[100]如果元素集合是Map,则这适用于地图值。

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

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

答案 1 :(得分:0)

此错误已在Hibernate 4.2.6中修复

https://hibernate.atlassian.net/browse/HHH-8472

以前版本的解决方法:

@MapKeyType(@Type(type = "java.lang.String"))