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?
Alternative approaches with help of @MapKeyJoinColumn
or maybe @MapKeyClass
on a Map<AddressType, Address>
are also welcome, as long as I can end up with a getHomeAddress()
and getOfficeAddress()
in the Person
entity. I've looked around in this wikibook, but it's not entirely clear to me how to successfully use @MapKeyJoinColumn
in this situation with a triple join table.