How to persist a map inside a map in Hibernate?

96 Views Asked by At

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?

1

There are 1 best solutions below

0
On

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.