I tried
@ManyToMany(cascade = CascadeType.ALL)
Map data = new HashMap();
but it produces the error
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 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 data;
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;
Map
is defined using the JoinTable
JoinColumn
in the JoinTable
MapKey
Column