使用JPA保持Map <Integer,Float>

Roa*_*alt 5 java hibernate jpa

在类中持久保存以下地图的最佳方法是什么:

  @Entity
  class MyClass {


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

我试过这个,但代码导致:

引起:org.hibernate.AnnotationException:使用@OneToMany或@ManyToMany定位未映射的类:mypackage.myClass.myMap [java.lang.Float]

axt*_*avt 9

您不能使用@ManyToManyIntegerFloat因为这些类型是值类型,而不是实体.使用@ElementCollection(自Hibernate 3.5起)或@CollectionOfElements(在以前的版本中).

@ElementCollection
Map<Integer,Float> myMap = new HashMap<Integer, Float>();  
Run Code Online (Sandbox Code Playgroud)

也可以看看:

  • @Roalt:是的,`@ ElementCollection`是JPA 2.0的一部分. (2认同)