Jump to: navigation, search

Difference between revisions of "EclipseLink/UserGuide/JPA/Basic JPA Development/Entities/Ids/EmbeddedId"

m (New page: {{EclipseLink_Spec |link=http://jcp.org/en/jsr/detail?id=220 |section=Section 9.1.15 "IdClass Annotation""}} {{EclipseLink_JPA |previous=[[EclipseLink/UserGuide/JPA/Basic JPA Developm...)
 
m
Line 1: Line 1:
 +
=@EmbeddedId=
 +
Use the <tt>@EmbeddedId</tt> annotation to specify an embeddable composite primary key class (usually made up of two or more primitive or JDK object types) owned by the entity.
 +
{{EclipseLink_Note
 +
|note=Composite primary keys typically arise during mapping from legacy databases when the database key is comprised of several columns.
 +
}}
  
 +
A composite primary key class has the following characteristics:
 +
* It is a POJO class.
 +
* It is a public class with a public no-argument constructor.
 +
* If you use property-based access, the properties of the primary key class are public or protected.
 +
* It is serializable.
 +
* It defines <tt>equals</tt> and <tt>hashCode</tt> methods. The semantics of value equality for these methods must be consistent with the database equality for the database types to which the key is mapped.
 +
 +
Alternatively, you can make the composite primary key class a nonembedded class (see [[EclipseLink/UserGuide/JPA/Basic JPA Development/Mapping/Entity/IdClass|@IdClass]]).
 +
 +
The <tt>@EmbeddedId</tt> annotation does not have attributes.
 +
 +
This example shows a typical composite primary key class annotated with [[#@Embeddable|<tt>@Embeddable</tt>]]. The [[#Example 18-6|Usage of @EmbeddedId Annotation]] example shows how to configure an entity with this embeddable composite primary key class using the <tt>@EmbeddedId</tt> annotation.
 +
 +
 +
<span id="'Example 18-5"></span>
 +
''''' Embeddable Composite Primary Key Class'''''
 +
<source lang="java">
 +
public class EmployeePK implements Serializable {
 +
 +
    private String empName;
 +
    private long empID;
 +
 +
    public EmployeePK() {
 +
    }
 +
 +
    public String getName() {
 +
        return this.empName;
 +
    }
 +
 +
    public void setName(String name) {
 +
        this.empName = name;
 +
    }
 +
 +
    public long getId() {
 +
        return this.empID;
 +
    }
 +
 +
    public void setId(long id) {
 +
        this.empID = id;
 +
    }
 +
 +
    public int hashCode() {
 +
        return (int)this.empName.hashCode()+ this.empID;
 +
    }
 +
 +
    public boolean equals(Object obj) {
 +
        if (obj == this) return true;
 +
        if (!(obj instanceof EmployeePK)) return false;
 +
        if (obj == null) return false;
 +
        EmployeePK pk = (EmployeePK) obj;
 +
        return pk.empID == this.empID && pk.empName.equals(this.empName);
 +
    }
 +
}
 +
</source>
 +
 +
<span id="Example 18-6"></span>
 +
''''' Usage of @EmbeddedId Annotation'''''
 +
<source lang="java">
 +
@Entity
 +
public class Employee implements Serializable{
 +
 +
    EmployeePK primaryKey;
 +
 +
    public Employee {
 +
    }
 +
 +
    @EmbeddedId
 +
    public EmployeePK getPrimaryKey() {
 +
        return primaryKey:
 +
    }
 +
 +
    public void setPrimaryKey(EmployeePK pk) {
 +
        primaryKey = pk;
 +
    }
 +
 +
    ...
 +
}
 +
</source>
  
 
{{EclipseLink_Spec
 
{{EclipseLink_Spec
 
|link=http://jcp.org/en/jsr/detail?id=220
 
|link=http://jcp.org/en/jsr/detail?id=220
|section=Section 9.1.15 "IdClass Annotation""}}
+
|section=9.1.14 "EmbeddedId Annotation"}}
  
  

Revision as of 08:55, 17 June 2010

@EmbeddedId

Use the @EmbeddedId annotation to specify an embeddable composite primary key class (usually made up of two or more primitive or JDK object types) owned by the entity.

Elug note icon.png

Note: Composite primary keys typically arise during mapping from legacy databases when the database key is comprised of several columns.

A composite primary key class has the following characteristics:

  • It is a POJO class.
  • It is a public class with a public no-argument constructor.
  • If you use property-based access, the properties of the primary key class are public or protected.
  • It is serializable.
  • It defines equals and hashCode methods. The semantics of value equality for these methods must be consistent with the database equality for the database types to which the key is mapped.

Alternatively, you can make the composite primary key class a nonembedded class (see @IdClass).

The @EmbeddedId annotation does not have attributes.

This example shows a typical composite primary key class annotated with @Embeddable. The Usage of @EmbeddedId Annotation example shows how to configure an entity with this embeddable composite primary key class using the @EmbeddedId annotation.


Embeddable Composite Primary Key Class

 public class EmployeePK implements Serializable {
 
     private String empName;
     private long empID;
 
     public EmployeePK() {
     }
 
     public String getName() {
         return this.empName;
     }
 
     public void setName(String name) {
         this.empName = name;
     }
 
     public long getId() {
         return this.empID;
     }
 
     public void setId(long id) {
         this.empID = id;
     }
 
     public int hashCode() {
         return (int)this.empName.hashCode()+ this.empID;
     }
 
     public boolean equals(Object obj) {
         if (obj == this) return true;
         if (!(obj instanceof EmployeePK)) return false;
         if (obj == null) return false;
         EmployeePK pk = (EmployeePK) obj;
         return pk.empID == this.empID && pk.empName.equals(this.empName);
     }
 }

Usage of @EmbeddedId Annotation

 @Entity
 public class Employee implements Serializable{
 
     EmployeePK primaryKey;
 
     public Employee {
     }
 
     @EmbeddedId
     public EmployeePK getPrimaryKey() {
         return primaryKey:
     }
 
     public void setPrimaryKey(EmployeePK pk) {
         primaryKey = pk;
     }
 
     ...
 }
Elug javaspec icon.gif

For more information, see 9.1.14 "EmbeddedId Annotation" in the JPA Specification.


Eclipselink-logo.gif
Version: DRAFT
Other versions...