I have two objects: Entity and Secondary. The Entity contains a Map of Secondary to String. It also has a composite key.
I had the mapping working with JDO, but now it is(past) time to move to JPA.
I can't figure out how to write the annotations to make it work,
There are three tables in the DB containing data.
desc map_table
Name Null Type
---------- -------- ------------
ENTITYID NOT NULL VARCHAR2(12)
ENTITYSOURCE NOT NULL NUMBER(1)
SECONDARYID NOT NULL VARCHAR2(20)
VALUE NOT NULL VARCHAR2(16)
desc entity
Name Null Type
---------- -------- ------------
ID NOT NULL VARCHAR2(12)
SOURCE NOT NULL NUMBER(1)
.....
desc secondary
Name Null Type
---------- -------- ------------
ID NOT NULL VARCHAR2(12)
name NUMBER(1)
I am running Hibernate 4.2.19
The main class:
@Entity
@Table(name="ENTITY")
public class Entity
{
@EmbeddedId
private EntityKey key;
@SomehtingIHavntFiguredOut
private Map<Secondary, String> domainConfig = new HashMap<Secondary, String>();
}
Here is the JDO mapping that I am leaving behind:
<field name="map" table="MAP_TABLE" default-fetch-group="true">
<map key-type="com.foo.Secondary" value-type="String" />
<join>
<column name="ENTIYID" target="ID"/>
<column name="ENTITYSOURCE" target="SOURCE"/>
</join>
<key>
<column name="SECONDARYID"/>
</key>
<value>
<column name="VALUE"/>
</value>
My solution
@ElementCollection
@CollectionTable(name="MAP_TABLE", joinColumns={
@JoinColumn(name="entityid",referencedColumnName="id"),
@JoinColumn(name="entitysource",referencedColumnName="source")
})
@MapKeyJoinColumn(name="secondaryid")
@Column(name = "VALUE")
private Map<Secondary, String> domainConfig = new HashMap<Secondary, String>();
I was unclear on when to use @ElementCollection, and when to use @OneToMany. I think because the values are primitives The correct annotation is @ElementCollection.