I've here a database with a PERSON
- ADDRESS
- ADDRESS_TYPE
relationship maintained by a triple join table PERSON_ADDRESS
. The PERSON
-ADDRESS
relationship is effectively one-to-many.
PERSON
ID FIRSTNAME LASTNAME -- --------- -------- 1 John Doe 2 Jane Doe
ADDRESS
ID STREET CITY -- -------------------- ------------- 1 Home Street 1 Hometown 2 Office Street 1 Officetown 3 Main Street 1 Maintown 4 Business Building 1 Businesstown
ADDRESS_TYPE
ID NAME -- --------------- 1 Home Address 2 Office Address
PERSON_ADDRESS
PERSON_ID ADDRESS_TYPE_ID ADDRESS_ID --------- --------------- ---------- 1 1 1 1 2 2 2 1 3 2 2 4
For practical reasons, I'd like to have my Person
entity to end up like:
public class Person {
private Address homeAddress; // Insertable/updateable by ADDRESS_TYPE_ID=1
private Address officeAddress; // Insertable/updateable by ADDRESS_TYPE_ID=2
}
Is this ever possible with JPA 2.0 annotations?
I've read the Map Key Columns chapter of the JPA wikibook and it seems that I have to use a @MapKeyJoinColumn
, but it's not entirely clear to me how to successfully use it in this situation. I expected to see a @JoinColumn
example along it, but it is absent in the code snippets in the wikibook.
If that's not possible with @MapKeyJoinColumn
, then an alternative approach with help of maybe @MapKeyClass
on a Map<AddressType, Address>
is also welcome, as long as I can end up with a getHomeAddress()
and getOfficeAddress()
in the Person
entity.