简体   繁体   中英

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 . 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 . 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> . Granted, I'm making some assumptions about what those Long values represent, but it's hard to tell without more context.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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