Jump to: navigation, search

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

m
(Example: Embeddable Composite Primary Key Class)
 
(19 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 +
{{EclipseLink_UserGuide
 +
|info=y
 +
|toc=n
 +
|eclipselink=y
 +
|eclipselinktype=JPA
 +
|api=y
 +
|apis=
 +
*[http://www.eclipse.org/eclipselink/api/2.2/javax/persistence/EmbeddedId.html @EmbeddedId]
 +
}}
 
=@EmbeddedId=
 
=@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.
 
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.
Line 11: Line 20:
 
* It is serializable.
 
* 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.
 
* 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.
 +
* An instance of the <code>EmbeddedId</code> is used with the <code>EntityManager find()</code> operation, to find an entity by its id.
 +
  
 
Alternatively, you can make the composite primary key class a nonembedded class (see [[EclipseLink/UserGuide/JPA/Basic JPA Development/Mapping/Entity/IdClass|@IdClass]]).
 
Alternatively, you can make the composite primary key class a nonembedded class (see [[EclipseLink/UserGuide/JPA/Basic JPA Development/Mapping/Entity/IdClass|@IdClass]]).
Line 17: Line 28:
  
 
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.
 
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>
 
<span id="'Example 18-5"></span>
''''' Embeddable Composite Primary Key Class'''''
+
======''Example: Embeddable Composite Primary Key Class''======
 
<source lang="java">
 
<source lang="java">
public class EmployeePK implements Serializable {
+
@Embeddable
 +
public class EmployeePK implements Serializable {
 
   
 
   
    private String empName;
+
    private String empName;
    private long empID;
+
    private long empID;
 
   
 
   
    public EmployeePK() {
+
    public EmployeePK() {
    }
+
    }
 
   
 
   
    public String getName() {
+
    public String getName() {
        return this.empName;
+
        return this.empName;
    }
+
    }
 
   
 
   
    public void setName(String name) {
+
    public void setName(String name) {
        this.empName = name;
+
        this.empName = name;
    }
+
    }
 
   
 
   
    public long getId() {
+
    public long getEmpID() {
        return this.empID;
+
        return this.empID;
    }
+
    }
 
   
 
   
    public void setId(long id) {
+
    public void setEmpID(long id) {
        this.empID = id;
+
        this.empID = id;
    }
+
    }
 
   
 
   
    public int hashCode() {
+
    public int hashCode() {
        return (int)this.empName.hashCode()+ this.empID;
+
        return (int)this.empName.hashCode()+ this.empID;
    }
+
    }
 
   
 
   
    public boolean equals(Object obj) {
+
    public boolean equals(Object obj) {
        if (obj == this) return true;
+
        if (obj == this) return true;
        if (!(obj instanceof EmployeePK)) return false;
+
        if (!(obj instanceof EmployeePK)) return false;
        if (obj == null) return false;
+
        EmployeePK pk = (EmployeePK) obj;
        EmployeePK pk = (EmployeePK) obj;
+
        return pk.empID == this.empID && pk.empName.equals(this.empName);
        return pk.empID == this.empID && pk.empName.equals(this.empName);
+
    }
    }
+
}
}
+
 
</source>
 
</source>
  
 
<span id="Example 18-6"></span>
 
<span id="Example 18-6"></span>
''''' Usage of @EmbeddedId Annotation'''''
+
 
 +
======''Example: @EmbeddedId Annotation''======
 
<source lang="java">
 
<source lang="java">
@Entity
+
@Entity
public class Employee implements Serializable{
+
public class Employee implements Serializable {
 
   
 
   
    EmployeePK primaryKey;
+
    @EmbeddedId
 +
    EmployeePK primaryKey;
 
   
 
   
    public Employee {
+
    public Employee {
    }
+
    }
 
   
 
   
    @EmbeddedId
+
    public EmployeePK getPrimaryKey() {
    public EmployeePK getPrimaryKey() {
+
        return primaryKey:
        return primaryKey:
+
    }
    }
+
 
   
 
   
    public void setPrimaryKey(EmployeePK pk) {
+
    public void setPrimaryKey(EmployeePK pk) {
        primaryKey = pk;
+
        primaryKey = pk;
    }
+
    }
+
 
    ...
+
    ...
}
+
}
 +
</source>
 +
 
 +
======''Example: <code><embedded-id></code> XML''======
 +
<source lang="xml">
 +
<entity class="Employee">
 +
    <attributes>
 +
        <embedded-id name="primaryKey"/>
 +
    </attributes>
 +
</entity>
 +
<embeddable class="EmployeePK"/>
 
</source>
 
</source>
  
 
{{EclipseLink_Spec
 
{{EclipseLink_Spec
 
|link=http://jcp.org/en/jsr/detail?id=220
 
|link=http://jcp.org/en/jsr/detail?id=220
|section=9.1.14 "EmbeddedId Annotation"}}
+
|section=1.1.15 "EmbeddedId Annotation"}}
  
  
  
 
{{EclipseLink_JPA
 
{{EclipseLink_JPA
|previous=[[EclipseLink/UserGuide/JPA/Basic JPA Development/Mapping/Entity/IdClass|@IdClass]]
+
|previous=[[EclipseLink/UserGuide/JPA/Basic JPA Development/Mapping/Entities/Ids/IdClass|@IdClass]]
|next=    [[EclipseLink/UserGuide/JPA/Basic JPA Development/Mapping/Entity/GeneratedValue|@GeneratedValue]]
+
|next=    [[EclipseLink/UserGuide/JPA/Basic_JPA_Development/Mapping/Entities/Ids/GeneratedValue|@GeneratedValue]]
|up=      [[EclipseLink/UserGuide/JPA/Basic JPA Development/Mapping/Entity|Entity]]
+
|up=      [[EclipseLink/UserGuide/JPA/Basic_JPA_Development/Entities/Creating_and_Configuring_Entities|Configuring Entities]]
 
|version= 2.1.0
 
|version= 2.1.0
}}
+
|version=2.2.0 DRAFT}}

Latest revision as of 10:23, 9 June 2011

EclipseLink JPA

link="http://wiki.eclipse.org/EclipseLink"
EclipseLink
Website
Download
Community
Mailing ListForumsIRC
Bugzilla
Open
Help Wanted
Bug Day
Contribute
Browse Source

Elug api package icon.png Key API

@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.
  • An instance of the EmbeddedId is used with the EntityManager find() operation, to find an entity by its id.


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.

Example: Embeddable Composite Primary Key Class
@Embeddable
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 getEmpID() {
        return this.empID;
    }
 
    public void setEmpID(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;
        EmployeePK pk = (EmployeePK) obj;
        return pk.empID == this.empID && pk.empName.equals(this.empName);
    }
}

Example: @EmbeddedId Annotation
@Entity
public class Employee implements Serializable {
 
    @EmbeddedId
    EmployeePK primaryKey;
 
    public Employee {
    }
 
    public EmployeePK getPrimaryKey() {
        return primaryKey:
    }
 
    public void setPrimaryKey(EmployeePK pk) {
        primaryKey = pk;
    }
 
    ...
}
Example: <embedded-id> XML
<entity class="Employee">
    <attributes>
        <embedded-id name="primaryKey"/>
    </attributes>
</entity>
<embeddable class="EmployeePK"/>
Elug javaspec icon.gif

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


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