javakaffee

Just another weblog about (web) development technologies like spring, tapestry, jersey, jsf, hibernate and more

November 28, 2006

Hibernate annotation for a Map of some enum type to some primitive type

Filed under: development, java, hibernate — martin.grotzke @ 5:19 pm

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:


    @CollectionOfElements
    @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<MyEnumType, String> 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 - or has anybody a clue how to do this?

1 Comment »

  1. […] 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 …Read more: here […]

    Pingback by Pierres Service » Blog Archive » Hibernate annotation for a Map of some enum type to some primitive … — November 28, 2006 @ 11:43 pm

RSS feed for comments on this post. TrackBack URI

Leave a comment

*
To prove you're a person (not a spam script), type the security word shown in the picture.
Anti-Spam Image

Powered by WordPress