jakarta.persistence.Entity
- Implemented Interfaces:
Annotation
- Target:
- Type
- be a non-
finaltop-level class or static inner class, - have a
publicorprotectedconstructor with no parameters, and - have no
finalmethods 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
columnin 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-oneassociation usually maps to a foreign key column or columns in one of the tables mapped by the entity, - a
one-to-oneassociation usually maps to a unique foreign key relationship (sometimes using a shared primary key), - a
one-to-manyassociation usually maps to a foreign key column or columns in one of the tables mapped by the associated entity, and - a
many-to-manyassociation 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:
- Jakarta Persistence (JPA) 1.0
Entity.Annotation Elements
String name- Default:
- ""
- Since:
- Jakarta Persistence (JPA) 1.0
Additional JDK methods inherited from java.lang.annotation.Annotation
annotationType(), equals(Object), hashCode(), toString()