java 如何映射 Map<String,Double>
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3886797/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to map a Map<String,Double>
提问by Nhaolio
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.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?
任何的想法?
回答by Pascal Thivent
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).
好吧,错误消息很清楚:Double
不是实体。如果要映射基本元素的集合,请使用CollectionOfElement
注释(来自 Hibernate)或ElementCollection
注释(来自 JPA 2.0)。
So, assuming you're using Hibernate Annotations 3.4, try this:
因此,假设您使用的是 Hibernate Annotations 3.4,请尝试以下操作:
@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:
如果您使用的是 Hibernate Annotations 3.5+,则更喜欢 JPA 2.0 注释:
@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
- JPA 2.0 specification
- Section 11.1.12 "ElementCollection Annotation"
- Section 11.1.28 "MapKeyClass Annotation"
- Hibernate 注释 3.4 参考指南
- JPA 2.0 规范
- 第 11.1.12 节“ElementCollection 注释”
- 第 11.1.28 节“MapKeyClass 注释”
Do you know how to customize the "ELEMENT" and "MAPKEY" column names ?
您知道如何自定义“ELEMENT”和“MAPKEY”列名称吗?
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 theJoinTable
- The name of the column for the key to the parent is set using a
JoinColumn
in theJoinTable
- The name of the column for the key to the parent is set using a
- 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
- 的集合表的名称
Map
是使用JoinTable
- 父项键的列名是使用 a 设置
JoinColumn
的JoinTable
- 父项键的列名是使用 a 设置
- 映射键的列名定义在
MapKey
- 映射值的列的名称是使用
Column