EclipseLink – JPA Annotations

SHARE & COMMENT :

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.

eclipselink

Let’s start define them based on Java EE 6 specification:

JPA Annotations for Fields

  • javax.persistence.BasicThe @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.EmbeddedThe 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.ElementCollectionThe ElementCollection annotation defines a collection of instances of a basic type or embeddable class.
  • javax.persistence.IdThe Id annotation specifies the primary key property or field of an entity.
  • javax.persistence.EmbeddedIdThe 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.VersionThe 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.TransientThe 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.EmbeddableThe 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.EntityThe entity annotation used to specify that a class is an entity.
  • javax.persistence.MappedSuperclassThe 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.CacheableThe 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.ExcludeDefaultListenersThe 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.ExcludeSuperclassListenersThe 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.IdClassThe 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.ManyToManyA ManyToMany annotation defines a many-valued association with many-to-many multiplicity.
  • javax.persistence.ManyToOneThe ManyToOne annotation defines a single-valued association to another entity class that has many-to-one multiplicity.
  • javax.persistence.OneToManyA 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
java.util.Map

JPA Annotations for JPQL Queries

  • javax.persistence.NamedQueryThe NamedQuery annotation is used to specify a named query in the Java Persistence query language (JPQL).
  • javax.persistence.NamedQueriesThe NamedQueries annotation is used to specify a named queries in the Java Persistence query language (JPQL).
  • javax.persistence.QueryHintUsed to supply a query property or hint to the NamedQuery or NamedNativeQuery annotation

JPA Annotations for Value Generation

  • javax.persistence.GeneratedValueThe GeneratedValue annotation provides for the specification of generation strategies for the values of primary keys
  • javax.persistence.SequenceGeneratorThe 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.TableGeneratorThe 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.AccessThe 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.PrePersistThe PrePersist callback method is invoked for a given entity before the respective EntityManager persist operation for that entity is executed.
  • javax.persistence.PreRemoveThe PreRemove callback method is invoked for a given entity before the respective EntityManager remove operation for that entity is executed.
  • javax.persistence.PostPersistThe PostPersist callback method is invoked for a given entity after the respective EntityManager persist operation for that entity is executed.
  • javax.persistence.PostRemoveThe PreRemove callback methods is invoked for a given entity after the respective EntityManager remove operation for that entity is executed.
  • javax.persistence.PreUpdateThe PreUpdate callback occur before the database update operation to entity data.
  • javax.persistence.PostUpdateThe PreUpdate callback occur after the database update operation to entity data.
  • javax.persistence.PostLoadThe 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.PersistenceContextThe PersistenceContext annotation is used to express a dependency on a container-managed entity manager persistence context.
  • javax.persistence.PersistenceContextsThe PersistenceContexts annotation is used to declare one or more PersistenceContext.
  • javax.persistence.PersistencePropertyDescribes a single container or persistence provider property. Used in PersistenceContext.
  • javax.persistence.PersistenceUnitThe PersistenceUnit annotation is used to express a dependency on an entity manager factory.
  • javax.persistence.PersistenceUnitsDeclares one or more PersistenceUnit annotations.

JPA Annotations for ORM Mapping

  • javax.persistence.AssociationOverrideThe 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.AssociationOverridesThe AssociationOverrides annotation is used to declare multiple AssociationOverride annotation.
  • javax.persistence.AttributeOverrideUsed to override the mapping of a Basic (whether explicit or default) property or field or Id property or field.
  • javax.persistence.AttributeOverridesUsed to override mappings of multiple properties or fields.
  • javax.persistence.CollectionTableSpecifies 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.ColumnIs used to specify the mapped column for a persistent property or field. If no column annotation is specified, the default values apply.
  • javax.persistence.DiscriminatorColumnSpecifies the discriminator column for the SINGLE_TABLE and JOINED Inheritance mapping strategies.
  • javax.persistence.DiscriminatorValueSpecifies 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.InheritanceDefines 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.JoinColumnSpecifies 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.JoinColumnsDefines 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.JoinTableUsed 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.LobSpecifies that a persistent property or field should be persisted as a large object to a database-supported large object type.
  • javax.persistence.MapKeyClassSpecifies 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.MapKeyColumnSpecifies 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.MapKeyJoinColumnSpecifies 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.MapKeyJoinColumnsSupports 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.MapsIdDesignates 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.OrderColumnSpecifies 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.PrimaryKeyJoinColumnSpecifies a primary key column that is used as a foreign key to join to another table.
  • javax.persistence.PrimaryKeyJoinColumnsGroups PrimaryKeyJoinColumn annotations. It is used to map composite foreign keys.
  • javax.persistence.SecondaryTableSpecifies 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.SecondaryTablesSpecifies multiple secondary tables for an entity.
  • javax.persistence.TableSpecifies the primary table for the annotated entity. Additional tables may be specified using SecondaryTableor SecondaryTablesannotation.
  • javax.persistence.UniqueConstraintSpecifies that a unique constraint is to be included in the generated DDL for a primary or secondary table.

Comments

comments

About Amr Mohammed

Speak Your Mind

*

Close
Please support the site
By clicking any of these buttons you help our site to get better