Have you ever been standing up close to an article that list most of the annotations that are used by the EclipseLink – JPA API? The EclipseLink provides many annotations covering the persistence into databases. But when we are coming into use of these annotations, we have already faced a big obstacles while remembering those annotations that would help us achieving what we have to do.
To make the using of annotations much easier, we have been considering them based on their locations within the JPA components (i.e. Classes & Fields), their behavior and the platform that they are living through. Throughout the article, you will find those annotations that are used with the fields and with the classes. And you will find also those annotations that are made the relationship between the different type of classes. Also, you should find those annotations that allowed you methods for listening Java Persistence API lifecycle. and finally, you should find those annotations that are queried the data.
Let’s start define them based on Java EE 6 specification:
JPA Annotations for Fields
- javax.persistence.Basic :†The @Baisc annotation is simplest type of mapping to a database column, this annotation can be applied to a persistent property or instance variables. See more details.
- javax.persistence.Embedded :†The embedded annotation is used to specify a persistent field or property of an entity whose value is an instance of an embeddable class.
- javax.persistence.ElementCollection :†The ElementCollection annotation defines a collection of instances of a basic type or embeddable class.
- javax.persistence.Id :†The Id annotation specifies the primary key property or field of an entity.
- javax.persistence.EmbeddedId :†The EmbeddedId annotation is applied to a persistent field or property of an entity class or mapped super class to denote a composite primary key that is an embeddable class. The embeddable class must be annotated as Embeddable.
- javax.persistence.Version :†The version annotation specifies the version of the field or property of an entity class that serves its optimistic lock value. The version is used to ensure integrity while merging operation and for concurrency control.
- javax.persistence.Transient :†The Transient annotation is used to annotate a property or field of an entity class, mapped super class, or embeddable class. It specifies that the property or field is not a persistent.†See more†details.
JPA Annotations for Classes
- javax.persistence.Embeddable :†The Embeddable annotation is used to specify a class whose instances are stored as intrinsic part of an owning entity and share the identity of the entity.
- javax.persistence.Entity :†The entity annotation used to specify that a class is an entity.
- javax.persistence.MappedSuperclass :†The MappedSuperclass annotation is used to specify the class whose mapping information is applied to the entities that inherit from it. A mapped super class has no separate table defined for it.
- javax.persistence.Cacheable :†The Cacheable annotation is used to specify whether an entity should be cached if caching enabled when the value of the persistence.xml caching element is ENABLE_SELECTIVE or DISABLE_SELECTIVE.†The value of the Cacheable annotation is inherited by subclasses, also it can be overridden by specifying Cacheable on a subclass.
- javax.persistence.EntityListeners : †The EntityListeners annotation specifies the callback listener classes to be used for an entity or mapped superclasses. The EntityListeners annotation may be applied to an entity class or mapped superclass.
- javax.persistence.ExcludeDefaultListeners :†The ExecludeDefaultListeners annotation specifies that the invocation of default listeners is to be executed for the entity class (or mapped super class) and its subclass
- javax.persistence.ExcludeSuperclassListeners :†The ExcludeSuperclassListener annotation specifies that the invocation of a superclass listener to be excluded for the entity class (or mapped superclass) and its subclasses.
- javax.persistence.IdClass :†The IdClass annotation is applied to an entity class or mapped superclass to specify a composite primary key class that is mapped to multiple fields or properties of the entity.
JPA Annotations for Relationship
- javax.persistence.ManyToMany :†A ManyToMany annotation defines a many-valued association with many-to-many multiplicity.
- javax.persistence.ManyToOne :†The ManyToOne annotation defines a single-valued association to another entity class that has many-to-one multiplicity.
- javax.persistence.OneToMany :†A OneToMany annotation defines a many-valued with one-to-many multiplicity.
- javax.persistence.OneToOne :†OneToOne annotation defines a single-valued association to another entity that has one-to-one multiplicity.
- javax.persistence.OrderBy :†The OrederBy annotation specifies the ordering of the elements of a collection valued association at the point when the association is retrieved.
- javax.persistence.MapKey :†The MapKey annotation is used to specify the map key for association of type
JPA Annotations for JPQL Queries
- javax.persistence.NamedQuery :†The NamedQuery†annotation is used to specify a named query in the Java Persistence query language (JPQL).
- javax.persistence.NamedQueries :†The NamedQueries annotation is used to specify a named queries in the Java Persistence query language (JPQL).
- javax.persistence.QueryHint :†Used to supply a query property or hint to the NamedQuery†or NamedNativeQuery†annotation
JPA Annotations for Value Generation
- javax.persistence.GeneratedValue :†The GeneratedValue annotation provides for the specification of generation strategies for the values of primary keys
- javax.persistence.SequenceGenerator :†The SequenceGenerator annotation defines a primary key generator that may be referenced by name when a generator element is specified for the GeneratedValue annotation.
- javax.persistence.TableGenerator :†The TableGenerator annotation defines a primary key generator that may be referenced by name when generator element is specified for the GeneratedValue
JPA Annotations for Access Modes
- javax.persistence.Access :†The Access annotation used to specify an access to be applied to an entity class, mapped class, or embeddable class or to a specific attribute of such a class.
JPA Annotations for Callback Methods – JPA Lifecycle
- javax.persistence.PrePersist :†The PrePersist callback method is invoked for a given entity before the respective EntityManager persist operation for that entity is executed.
- javax.persistence.PreRemove :†The PreRemove callback method is invoked for a given entity before the respective EntityManager remove operation for that entity is executed.
- javax.persistence.PostPersist :†The PostPersist callback method is invoked for a given entity after the respective EntityManager persist operation for that entity is executed.
- javax.persistence.PostRemove :†The PreRemove callback methods is invoked for a given entity after the respective EntityManager remove operation for that entity is executed.
- javax.persistence.PreUpdate :†The PreUpdate callback occur before the database update operation to entity data.
- javax.persistence.PostUpdate :†The PreUpdate callback occur after the database update operation to entity data.
- javax.persistence.PostLoad :†The PostLoad for an entity is invoked after the entity has been loaded into the current persistence context from the database after the refresh operation has been applied on it.
JPA Annotations for Java EE
- javax.persistence.PersistenceContext :†The PersistenceContext annotation is used to express a dependency on a container-managed entity manager persistence context.
- javax.persistence.PersistenceContexts :†The PersistenceContexts annotation is used to declare one or more PersistenceContext.
- javax.persistence.PersistenceProperty :†Describes a single container or persistence provider property. Used in PersistenceContext.
- javax.persistence.PersistenceUnit :†The PersistenceUnit annotation is used to express a dependency on an entity manager factory.
- javax.persistence.PersistenceUnits :†Declares one or more PersistenceUnit annotations.
JPA Annotations for ORM Mapping
- javax.persistence.AssociationOverride :†The AssociationOverride annotation is used to override a many-to-one or one-to-one mapping of property or field for an entity relationship.
- javax.persistence.AssociationOverrides :†The AssociationOverrides annotation is used to declare multiple AssociationOverride annotation.
- javax.persistence.AttributeOverride :†Used to override the mapping of a Basic†(whether explicit or default) property or field or Id
†property or field.
- javax.persistence.AttributeOverrides :†Used to override mappings of multiple properties or fields.
- javax.persistence.CollectionTable :†Specifies the table that is used for the mapping of collections of basic or embeddable types. Applied to the collection-valued field or property.
- javax.persistence.Column :†Is used to specify the mapped column for a persistent property or field. If no column†annotation is specified, the default values apply.
- javax.persistence.DiscriminatorColumn :†Specifies the discriminator column for the SINGLE_TABLE†and JOINED Inheritance†mapping strategies.
- javax.persistence.DiscriminatorValue :†Specifies the value of the discriminator column for entities of the given type.†The DiscriminatorValue†annotation can only be specified on a concrete entity class.†If the Discriminator†annotation is not specified and a discriminator column is used, a provider-specific function will be used to generate a value representing the entity type. If the
- javax.persistence.Inheritance :†Defines the inheritance strategy to be used for an entity class hierarchy. It is specified on the entity class that is the root of the entity class hierarchy. If the†Inheritance†annotation is not specified or if no inheritance type is specified for an entity class hierarchy, the†SINGLE_TABLE mapping strategy is used.
- javax.persistence.JoinColumn :†Specifies a column for joining an entity association or element collection. If the JoinColumn†annotation itself is defaulted, a single join column is assumed and the default values apply.
- javax.persistence.JoinColumns :†Defines mapping for composite foreign keys. This annotation groups†JoinColumn†annotations for the same relationship.†When the JoinColumns†annotation is used, both the name†and the referencedColumnName
†elements must be specified in each such JoinColumn†annotation.
- javax.persistence.JoinTable :†Used in the mapping of associations. It is specified on the owning side of an association.†A join table is typically used in the mapping of many-to-many and unidirectional one-to-many associations. It may also be used to map bidirectional many-to-one/one-to-many associations, unidirectional many-to-one relationships, and one-to-one associations (both bidirectional and unidirectional).
- javax.persistence.Lob :†Specifies that a persistent property or field should be persisted as a large object to a database-supported large object type.
- javax.persistence.MapKeyClass :†Specifies the type of the map key for associations of type java.util.Map. The map key can be a basic type, an embeddable class, or an entity. If the map is specified using Java generics, the MapKeyClass†annotation and associated type need not be specified; otherwise they must be specified.
- javax.persistence.MapKeyColumn :†Specifies the mapping for the key column of a map whose map key is a basic type. If the name†element is not specified, it defaults to the concatenation of the following: the name of the referencing relationship field or property; “_”; “KEY”.
- javax.persistence.MapKeyJoinColumn :†Specifies a mapping to an entity that is a map key. The map key join column is in the collection table, join table, or table of the target entity that is used to represent the map. If no MapKeyJoinColumn†annotation is specified, a single join column is assumed and the default values apply.
- javax.persistence.MapKeyJoinColumns :†Supports composite map keys that reference entities.The†MapKeyJoinColumns†annotation groups†MapKeyJoinColumn†annotations. When the†MapKeyJoinColumns†annotation is used, both the†
name†and the†referencedColumnName†elements must be specified in each of the grouped†MapKeyJoinColumn†annotations.
- javax.persistence.MapsId :†Designates a ManyToMany†or OneToOne†relationship attribute that provides the mapping for an EmbeddedId†primary key, an attribute within an EmbeddedId†primary key, or a simple primary key of the parent entity. The value†element specifies the attribute within a composite key to which the relationship attribute corresponds. If the entity’s primary key is of the same Java type as the primary key of the entity referenced by the relationship, the value attribute is not specified.
- javax.persistence.OrderColumn :†Specifies a column that is used to maintain the persistent order of a list. The persistence provider is responsible for maintaining the order upon retrieval and in the database. The persistence provider is responsible for updating the ordering upon flushing to the database to reflect any insertion, deletion, or reordering affecting the list.
- javax.persistence.PrimaryKeyJoinColumn :†Specifies a primary key column that is used as a foreign key to join to another table.
- javax.persistence.PrimaryKeyJoinColumns :†Groups†PrimaryKeyJoinColumn†annotations. It is used to map composite foreign keys.
- javax.persistence.SecondaryTable :†Specifies a secondary table for the annotated entity class. Specifying one or more secondary tables indicates that the data for the entity class is stored across multiple tables.
- javax.persistence.SecondaryTables :†Specifies multiple secondary tables for an entity.
- javax.persistence.Table :†Specifies the primary table for the annotated entity. Additional tables may be specified using†SecondaryTableor†SecondaryTablesannotation.
- javax.persistence.UniqueConstraint :†Specifies that a unique constraint is to be included in the generated DDL for a primary or secondary table.