×
☰ See All Chapters

JPA Annotations

In this tutorial we will list out all the JPA annotations. In this tutorial we are just listing out the annotations, annotation details and their examples will be studied in the subsequent chapters. JPA defines dozens of annotations that can be divided into the following groups:

Annotations for JPA aware classes:

  • JPA Annotations for Classes 

JPA defines three types of persistable classes which are set by the following annotations:

Entity and mapped super classes can be further configured by annotations that specify cache preferences and lifecycle event listener policy.

    • javax.persistence.Cacheable 

    • javax.persistence.EntityListeners 

    • javax.persistence.ExcludeDefaultListeners 

    • javax.persistence.ExcludeSuperclassListeners 

Another JPA class annotation defines an ID class, ID classes are useful in representing composite primary keys.

  • JPA Annotations for JPQL Queries 

The following annotations are used to define static named JPA queries:

    • javax.persistence.NamedQueries 

    • javax.persistence.NamedQuery 

    • javax.persistence.QueryHint 

Annotations for fields in JPA persistable classes:

  • JPA Annotations for Fields 

The way a field of a persistable class is managed by JPA can be set by the following annotations:

Additional annotations (and enum) are designated for enum fields:

    • javax.persistence.Enumerated 

    • javax.persistence.MapKeyEnumerated 

    • javax.persistence.EnumType 

Other additional annotations (and enum) are designated for date and calendar fields:

    • javax.persistence.Temporal 

    • javax.persistence.TemporalType 

    • javax.persistence.MapKeyTemporal 

 

  • JPA Annotations for Relationships 

Relationships are persistent fields in persistable classes that reference other entity objects. The four relationship modes are represented by the following annotations:

    • javax.persistence.ManyToMany 

    • javax.persistence.ManyToOne 

    • javax.persistence.OneToMany 

    • javax.persistence.OneToOne 

Unlike ORM JPA implementations, ObjectDB does not enforce specifying any of the annotations above. Specifying a relationship annotation enables configuring cascade and fetch policy, using the following enum types:

    • javax.persistence.CascadeType 

    • javax.persistence.FetchType 

Additional annotations are supported by ObjectDB for the inverse side of a bidirectional relationship (which is calculated by a query) :

    • javax.persistence.OrderBy 

    • javax.persistence.MapKey 

Annotations for additional JPA configuration:

  • JPA Annotations for Access Modes 

Persistence fields can either be accessed by JPA directly (as fields) or indirectly (as properties and get/set methods). JPA 2 provides an annotation and an enum for setting the access mode:

  • JPA Annotations for Value Generation 

Automatically generated values are mainly useful for primary key fields, but are supported by ObjectDB also for regular (non primary key) persistent fields. At the field level, the @GeneratedValue with an optional GenerationType strategy is specified:

The @GeneratedValue annotation can also reference a value generator, which is defined at the class level by using one of the following annotations:

Annotations for JPA lifecycle event callbacks:

  • JPA Annotations for Callback Methods 

The following annotations can mark methods as JPA callback methods:

    • javax.persistence.PrePersist 

    • javax.persistence.PreRemove 

    • javax.persistence.PreUpdate 

    • javax.persistence.PostLoad 

    • javax.persistence.PostPersist 

    • javax.persistence.PostRemove 

    • javax.persistence.PostUpdate 

Java EE container annotations:

  • JPA Annotations for Java EE 

The following JPA annotations are in use to integrate JPA into a Java EE application and are managed by the Java EE container:

    • javax.persistence.PersistenceContext 

    • javax.persistence.PersistenceContextType 

    • javax.persistence.PersistenceContexts 

    • javax.persistence.PersistenceProperty 

    • javax.persistence.PersistenceUnit 

    • javax.persistence.PersistenceUnits 

Many other JPA annotations are designated for Object Relational Mapping (ORM) JPA providers (for mapping between a relational database and an object model and for direct SQL queries):

  • JPA Annotations for Mapping (ORM) 

The following JPA annotations and enums are designated for mapping a JPA object model to a relational database.

    • javax.persistence.AssociationOverride 

    • javax.persistence.AssociationOverrides 

    • javax.persistence.AttributeOverride 

    • javax.persistence.AttributeOverrides 

    • javax.persistence.CollectionTable 

    • javax.persistence.Column 

    • javax.persistence.DiscriminatorColumn 

    • javax.persistence.DiscriminatorType 

    • javax.persistence.DiscriminatorValue 

    • javax.persistence.Inheritance 

    • javax.persistence.InheritanceType 

    • javax.persistence.JoinColumn 

    • javax.persistence.JoinColumns 

    • javax.persistence.JoinTable 

    • javax.persistence.Lob 

    • javax.persistence.MapKeyClass 

    • javax.persistence.MapKeyColumn 

    • javax.persistence.MapKeyJoinColumn 

    • javax.persistence.MapKeyJoinColumns 

    • javax.persistence.MapsId 

    • javax.persistence.OrderColumn 

    • javax.persistence.PrimaryKeyJoinColumn 

    • javax.persistence.PrimaryKeyJoinColumns 

    • javax.persistence.SecondaryTable 

    • javax.persistence.SecondaryTables 

    • javax.persistence.Table 

    • javax.persistence.UniqueConstraint 

 

  • JPA Annotations for SQL Queries 

The following JPA annotations are designated for SQL queries on relational databases:

    • javax.persistence.ColumnResult 

    • javax.persistence.EntityResult 

    • javax.persistence.FieldResult 

    • javax.persistence.NamedNativeQueries 

    • javax.persistence.NamedNativeQuery 

    • javax.persistence.SqlResultSetMapping 

    • javax.persistence.SqlResultSetMappings 

Annotations for Associations:


All Chapters
Author