Jump to: navigation, search

Difference between revisions of "EclipseLink/Examples/JPA/2.0/DerivedIdentifiers"

(How to use derived identifiers to map composite Ids through OneToOne relationships)
Line 1: Line 1:
=How to use derived identifiers to map composite Ids through OneToOne relationships=
+
=How to use derived identifiers to map composite Ids through ManyToOne relationships=
 
Defining an <code>Id</code> for a <code>OneToOne</code> or <code>ManyToOne</code> in JPA 2.0 is much simpler.  The <code>@Id</code> annotation or <code>id</code> XML attribute can be added to a <code>OneToOne</code> or <code>ManyToOne</code> mapping.  The <code>Id</code> used for the object will be derived from the target object's <code>Id</code>.  If the <code>Id</code> is a single value, then the source object's <code>Id</code> is the same as the target object's <code>Id</code>.  If it is a composite <code>Id</code>, then the <code>IdClass</code> will contain the <code>Basic</code> <code>Id</code> attributes, and the target object's <code>Id</code> as the relationship value.  If the target object also has a composite <code>Id</code>, then the source object's <code>IdClass</code> will contain the target object's <code>IdClass</code>.
 
Defining an <code>Id</code> for a <code>OneToOne</code> or <code>ManyToOne</code> in JPA 2.0 is much simpler.  The <code>@Id</code> annotation or <code>id</code> XML attribute can be added to a <code>OneToOne</code> or <code>ManyToOne</code> mapping.  The <code>Id</code> used for the object will be derived from the target object's <code>Id</code>.  If the <code>Id</code> is a single value, then the source object's <code>Id</code> is the same as the target object's <code>Id</code>.  If it is a composite <code>Id</code>, then the <code>IdClass</code> will contain the <code>Basic</code> <code>Id</code> attributes, and the target object's <code>Id</code> as the relationship value.  If the target object also has a composite <code>Id</code>, then the source object's <code>IdClass</code> will contain the target object's <code>IdClass</code>.
  

Revision as of 09:06, 30 November 2009

How to use derived identifiers to map composite Ids through ManyToOne relationships

Defining an Id 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.

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;
    ...
}

Example JPA 2.0 ManyToOne id XML

<entity name="Address" class="org.acme.Address" access="FIELD">
    <id-class class="org.acme.PhonePK"/>
    <id name="type"/>
    <many-to-one name="owner" id="true">
        <join-column name="OWNER_ID" referencedColumnName="EMP_ID"/>
    </many-to-one>
<entity/>

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 + owner;
    }
}