Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Persist a Map<Integer,Float> with JPA

What's the best way to persist the following map in a class:

  @Entity
  class MyClass {


      @ManyToMany(cascade = CascadeType.ALL)    
      Map<Integer,Float> myMap = new HashMap<Integer, Float>(); 
  } 

I've tried this, but the code results in:

Caused by: org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: mypackage.myClass.myMap[java.lang.Float]

like image 771
Roalt Avatar asked Jan 31 '11 10:01

Roalt


1 Answers

You cannot use @ManyToMany with Integer and Float because these types are value types, not entities. Use @ElementCollection (since Hibernate 3.5) or @CollectionOfElements (in previous versions).

@ElementCollection
Map<Integer,Float> myMap = new HashMap<Integer, Float>();  

See also:

  • 7.2.3. Collections of basic types and embeddable objects
like image 112
axtavt Avatar answered Oct 09 '22 02:10

axtavt