如何映射 Map

发布于 2024-09-26 06:23:06 字数 958 浏览 5 评论 0原文

我尝试过,

@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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一杯敬自由 2024-10-03 06:23:06

嗯,错误消息非常清楚:Double 不是一个实体。如果要映射基本元素的集合,请使用 CollectionOfElement 注释(来自 Hibernate)或 ElementCollection 注释(来自 JPA 2.0)。

因此,假设您使用 Hibernate Annotations 3.4,请尝试以下操作:

@CollectionOfElements(targetElement = Double.class)
@org.hibernate.annotations.MapKey(targetElement = String.class)
Map data;

或者,当使用泛型时:

@CollectionOfElements
Map<String, Double> data;

如果您使用 Hibernate Annotations 3.5+,则更喜欢 JPA 2.0 注释:

@ElementCollection(targetClass = Double.class)
@MapKeyClass(String.class)
Map data;

或者,当使用泛型时:

@ElementCollection
Map<String, Double> data;

参考资料


您知道如何自定义“ELEMENT”和“MAPKEY”列名称吗?

您可以完全自定义结果。我认为下面的示例演示了一切:

@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;
  • 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 the CollectionOfElement annotation (from Hibernate) or the ElementCollection annotation (from JPA 2.0).

So, assuming you're using Hibernate Annotations 3.4, try this:

@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:

@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 ?

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
    • The name of the column for the key to the parent is set using a JoinColumn in 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文