Primary Keys through OneToOne and ManyToOne Relationships
and this
Derived Identifiers
and this
Derived Identifiers (other source)
and this
Derived Identifiers
and this
Derived Identifiers (other source)
Primary Keys through OneToOne and ManyToOne Relationships
A common model is to have a dependent object share the primary key of its parent. In the case of aOneToOne the child's primary key is the same as the parent, and in the case of a ManyToOne the child's primary key is composed of the parent's primary key and another locally unique field.JPA 1.0 does not allow
@Id on a OneToOne or ManyToOne, but JPA 2.0 does.One of the biggest pitfalls when working with composite primary keys comes with the implementation of associations to entity classes whose tables have multi-column primary keys (using the @JoinColumns annotation). Many JPA implementations may throw seemingly inconsistent exceptions when not specifying referencedColumnName for every @JoinColumn annotation, which JPA requires (even if all referenced column names are equal to the ones in the referencing table). See http://download.oracle.com/javaee/5/api/javax/persistence/JoinColumns.html
[edit] JPA 1.0
Unfortunately JPA 1.0 does not handle this model well, and things become complicated, so to make your life a little easier you may consider defining a generated unique id for the child. JPA 1.0 requires that all@Id mappings be Basic mappings, so if your Id comes from a foreign key column through a OneToOne or ManyToOne mapping, you must also define a Basic @Id mapping for the foreign key column. The reason for this is in part that the Id must be a simple object for identity and caching purposes, and for use in the IdClass or the EntityManager find() API.Because you now have two mappings for the same foreign key column you must define which one will be written to the database (it must be the
Basic one), so the OneToOne or ManyToOne foreign key must be defined to be read-only. This is done through setting the JoinColumn attributes insertable and updatable to false, or by using the @PrimaryKeyJoinColumn instead of the @JoinColumn.A side effect of having two mappings for the same column is that you now have to keep the two in synch. This is typically done through having the set method for the
OneToOne attribute also set the Basic attribute value to the target object's id. This can become very complicated if the target object's primary key is a GeneratedValue, in this case you must ensure that the target object's id has been assigned before relating the two objects.Some times I think that JPA primary keys would be much simpler if they were just defined on the entity using a collection of
Columns
instead of mixing them up with the attribute mapping. This would leave
you free to map the primary key field in any manner you desired. A
generic List could be used to pass the primary key to find()
methods, and it would be the JPA provider's responsibility for hashing
and comparing the primary key correctly instead of the user's IdClass. But perhaps for simple singleton primary key models the JPA model is more straight forward.- TopLink / EclipseLink : Allow the primary key to be specified as a list of columns instead of using
Idmappings. This allowsOneToOneandManyToOnemapping foreign keys to be used as the primary key without requiring a duplicate mapping. It also allows the primary key to be defined through any other mapping type. This is set through using aDescriptorCustomizerand theClassDescriptoraddPrimaryKeyFieldNameAPI.
- Hibernate / Open JPA / EclipseLink (as of 1.2): Allows the
@Idannotation to be used on aOneToOneorManyToOnemapping.
[edit] Example OneToOne id annotation
... @Entity public class Address { @Id @Column(name="OWNER_ID") private long ownerId; @OneToOne @PrimaryKeyJoinColumn(name="OWNER_ID", referencedColumnName="EMP_ID") private Employee owner; ... public void setOwner(Employee owner) { this.owner = owner; this.ownerId = owner.getId(); } ... }
[edit] Example OneToOne id XML
<entity name="Address" class="org.acme.Address" access="FIELD"> <attributes> <id name="ownerId"> <column name="OWNER_ID"/> </id> <one-to-one name="owner"> <primary-key-join-column name="OWNER_ID" referencedColumnName="EMP_ID"/> </one-to-one> </attributes> <entity/>
[edit] Example ManyToOne id annotation
... @Entity @IdClass(PhonePK.class) public class Phone { @Id @Column(name="OWNER_ID") private long ownerId; @Id private String type; @ManyToOne @PrimaryKeyJoinColumn(name="OWNER_ID", referencedColumnName="EMP_ID") private Employee owner; ... public void setOwner(Employee owner) { this.owner = owner; this.ownerId = owner.getId(); } ... }
[edit] Example ManyToOne id XML
<entity name="Address" class="org.acme.Address" access="FIELD"> <id-class class="org.acme.PhonePK"/> <attributes> <id name="ownerId"> <column name="OWNER_ID"/> </id> <id name="type"/> <many-to-one name="owner"> <primary-key-join-column name="OWNER_ID" referencedColumnName="EMP_ID"/> </many-to-one> </attributes> <entity/>
[edit] JPA 2.0
Defining anId for a OneToOne or ManyToOne in JPA 2.0 is much simpler. The @Id annotation or id XML attribute can be added to a OneToOne or ManyToOne mapping. The Id used for the object will be derived from the target object's Id. If the Id is a single value, then the source object's Id is the same as the target object's Id. If it is a composite Id, then the IdClass will contain the Basic Id attributes, and the target object's Id as the relationship value. If the target object also has a composite Id, then the source object's IdClass will contain the target object's IdClass.[edit] Example JPA 2.0 ManyToOne id annotation
... @Entity @IdClass(PhonePK.class) public class Phone { @Id private String type; @ManyToOne @Id @JoinColumn(name="OWNER_ID", referencedColumnName="EMP_ID") private Employee owner; ... }
[edit] Example JPA 2.0 ManyToOne id XML
<entity name="Address" class="org.acme.Address" access="FIELD"> <id-class class="org.acme.PhonePK"/> <attributes> <id name="type"/> <many-to-one name="owner" id="true"> <join-column name="OWNER_ID" referencedColumnName="EMP_ID"/> </many-to-one> </attributes> <entity/>
[edit] Example JPA 2.0 id class
... public class PhonePK { private String type; private long owner; public PhonePK() {} public PhonePK(String type, long owner) { this.type = type; this.owner = owner; } public boolean equals(Object object) { if (object instanceof PhonePK) { PhonePK pk = (PhonePK)object; return type.equals(pk.type) && owner == pk.owner; } else { return false; } } public int hashCode() { return type.hashCode() + owner; } }