Mapping a @Lob valued Map
Asked Answered
A

2

7

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 map "contents" can be large. Note that I do not care about how the key of map "contents" is mapped to the database. I just couldn't find a way to specify that the @Lob annotation should be applied only to the value of the map.

While Entry.titles is mapped to the database without problems, Entry.contents is not. No database table is created 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 are appreciated!

Atalya answered 23/1, 2011 at 18:57 Comment(0)
M
6

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

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.

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

Also this bug seems to be unreported, feel free to report it: Hibernate JIRA.

Mohsen answered 23/1, 2011 at 20:6 Comment(0)
R
1

This bug was fixed in Hibernate 4.2.6

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

Workaround for previous versions:

@MapKeyType(@Type(type = "java.lang.String"))
Rhesus answered 4/11, 2013 at 13:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.