Notice: This Wiki is now read only and edits are no longer possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.
Difference between revisions of "EclipseLink/UserGuide/JPA/Basic JPA Development/Entities/Ids/EmbeddedId"
(→@EmbeddedId) |
(→Example: Embeddable Composite Primary Key Class) |
||
(4 intermediate revisions by the same user not shown) | |||
Line 32: | Line 32: | ||
======''Example: Embeddable Composite Primary Key Class''====== | ======''Example: Embeddable Composite Primary Key Class''====== | ||
<source lang="java"> | <source lang="java"> | ||
− | + | @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); | |
− | + | } | |
− | + | } | |
− | + | ||
</source> | </source> | ||
<span id="Example 18-6"></span> | <span id="Example 18-6"></span> | ||
+ | |||
======''Example: @EmbeddedId Annotation''====== | ======''Example: @EmbeddedId Annotation''====== | ||
<source lang="java"> | <source lang="java"> | ||
Line 94: | Line 95: | ||
</source> | </source> | ||
− | ======''Example: <code>embedded-id</code> XML''====== | + | ======''Example: <code><embedded-id></code> XML''====== |
<source lang="xml"> | <source lang="xml"> | ||
<entity class="Employee"> | <entity class="Employee"> | ||
Line 101: | Line 102: | ||
</attributes> | </attributes> | ||
</entity> | </entity> | ||
+ | <embeddable class="EmployeePK"/> | ||
</source> | </source> | ||
Latest revision as of 10:23, 9 June 2011
EclipseLink JPA
EclipseLink | |
Website | |
Download | |
Community | |
Mailing List • Forums • IRC • mattermost | |
Issues | |
Open • Help Wanted • Bug Day | |
Contribute | |
Browse Source |
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.
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 theEntityManager 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"/>
For more information, see 1.1.15 "EmbeddedId Annotation" in the JPA Specification.