java - How to map a Map<String,Double> -


i tried

@manytomany(cascade = cascadetype.all) map<string, double> data = new hashmap<string, double>(); 

but produces error :

   org.hibernate.annotationexception: use of @onetomany or @manytomany targeting unmapped class: com.company.klass.data[java.lang.double] @ org.hibernate.cfg.annotations.collectionbinder.bindmanytomanysecondpass(collectionbinder.java:1016) @ org.hibernate.cfg.annotations.collectionbinder.bindstartomanysecondpass(collectionbinder.java:567) @ org.hibernate.cfg.annotations.mapbinder$1.secondpass(mapbinder.java:80) @ org.hibernate.cfg.collectionsecondpass.dosecondpass(collectionsecondpass.java:43) @ org.hibernate.cfg.configuration.secondpasscompile(configuration.java:1130) @ org.hibernate.cfg.annotationconfiguration.secondpasscompile(annotationconfiguration.java:296) @ org.hibernate.cfg.configuration.buildmappings(configuration.java:1115) 

any idea?

well, error message pretty clear: double isn't entity. if want map collection of basic elements, use collectionofelement annotation (from hibernate) or elementcollection annotation (from jpa 2.0).

so, assuming you're using hibernate annotations 3.4, try this:

@collectionofelements(targetelement = double.class) @org.hibernate.annotations.mapkey(targetelement = string.class) map data; 

or, when using generics:

@collectionofelements map<string, double> data; 

and if you're using hibernate annotations 3.5+, prefer jpa 2.0 annotations:

@elementcollection(targetclass = double.class) @mapkeyclass(string.class) map data; 

or, when using generics:

@elementcollection map<string, double> data; 

references


do know how customize "element" , "mapkey" column names ?

you can customize result. think sample below demonstrates everything:

@collectionofelements(targetelement = double.class) @jointable(name = "collection_table",      joincolumns = @joincolumn(name = "parent_id")) @org.hibernate.annotations.mapkey(targetelement = string.class,      columns = @column(name = "some_key")) @column(name = "some_value") private map data; 
  • the name of collection table map defined using jointable
    • the name of column key parent set using joincolumn in jointable
  • the name of column key of map defined in mapkey
  • the name of column value of map defined using column

Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -