Notice: this Wiki will be going read only early in 2024 and edits will no longer be 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/IdClass"
m (→@IdClass) |
(→Example: @IdClass Annotation With ManyToOne) |
||
(24 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
− | |||
{{EclipseLink_UserGuide | {{EclipseLink_UserGuide | ||
|info=y | |info=y | ||
|toc=n | |toc=n | ||
|eclipselink=y | |eclipselink=y | ||
− | |eclipselinktype=JPA}} | + | |eclipselinktype=JPA |
+ | |api=y | ||
+ | |apis= | ||
+ | *[http://www.eclipse.org/eclipselink/api/latest/javax/persistence/IdClass.html @IdClass] | ||
+ | }} | ||
=@IdClass= | =@IdClass= | ||
Line 21: | Line 24: | ||
* 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. | ||
* Its fields or properties must correspond in type and name to the entity primary key fields or properties annotated with [[EclipseLink/UserGuide/JPA/Basic JPA Development/Mapping/Entity/Id|@Id]]. | * Its fields or properties must correspond in type and name to the entity primary key fields or properties annotated with [[EclipseLink/UserGuide/JPA/Basic JPA Development/Mapping/Entity/Id|@Id]]. | ||
+ | * An instance of the <code>IdClass</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 an embedded class owned by the entity (see [[EclipseLink/UserGuide/JPA/Basic JPA Development/Mapping/Entity/EmbeddedId|@EmbeddedId]]). | Alternatively, you can make the composite primary key class an embedded class owned by the entity (see [[EclipseLink/UserGuide/JPA/Basic JPA Development/Mapping/Entity/EmbeddedId|@EmbeddedId]]). | ||
− | The <tt>@IdClass</tt> annotation has a required attribute <tt>value</tt> that you set to the class to specify this class as a composite primary key class | + | The <tt>@IdClass</tt> annotation has a required attribute <tt>value</tt> that you set to the class to specify this class as a composite primary key class. |
− | + | ||
− | + | ||
+ | The [[#Example 18-3|Nonembedded Composite Primary Key Class]] example, below, shows a nonembedded composite primary key class. In this class, fields <tt>empName</tt> and <tt>birthDay</tt> must correspond in name and type to properties in the entity class. The [[#Example 18-4|@IdClass Annotation]] example, below, shows how to configure an entity with this nonembedded composite primary key class using the <tt>@IdClass</tt> annotation. Because entity class fields <tt>empName</tt> and <tt>birthDay</tt> are used in the primary key, you must also annotate them using the <tt>@Id</tt> annotation (see [[EclipseLink/UserGuide/JPA/Basic JPA Development/Mapping/Entity/Id|@Id]]). | ||
<span id="Example 18-3"></span> | <span id="Example 18-3"></span> | ||
− | '' | + | ======''Example: Nonembedded Composite Primary Key Class''====== |
<source lang="java"> | <source lang="java"> | ||
public class EmployeePK implements Serializable { | public class EmployeePK implements Serializable { | ||
Line 48: | Line 51: | ||
} | } | ||
− | public | + | public Date getBirthDay() { |
return this.birthDay; | return this.birthDay; | ||
} | } | ||
− | public void | + | public void setBirthDay(Date date) { |
this.birthDay = date; | this.birthDay = date; | ||
} | } | ||
Line 63: | Line 66: | ||
if (obj == this) return true; | if (obj == this) return true; | ||
if (!(obj instanceof EmployeePK)) return false; | if (!(obj instanceof EmployeePK)) return false; | ||
− | |||
EmployeePK pk = (EmployeePK) obj; | EmployeePK pk = (EmployeePK) obj; | ||
− | return pk.birthDay | + | return pk.birthDay.equals(this.birthDay) && pk.empName.equals(this.empName); |
} | } | ||
} | } | ||
Line 71: | Line 73: | ||
<span id="Example 18-4"></span> | <span id="Example 18-4"></span> | ||
− | '' | + | |
+ | ======''Example: @IdClass Annotation''====== | ||
<source lang="java"> | <source lang="java"> | ||
− | + | @IdClass(EmployeePK.class) | |
− | + | @Entity | |
− | + | public class Employee implements Serializable{ | |
+ | |||
+ | @Id String empName; | ||
+ | @Id Date birthDay; | ||
+ | ... | ||
+ | } | ||
+ | </source> | ||
+ | |||
+ | ======''Example: <code><id-class></code> XML''====== | ||
+ | <source lang="xml"> | ||
+ | <entity class="Employee"> | ||
+ | <id-class class="EmployeePK"/> | ||
+ | <attributes> | ||
+ | <id name="empName"/> | ||
+ | <id name="birthDay"/> | ||
+ | </attributes> | ||
+ | </entity> | ||
+ | </source> | ||
+ | |||
+ | If one of the <code>Id</code> values comes from a <code>OneToOne</code> or <code>ManyToOne</code> relationship, then the <code>IdClass</code> must contain the <code>Id</code> of the related entity. | ||
+ | |||
+ | The [[#Example: Composite Primary Key Class With Foreign Key|Composite Primary Key Class With Foreign Key]] example, below, shows a composite primary key class that is composed of a foreign key. The id class contains the id of the department, not a department reference. | ||
+ | |||
+ | ======''Example: Composite Primary Key Class With Foreign Key''====== | ||
+ | <source lang="java"> | ||
+ | public class EmployeePK implements Serializable { | ||
− | + | private long empId; | |
− | + | private long department; | |
− | ... | + | |
− | } | + | public EmployeePK() { |
+ | } | ||
+ | |||
+ | public long getEmpId() { | ||
+ | return this.empId; | ||
+ | } | ||
+ | |||
+ | public void setEmpId(long empId) { | ||
+ | this.empId = empId; | ||
+ | } | ||
+ | |||
+ | public long getDepartment() { | ||
+ | return this.department; | ||
+ | } | ||
+ | |||
+ | public void setDepartment(long department) { | ||
+ | this.department = department; | ||
+ | } | ||
+ | |||
+ | public int hashCode() { | ||
+ | return (int)this.empId.hashCode(); | ||
+ | } | ||
+ | |||
+ | public boolean equals(Object obj) { | ||
+ | if (obj == this) return true; | ||
+ | if (!(obj instanceof EmployeePK)) return false; | ||
+ | EmployeePK pk = (EmployeePK) obj; | ||
+ | return pk.empId.equals(this.empId) && pk.department.equals(this.department); | ||
+ | } | ||
+ | } | ||
</source> | </source> | ||
+ | ======''Example: @IdClass Annotation With ManyToOne''====== | ||
+ | <source lang="java"> | ||
+ | @IdClass(EmployeePK.class) | ||
+ | @Entity | ||
+ | public class Employee implements Serializable{ | ||
+ | @Id | ||
+ | long empId; | ||
+ | @Id | ||
+ | @ManyToOne | ||
+ | Department department; | ||
+ | ... | ||
+ | } | ||
+ | </source> | ||
+ | |||
+ | ======''Example: <code><id-class></code> XML With <code><many-to-one></code>''====== | ||
+ | <source lang="xml"> | ||
+ | <entity class="Employee"> | ||
+ | <id-class class="EmployeePK"/> | ||
+ | <attributes> | ||
+ | <id name="empId"/> | ||
+ | <many-to-one name="department" id="true"> | ||
+ | <id/> | ||
+ | </many-to-one> | ||
+ | </attributes> | ||
+ | </entity> | ||
+ | </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 | + | |section=Section 11.1.19 "IdClass Annotation"}} |
{{EclipseLink_JPA | {{EclipseLink_JPA | ||
− | |previous=[[EclipseLink/UserGuide/JPA/ | + | |previous=[[EclipseLink/UserGuide/JPA/Basic_JPA_Development/Entities/Ids/Id|@Id]] |
− | |next= [[EclipseLink/UserGuide/JPA/Basic JPA Development/Mapping/ | + | |next= [[EclipseLink/UserGuide/JPA/Basic JPA Development/Mapping/Entities/Ids/EmbeddedId|@EmbeddedId]] |
− | |up= [[EclipseLink/UserGuide/JPA/ | + | |up= [[EclipseLink/UserGuide/JPA/Basic_JPA_Development/Entities/Creating_and_Configuring_Entities|Configuring Entities]] |
|version=2.2.0 DRAFT}} | |version=2.2.0 DRAFT}} |
Latest revision as of 10:12, 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
@IdClass
Use the @IdClass annotation to specify a composite primary key class (usually made up of two or more primitive, JDK object types or Entity types) for an entity or MappedSuperclass.
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.
- Its fields or properties must correspond in type and name to the entity primary key fields or properties annotated with @Id.
- An instance of the
IdClass
is used with theEntityManager find()
operation, to find an entity by its id.
Alternatively, you can make the composite primary key class an embedded class owned by the entity (see @EmbeddedId).
The @IdClass annotation has a required attribute value that you set to the class to specify this class as a composite primary key class.
The Nonembedded Composite Primary Key Class example, below, shows a nonembedded composite primary key class. In this class, fields empName and birthDay must correspond in name and type to properties in the entity class. The @IdClass Annotation example, below, shows how to configure an entity with this nonembedded composite primary key class using the @IdClass annotation. Because entity class fields empName and birthDay are used in the primary key, you must also annotate them using the @Id annotation (see @Id).
Example: Nonembedded Composite Primary Key Class
public class EmployeePK implements Serializable { private String empName; private Date birthDay; public EmployeePK() { } public String getName() { return this.empName; } public void setName(String name) { this.empName = name; } public Date getBirthDay() { return this.birthDay; } public void setBirthDay(Date date) { this.birthDay = date; } public int hashCode() { return (int)this.empName.hashCode(); } public boolean equals(Object obj) { if (obj == this) return true; if (!(obj instanceof EmployeePK)) return false; EmployeePK pk = (EmployeePK) obj; return pk.birthDay.equals(this.birthDay) && pk.empName.equals(this.empName); } }
Example: @IdClass Annotation
@IdClass(EmployeePK.class) @Entity public class Employee implements Serializable{ @Id String empName; @Id Date birthDay; ... }
Example: <id-class>
XML
<entity class="Employee"> <id-class class="EmployeePK"/> <attributes> <id name="empName"/> <id name="birthDay"/> </attributes> </entity>
If one of the Id
values comes from a OneToOne
or ManyToOne
relationship, then the IdClass
must contain the Id
of the related entity.
The Composite Primary Key Class With Foreign Key example, below, shows a composite primary key class that is composed of a foreign key. The id class contains the id of the department, not a department reference.
Example: Composite Primary Key Class With Foreign Key
public class EmployeePK implements Serializable { private long empId; private long department; public EmployeePK() { } public long getEmpId() { return this.empId; } public void setEmpId(long empId) { this.empId = empId; } public long getDepartment() { return this.department; } public void setDepartment(long department) { this.department = department; } public int hashCode() { return (int)this.empId.hashCode(); } public boolean equals(Object obj) { if (obj == this) return true; if (!(obj instanceof EmployeePK)) return false; EmployeePK pk = (EmployeePK) obj; return pk.empId.equals(this.empId) && pk.department.equals(this.department); } }
Example: @IdClass Annotation With ManyToOne
@IdClass(EmployeePK.class) @Entity public class Employee implements Serializable{ @Id long empId; @Id @ManyToOne Department department; ... }
Example: <id-class>
XML With <many-to-one>
<entity class="Employee"> <id-class class="EmployeePK"/> <attributes> <id name="empId"/> <many-to-one name="department" id="true"> <id/> </many-to-one> </attributes> </entity>
For more information, see Section 11.1.19 "IdClass Annotation" in the JPA Specification.
@Id | @EmbeddedId | |
EclipseLink Home
JPA User Guide: Table of Contents, Search |
||
How to contribute to this guide... |