Jump to: navigation, search

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

(Example: Nonembedded Composite Primary Key Class)
(Example: Nonembedded Composite Primary Key Class)
Line 65: Line 65:
 
         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.birthDay.equals(this.birthDay) && pk.empName.equals(this.empName);
 
         return pk.birthDay.equals(this.birthDay) && pk.empName.equals(this.empName);

Revision as of 10:11, 8 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

@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.

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.
  • 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.

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;
   ...
}


Elug javaspec icon.gif

For more information, see Section 11.1.19 "IdClass Annotation" in the JPA Specification.


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