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/IdClass"
m (New page: =@IdClass= Use the <tt>@IdClass</tt> 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 Mapp...) |
m (→@IdClass) |
||
Line 3: | Line 3: | ||
Use the <tt>@IdClass</tt> 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. | Use the <tt>@IdClass</tt> 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. | ||
− | + | {{EclipseLink_Note | |
− | { | + | |note=Composite primary keys typically arise during mapping from legacy databases when the database key is comprised of several columns. |
− | | | + | }} |
− | + | ||
− | + | ||
Line 16: | Line 14: | ||
* 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. | ||
− | * Its fields or properties must correspond in type and name to the entity primary key fields or properties annotated with [[ | + | * 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]]. |
− | Alternatively, you can make the composite primary key class an embedded class owned by the entity (see [[ | + | Alternatively, you can make the composite primary key class an embedded class owned by the entity (see [[Editing 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 (see [[#@AttributeOverride|@AttributeOverride]]). | 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 (see [[#@AttributeOverride|@AttributeOverride]]). | ||
− | The [[#Example 18-3|Nonembedded Composite Primary Key Class]] example 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|Usage of @IdClass Annotation]] example 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 [[ | + | The [[#Example 18-3|Nonembedded Composite Primary Key Class]] example 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|Usage of @IdClass Annotation]] example 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]]). |
Line 83: | Line 81: | ||
{{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 | + | |section=Section 9.1.15 "IdClass Annotation""}} |
Line 89: | Line 87: | ||
{{EclipseLink_JPA | {{EclipseLink_JPA | ||
|previous=[[EclipseLink/UserGuide/JPA/Basic JPA Development/Mapping/Entity/Id|@Id]] | |previous=[[EclipseLink/UserGuide/JPA/Basic JPA Development/Mapping/Entity/Id|@Id]] | ||
− | |next= [[EclipseLink/UserGuide/JPA/Basic JPA Development/Mapping/Entity/ | + | |next= [[EclipseLink/UserGuide/JPA/Basic JPA Development/Mapping/Entity/EmbeddedId|@EmbeddedId]] |
|up= [[EclipseLink/UserGuide/JPA/Basic JPA Development/Mapping/Entity|Entity]]}} | |up= [[EclipseLink/UserGuide/JPA/Basic JPA Development/Mapping/Entity|Entity]]}} |
Revision as of 08:52, 17 June 2010
@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.
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 (see @AttributeOverride).
The Nonembedded Composite Primary Key Class example 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 Usage of @IdClass Annotation example 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).
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 long getDateOfBirth() { return this.birthDay; } public void setDateOfBirth(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; if (obj == null) return false; EmployeePK pk = (EmployeePK) obj; return pk.birthDay = this.birthDay && pk.empName.equals(this.empName); } }
Usage of @IdClass Annotation
@IdClass (EmployeePK.class) @Entity public class Employee implements Serializable{ @Id String empName; @Id Date birthDay; ... }
For more information, see Section 9.1.15 "IdClass Annotation"" in the JPA Specification.
@Id | @EmbeddedId | |
EclipseLink Home
JPA User Guide: Table of Contents, Search |
||
How to contribute to this guide... |