简体   繁体   English

如何映射Map <String,Double>

[英]How to map a Map<String,Double>

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? 任何的想法?

Well, the error message is pretty clear: Double isn't an entity. 好吧,错误信息非常清楚: Double不是一个实体。 If you want to map a collection of basic elements, use the CollectionOfElement annotation (from Hibernate) or the ElementCollection annotation (from JPA 2.0). 如果要映射基本元素的集合,请使用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 Map is defined using the JoinTable Map的集合表的名称是使用JoinTable定义的
    • The name of the column for the key to the parent is set using a JoinColumn in the JoinTable 使用JoinTableJoinColumn设置父键的列的名称
  • The name of the column for the key of the map is defined in the MapKey 地图键的列名称在MapKey定义
  • The name of the column for the value of the map is defined using the Column 使用Column定义映射值的Column

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM