Jump to: navigation, search

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

Line 6: Line 6:
*[http://www.eclipse.org/eclipselink/api/2.2/javax/persistence/Id.html @Id]
*[http://www.eclipse.org/eclipselink/api/latest/javax/persistence/Id.html @Id]
*[http://www.eclipse.org/eclipselink/api/2.2/javax/persistence/Id.html @IdClass]
*[http://www.eclipse.org/eclipselink/api/latest/javax/persistence/Id.html @IdClass]
*[http://www.eclipse.org/eclipselink/api/2.2/javax/persistence/Id.html @GeneratedValue]
*[http://www.eclipse.org/eclipselink/api/latest/javax/persistence/Id.html @GeneratedValue]
*[http://www.eclipse.org/eclipselink/api/latest/org/eclipse/persistence/annotations/PrimaryKey.html @PrimaryKey]

Revision as of 11:50, 14 June 2011

EclipseLink JPA

Mailing ListForumsIRCmattermost
OpenHelp WantedBug Day
Browse Source


Use the @Id annotation or <id> XML element to designate one or more persistent fields or properties as the entity's primary key.

For each entity, you must designate at least one of the following:

Elug note icon.png

Note: The @Id and @IdClass combination – is only applicable to composite primary key configuration.

The @Id annotation does not have attributes.

By default, the entities Id must be set by the application, normally before the persist is called. A @GeneratedValue can be used to have EclipseLink generate the Id value.

If the Id (or part of the Id) is also a foreign key from a reference to another entity, the @Id annotation should be placed on the @OneToOne or @ManyToOne attribute. The entity's Id is then composed of the Id of the referenced entity.

This example shows how to use this annotation to designate the persistent field empID as the primary key of the Employee table.

Example: @Id Annotation
public class Employee implements Serializable {
    private int empID;

Example: Id XML
<entity class="Employee">
        <id name="empID"/>
Elug javaspec icon.gif

For more information, see Section 11.1.18 "Id Annotation" in the JPA Specification.

The @Id annotation supports the use of EclipseLink converters. See Using EclipseLink JPA Converters.

It is possible to generate the value for an Id using the prePersist event, or through triggers when using the Oracle database platform. See Returning.

Allowing Zero Value Primary Keys

By default, EclipseLink interprets zero as null for primitive types that cannot be null (such as int and long) causing zero to be an invalid value for primary keys. You can modify this setting by using the eclipselink.id-validation property in the persistence.xml file. Valid values are:

  • NULL – EclipseLink interprets zero values as zero. This permits primary keys to use a value of zero.
  • ZERO (default) – EclipseLink interprets zero as null.
  • NEGATIVE – EclipseLink interprets negative values as null.
  • NONE – EclipseLink does not validate the id value.

You can also use the @PrimaryKey annotation to configure the id validation for a specific entity.

Version: 2.2.0 DRAFT
Other versions...