Java Persistence API (JPA) Extensions Reference for EclipseLink, Release 2.4
  Go To Table Of Contents
 Search
 PDFComments
Comments


@PrimaryKey

Use @PrimaryKey to allow advanced configuration of the ID.

A validation policy can be given that allows specifying if zero is a valid ID value. The set of primary key columns can also be specified precisely.


Annotation Elements

Table 2-53 describes this annotation's elements.

Table 2-53 @PrimaryKey Annotation Elements

Annotation Element Description Default

cacheKeyType

(Optional) Configures the cache key type to store the object in the cache.

AUTO

columns

(Optional) Directly specify the primary key columns. This can be used instead of @Id if the primary key includes a non basic field, such as a foreign key, or a inheritance discriminator, embedded, or transformation mapped field.


validation

(Optional) Configures what ID validation is done:

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

By default 0 is not a valid ID value, this can be used to allow 0 ID values.

ZERO



Usage

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 @PrimaryKey annotation to configure an IdValidation for an entity class. Use the eclipselink.id-validation property to configure an IdValidation for the entire persistence unit.

Setting the validation element also affects how EclipseLink generates IDs: new IDs are generated only for IDs that are not valid (null or 0, by default); setting to NONE disables ID generation.


Examples

Example 2-91 shows how to use this annotation.

Example 2-91 Using @PrimaryKey Annotation

@PrimaryKey(validation=IdValidation.ZERO)
public class Employee implements Serializable, Cloneable {
...
}

Example 2-92 shows how to use the <primary-key> element in your eclipselink-orm.xml file.

Example 2-92 Using @<primary-key> XML

<entity name="Employee" class="foo.Employee" access="PROPERTY">
   <primary-key validation="ZERO"/>
...
</entity>


See Also

For more information, see: