简体   繁体   English

如何在Hibernate中将地图持久化?

[英]How to persist a map inside a map in Hibernate?

So I have a class similar to this 所以我有一个与此类似的课程

public class MyClass{ 
...

@ElementCollection
private Map<Long,Map<Long,Double>> Vs = new HashMap<Long, Map<Long,Double>>(); 

...
}

This returns an error when I try to persist the class: 当我尝试保留该类时,这将返回错误:

org.hibernate.MappingException: Could not determine type for: java.util.Map, at table: MyClass_Vs, for columns: [org.hibernate.mapping.Column(Vs)]

I think the error is because there is a map inside the original map that has the annotation of @ElementCollection . 我认为错误是因为原始地图中有一个具有@ElementCollection注释的地图。 Any idea of how to solve this? 有什么解决办法的想法吗?

I really doubt Hibernate will be able to handle this type of mapping for you with just @ElementCollection . 我真的怀疑Hibernate是否仅使用@ElementCollection就能为您处理这种类型的映射。 You probably will need to define a new composite key, containing both of those Long map keys as @Id fields, then use an element collection on a Map<CompositeLong, Double> . 您可能需要定义一个新的组合键,将这两个Long映射键都包含为@Id字段,然后在Map<CompositeLong, Double>上使用元素集合。 Granted, I'm making some assumptions about what those Long values represent, but it's hard to tell without more context. 当然,我对这些Long值代表什么进行了一些假设,但是如果没有更多上下文,很难说得出。

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

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