JPA Entity Primary Key Using @Id and @IdClass

At this tutorial we’ve explained the concept of entity identification. Entity identification is the process in which the Java Persistence Implementation identify an entity uniquely from set of entities that belong to the same type on a memory level or a database level. Each entity that located in the persistence context must has a unique identifier called Primary Key.

The primary key must be defined on the entity that is form the root of the entity hierarchy or on mapped superclass of the entity hierarchy. The primary key must be defined once in an entity hierarchy, so if you’ve ever tried to create an entity without mentioning of its primary key (Entity Identifier), you are always getting a compiler error as you would be seeing in the following fragment below.

This fragment of code was extracted from a previous code that made before for Employee Entity (See EclipseLink Tutorial Examples). The identifier is only defined inside Employee entity; Employee is the root of the entity hierarchy that contains a Developer, ContractorDeveloper and FreelanceDeveloper as a sub-classes (entities).

A simple (i.e non-composite) primary key must correspond to a single entity persistent field or property of the entity class. The @Id annotation is used to denote a simple primary key. A composite primary key must correspond to a either a single persistent field or property or to a set of fields or properties.

The primary key (or field or property of a composite primary key) should be one of the following types:

Java primitive types

Java primitive wrapper types

java.lang.String

java.util.Date (note that the Temporal Type should be specified as DATE)

java.sql.Date

No rules for simple primary key and few rules applied to composite primary key. The following are the rules applied to the composite primary key:

The primary key class must be public and must have no-arg constructor.

The primary key class must be serializable

The primary key class must define the equals and hashCode methods.

The composite primary key must either be represented and mapped as embeddable class or must be represented and mapped to multiple fields or properties of the entity class as you would be seeing in this tutorial.

If the composite primary key class is mapped to multiple fields or properties of the entity class, the names of primary key field or properties in the primary class and those of the entity class must correspond and their types must be the same.

If the property-access based is used, the properties of the primary key class must be public or protected.

@Id

The Id annotation specifies the primary key property or field of an entity. The Id annotation may be applied in an entity or mapped superclass. See the following fragment of code that shows you the Project entity. Project entity is the root of the entity inheritance.

No entity identifier has been defined, cause it’s already defined in the Project.

Anatomy of @Id

Target: Field or Methods

Uses: @Id

Argument: No argument provided

@Target({METHOD, FIELD}) @Retention(RUNTIME)
public @interface Id {}

@IdClass

The IdClass annotation is applied to an entity class or mapped superclass to specify a composite primary key class that’s mapped to multiple fields or properties of the entity.The names of primary key field or properties in the primary class and those of the entity class must correspond and their types must be the same. The Id annotation must also be applied to the corresponding fields or properties of the entity. The next coming lines will show you how to use the IdClass annotation.

@EmbeddedId

Summary

Java Persistence API provides you a various kind of primary keys. Primary key used to identify the entity that’s being persisted either in the memory or inside the database. @Id used to define a simple primary key, while the @IdClass and @EmbeddedId for composite.