posted 16 years ago
Yes, you can (at least in Hibernate used as a JPA provider). Follow this code:
The tables are:
ADDRESS: ID, STREET, LOCATION
PERSON: ID, NAME, STREET - where street is not mapped as a persistent property, but rather as a key for the OneToOne association. Remeber that for OneToOne the STREET in ADDRESS table must be unique, Hibernate throws exceptions as it tries to join with address, but there are two (or more) addressses for a OneToOne relation.
The code to persist is standard:
You can do the same for OneToMany, in which case the ADDRESS.STREET doesn't have to be unique.
Hope this helps.
[ November 29, 2008: Message edited by: Raf Szczypiorski ]