9

I tried

@ManyToMany(cascade = CascadeType.ALL)
Map<String, Double> data = new HashMap<String, Double>();

but it produces the error :

   org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: com.company.Klass.data[java.lang.Double]
at org.hibernate.cfg.annotations.CollectionBinder.bindManyToManySecondPass(CollectionBinder.java:1016)
at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:567)
at org.hibernate.cfg.annotations.MapBinder$1.secondPass(MapBinder.java:80)
at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:43)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1130)
at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:296)
at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1115)

any idea?

Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
Nhaolio
  • 145
  • 1
  • 2
  • 6

1 Answers1

23

Well, the error message is pretty clear: Double isn't an entity. If you want to map a collection of basic elements, use the CollectionOfElement annotation (from Hibernate) or the 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 the JPA 2.0 annotations:

@ElementCollection(targetClass = Double.class)
@MapKeyClass(String.class)
Map data;

Or, when using generics:

@ElementCollection
Map<String, Double> data;

References


Do you know how to customize the "ELEMENT" and "MAPKEY" column names ?

You can fully customize the result. I think the 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 the collection table for the Map is defined using the JoinTable
    • The name of the column for the key to the parent is set using a JoinColumn in the JoinTable
  • The name of the column for the key of the map is defined in the MapKey
  • The name of the column for the value of the map is defined using the Column
Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
  • Thank you so much ! Do you know how to customize the "ELEMENT" and "MAPKEY" column names ? – Nhaolio Oct 08 '10 at 01:51
  • When using @ElementCollection Map data; I get this error in hbm2ddl: org.hibernate.MappingException: Could not determine type for: java.util.Map, at table: MYTABLE, for columns: [org.hibernate.mapping.Column(MYFIELD)] – Wouter Lievens Nov 09 '10 at 08:07
  • @Wouter Open a question, mention the version of Hibernate, etc (but the above answer is correct, as per the JPA 2.0 specification). – Pascal Thivent Nov 09 '10 at 08:18
  • Problem still exists, so I've followed your suggestion: http://stackoverflow.com/questions/6096525/mapping-a-mapstring-double-with-hibernate-and-jpa – Wouter Lievens May 23 '11 at 11:27
  • 1
    @org.hibernate.annotations.MapKey(targetElement = String.class, columns = @Column(name = "SOME_KEY")) is deprecated: @MapKeyColumn(name="SOME_KEY") should work in its stead – Nathan Feger Jan 24 '12 at 22:45