Annotation Interface Entity


@Documented @Target(TYPE) @Retention(RUNTIME) public @interface Entity
Declares that the annotated class is an entity. The annotated entity class must:
  • be a non-final top-level class or static inner class,
  • have a public or protected constructor with no parameters, and
  • have no final methods or persistent instance variables.

An enum, record, or interface may not be designated as an entity.

An entity has a primary table, mapped using the Table annotation, and may have one or more secondary tables, mapped using the SecondaryTable annotation.

An entity class holds state, represented as persistent fields and properties:

  • a field or property of basic type maps to a single column in one of the tables mapped by the entity,
  • a field of property of embeddable type has nested mappings to multiple columns in one of the tables mapped by the entity,
  • an element collection usually maps to a separate collection table,
  • a many-to-one association usually maps to a foreign key column or columns in one of the tables mapped by the entity,
  • a one-to-one association usually maps to a unique foreign key relationship (sometimes using a shared primary key),
  • a one-to-many association usually maps to a foreign key column or columns in one of the tables mapped by the associated entity, and
  • a many-to-many association usually maps to a join table.

Every entity class must have at least one field or property annotated Id or EmbeddedId holding the primary key of the entity. An entity class may optionally have a field or property annotated Version, which holds a value used to detect optimistic lock failure.

Fields or properties of an entity class are persistent by default. The Transient annotation or the Java transient keyword must be used to explicitly declare any field or property of an entity which is not persistent.

The entity access type determines whether the persistence provider accesses the state of the entity using getter and setter methods, or via direct field access. It is almost never necessary to explicitly specify an AccessType, since the default access type for an entity is determined by the placement of mapping annotations on the entity class.

Apart from its persistent fields and properties, an entity class may declare callback methods using PrePersist, PreUpdate, PreRemove, PostPersist, PostUpdate, and/or PostRemove.

Since:
1.0
  • Optional Element Summary

    Optional Elements
    Modifier and Type
    Optional Element
    Description
    (Optional) The entity name.
  • Element Details

    • name

      String name
      (Optional) The entity name. Defaults to the unqualified name of the entity class. This name is used to refer to the entity in queries. The name must not be a reserved literal in the Jakarta Persistence query language.
      Default:
      ""