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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 03:51:06  来源:igfitidea点击:

How to map a Map<String,Double>

javahibernateormjpahibernate-annotations

提问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: Doubleisn't an entity. If you want to map a collection of basic elements, use the CollectionOfElementannotation (from Hibernate) or the ElementCollectionannotation (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

参考



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 Mapis defined using the JoinTable
    • The name of the column for the key to the parent is set using a JoinColumnin 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
  • 的集合表的名称Map是使用JoinTable
    • 父项键的列名是使用 a 设置JoinColumnJoinTable
  • 映射键的列名定义在 MapKey
  • 映射值的列的名称是使用 Column