Thursday, November 26, 2009

Hibernate annotation for a Map Enum type

Hibernate Annotations are so great, especially together with the Hibernate Tools that allows to generate the whole database (sql table definitions) from your annotated beans.
Right now I mapped the first time a java.util.Map with some enum type as key and a primitive type as value (java.lang.String).
The mapping looks like the following:

public class Test { 
    @JoinTable(name = "my_table") 
    @Cascade(value = org.hibernate.annotations.CascadeType.DELETE_ORPHAN) 
    @Enumerated(value = EnumType.STRING) 
    @org.hibernate.annotations.MapKey(columns = {@Column(name = "my_enum")}) 
    @Column(name = "my_value", length = 4000) 
    public Map getMappedValues() { 
        return _mappedValues; 

What I was not able to do was to define the type of the value (String) as hibernate type text. This gave the correct table/column definition at build time (hibernate tools), but a ClassCastException at runtime, saying that MyEnumType is not compatible to hibernate’s type text. So I used the length of the @Column annotation as a temporary workaround - I’ll dig into this now or later

No comments:

Post a Comment