Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to map a Map<String,Double>

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?

like image 470
Nhaolio Avatar asked Oct 08 '10 00:10

Nhaolio


People also ask

What is string string map?

a Map<type, type> is a collection that associates one of the first type with one of the second type. So a Map<String, String> associates a String with another String. This is an instance of generics.

How to append values in HashMap in Java?

To add elements to HashMap, use the put() method.

How to update a key value in HashMap?

hashmap. put(key, hashmap. get(key) + 1); The method put will replace the value of an existing key and will create it if doesn't exist.


1 Answers

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

  • Hibernate Annotations 3.4 Reference Guide
    • 2.4.6.2.2. Map
    • 2.4.6.2.5. Collection of element or composite elements
  • JPA 2.0 specification
    • Section 11.1.12 "ElementCollection Annotation"
    • Section 11.1.28 "MapKeyClass Annotation"

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
like image 105
Pascal Thivent Avatar answered Sep 21 '22 17:09

Pascal Thivent