Annotation Interface Id


@Target({METHOD,FIELD}) @Retention(RUNTIME) public @interface Id
Identifies the primary key field or property of an entity class. Every entity must have a primary key. The primary key must be declared by:
  • the entity class that is the root of the entity hierarchy, or
  • a mapped superclass that is a (direct or indirect) superclass of all entity classes in the entity hierarchy.
A primary key must be defined exactly once in each entity hierarchy.

The field or property to which the Id annotation is applied should have one of the following types:

The mapped column for the primary key of the entity is assumed to be the primary key of the primary table. If no Column annotation is specified, the primary key column name is assumed to be the name of the primary key property or field.

For example, this field holds a generated primary key assigned by the persistence provider:

@Id @GeneratedValue
UUID uuid;

This property holds a primary key assigned by the application:

@Id
public String getIsbn() {
    return isbn;
}

public void setIsbn(String isbn) {
    this.isbn = isbn;
}

An entity must declare or inherit exactly one field or property annotated @Id or @EmbeddedId unless the entity specifies an @IdClass, in which case the entity must have a field or property annotated @Id for each field or property of its id class.

The @Id annotation should never be applied to a field or property of an embeddable id or id class.

The persistence provider is permitted to use the value of the identifier and version fields or properties of an entity instance to determine whether the instance is new or detached.

Since:
1.0
See Also: