如何映射 Map
我尝试过,
@ManyToMany(cascade = CascadeType.ALL)
Map<String, Double> data = new HashMap<String, Double>();
但它产生了错误:
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)
知道吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
嗯,错误消息非常清楚:
Double
不是一个实体。如果要映射基本元素的集合,请使用CollectionOfElement
注释(来自 Hibernate)或ElementCollection
注释(来自 JPA 2.0)。因此,假设您使用 Hibernate Annotations 3.4,请尝试以下操作:
或者,当使用泛型时:
如果您使用 Hibernate Annotations 3.5+,则更喜欢 JPA 2.0 注释:
或者,当使用泛型时:
参考资料
您可以完全自定义结果。我认为下面的示例演示了一切:
Map
的集合表的名称是使用JoinTable
定义的JoinTable
中的JoinColumn
设置的MapKey
中,Column
定义映射的值的列名称Well, the error message is pretty clear:
Double
isn't an entity. If you want to map a collection of basic elements, use theCollectionOfElement
annotation (from Hibernate) or theElementCollection
annotation (from JPA 2.0).So, assuming you're using Hibernate Annotations 3.4, try this:
Or, when using generics:
And if you're using Hibernate Annotations 3.5+, prefer the JPA 2.0 annotations:
Or, when using generics:
References
You can fully customize the result. I think the sample below demonstrates everything:
Map
is defined using theJoinTable
JoinColumn
in theJoinTable
MapKey
Column