Display a map with a positive @Lob value

This is part of my model:

@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 } 

I use the @Lob annotation because the value of the "contents" of the map can be large. Note that I don’t care how the “content” key of the map maps to the database. I simply could not find a way to indicate that the @Lob annotation should only apply to the value of the map.

While Entry.titles maps to the database without problems, Entry.contents does not. There is no database table, and MySQL / Hibernate complains that:

 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 

Any ideas appreciated!

+4
source share
2 answers

This is definitely a bug in Hibernate. The JPA 2.0 specification clearly states that in this case @Lob should be applied to the map value:

The Lob annotation can be used in combination with the Basic annotation or with the ElementCollection [100] annotation when the value of the element set is of the base type.
...

[100] If the collection of elements is a map, this refers to the value of the map.

Obvious workarounds include defining a column type using @MapKeyColumn(columnDefinition = "...") or using @Embeddable as a wrapper for values.

Also this error does not appear to be reported; feel free to report it: Hibernate JIRA .

+5
source

This bug has been fixed in Hibernate 4.2.6

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

Workaround for previous versions:

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

Source: https://habr.com/ru/post/1336702/


All Articles