Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

All Hibernate Annotations: Mapping Annotations

DZone's Guide to

All Hibernate Annotations: Mapping Annotations

If you make use of Hibernate, then this exhaustive reference of hibernate annotations will come in handy.

· Java Zone ·
Free Resource

Verify, standardize, and correct the Big 4 + more– name, email, phone and global addresses – try our Data Quality APIs now at Melissa Developer Portal!

This article provides a quick overview of all Hibernate mapping annotations. These Hibernate mapping annotations are from the Hibernate official user guide.

Also, check out JPA Mapping Annotations
Check out Hibernate Developer Guide and Spring Hibernate Tutorials to develop J2EE enterprise applications.

Hibernate Mapping Annotations

I list all the Hibernate mapping annotations for your quick reference :

@AccessType
@Any
@AnyMetaDef
@AnyMetaDefs
@AttributeAccessor
@BatchSize
@Cache
@Cascade
@Check
@CollectionId
@CollectionType
@ColumnDefault
@Columns
@ColumnTransformer
@ColumnTransformers
@CreationTimestamp
@DiscriminatorFormula
@DiscriminatorOptions
@DynamicInsert
@DynamicUpdate
@Entity @Fetch
@FetchProfile
@FetchProfile.FetchOverride
@FetchProfiles
@Filter
@FilterDef
@FilterDefs
@FilterJoinTable
@FilterJoinTables
@Filters
@ForeignKey
@Formula
@Generated
@GeneratorType
@GenericGenerator
@GenericGenerators
@Immutable
@Index
@IndexColumn
@JoinColumnOrFormula
@JoinColumnsOrFormulas
@JoinFormula
@LazyCollection
@LazyGroup
@LazyToOne
@ListIndexBase
@Loader
@ManyToAny
@MapKeyType
@MetaValue
@NamedNativeQueries
@NamedQueries
@NamedQuery
@Nationalized
@NaturalId
@NaturalIdCache
@NotFound
@OnDelete
@OptimisticLock
@OptimisticLocking
@OrderBy
@ParamDef
@Parameter
@Parent
@Persister
@Polymorphism
@Proxy
@RowId
@SelectBeforeUpdate
@Sort
@SortComparator
@SortNatural
@Source
@SQLDelete
@SQLDeleteAll
@SqlFragmentAlias
@SQLInsert
@SQLUpdate
@Subselect
@Synchronize
@Table
@Tables
@Target
@Tuplizer
@Tuplizers
@Type
@TypeDef
@TypeDefs
@UpdateTimestamp
@ValueGenerationType
@Where
@WhereJoinTable

@AccessType

The @AccessType annotation is deprecated. You should use either the JPA @Access or the Hibernate native @AttributeAccessor annotation.

@Any

The @Any annotation is used to define the any-to-one association, which can point to one of several entity types.
Read more about this annotation at @Any mapping official documentation.

@AnyMetaDef

The @AnyMetaDef annotation is used to provide metadata about an @Any or @ManyToAny mapping.
Read more about this annotation at @Any mapping official documentation.

@AnyMetaDefs

The @AnyMetaDefs annotation is used to group multiple @AnyMetaDef annotations.

@AttributeAccessor

The @AttributeAccessor annotation is used to specify a custom PropertyAccessStrategy. This should only be used to name a custom PropertyAccessStrategy. For property/field access type, the JPA@Access annotation should be preferred.

@BatchSize

The @BatchSize annotation is used to specify the size for batch loading the entries of a lazy collection.
Read more about this annotation at the Batch fetching official documentation.

@Cache

The @Cache annotation is used to specify the CacheConcurrencyStrategy of a root entity or a collection.
Read more about this annotation at Cachingofficial documentation.

@Cascade

The @Cascade annotation is used to apply the Hibernate specific CascadeType strategies (e.g. CascadeType.LOCK, CascadeType.SAVE_UPDATE, CascadeType.REPLICATE) on a given association.

For JPA cascading, I prefer using the javax.persistence.CascadeType instead. When combining both JPA and Hibernate CascadeType strategies, Hibernate will merge both sets of cascades.

Read more about this annotation at Cascading official documentation.

@Check

The @Check annotation is used to specify an arbitrary SQL CHECK constraint which can be defined at the class level.
Read more about this annotation at Database-level checks official documentation.

@CollectionId

The @CollectionId annotation is used to specify an identifier column for an idbag collection.
You might want to use the JPA@OrderColumn instead.

@CollectionType

The @CollectionType annotation is used to specify a custom collection type.

The collection can also name a @Type, which defines the Hibernate Type of the collection elements.
Read more about this annotation at Custom collection types official documentation. 

@ColumnDefault

The @ColumnDefault annotation is used to specify the DEFAULT DDL value to apply when using the automated schema generator.
The same behavior can be achieved using the definition attribute of the JPA @Column annotation.

Read more about this annotation at Default value for a database column official documentation.

@Columns

The @Columns annotation is used to group multiple JPA @Column annotations.

Read more about this annotation at @Columns mapping official documentation.

@ColumnTransformer

The @ColumnTransformer annotation is used to customize how a given column value is read from or write into the database.

Read more about this annotation at @ColumnTransformer mapping official documentation.

@ColumnTransformers

The @ColumnTransformers annotation is used to group multiple @ColumnTransformer annotations.

@CreationTimestamp

The @CreationTimestamp annotation is used to specify that the currently annotated temporal type must be initialized with the current JVM timestamp value.

Read more about this annotation at @CreationTimestamp mapping official documentation.

@DiscriminatorFormula

The @DiscriminatorFormula annotation is used to specify a Hibernate @Formula to resolve the inheritance discriminator value.

Read more about this annotation at @DiscriminatorFormula  official documentation.

@DiscriminatorOptions

The @DiscriminatorOptions annotation is used to provide the force and insert Discriminator properties.

Read more about this annotation at Discriminator official documentation.

@DynamicInsert

The @DynamicInsert annotation is used to specify that the INSERT SQL statement should be generated whenever an entity is to be persisted.

By default, Hibernate uses a cached INSERT statement that sets all table columns. When the entity is annotated with the @DynamicInsert annotation, the PreparedStatement is going to include only the non-null columns.

Read more about this annotation at @CreationTimestamp mapping official documentation.

@DynamicUpdate

The @DynamicUpdate annotation is used to specify that the UPDATE SQL statement should be generated whenever an entity is modified.

By default, Hibernate uses a cached UPDATE statement that sets all table columns. When the entity is annotated with the @DynamicUpdate annotation, the PreparedStatement is going to include only the columns whose values have been changed.

Read more about this annotation at @DynamicUpdate  official documentation.

@Entity

The @Entity annotation is deprecated. Use the JPA @Entity annotation instead.

@Fetch

The @Fetch annotation is used to specify the Hibernate specific FetchMode (e.g. JOIN, SELECT, SUBSELECT) used for the currently annotated association:

Read more about this annotation at @Fetch mapping official documentation.

@FetchProfile

The @FetchProfile annotation is used to specify a custom fetching profile, similar to a JPA Entity Graph.

Read more about this annotation at Fetch mapping official documentation.

@FetchProfile.FetchOverride

The @FetchProfile.FetchOverride annotation is used in conjunction with the @FetchProfile annotation, and it's used for overriding the fetching strategy of a particular entity association.

Read more about this annotation at Fetch profile official documentation.

@FetchProfiles

The @FetchProfiles annotation is used to group multiple @FetchProfile annotations.

@Filter

The @Filter annotation is used to add filters to an entity or the target entity of a collection.

Read more about this annotation at Filter mapping official documentation.

@FilterDef

The @FilterDef annotation is used to specify a @Filter definition (name, default condition and parameter types, if any).

Read more about this annotation at Filter mapping  official documentation.

@FilterDefs

The @FilterDefs annotation is used to group multiple @FilterDef annotations.

@FilterJoinTable

The @FilterJoinTable annotation is used to add @Filter capabilities to a join table collection.

Read more about this annotation at FilterJoinTable mapping official documentation.

@FilterJoinTables

The @FilterJoinTables annotation is used to group multiple @FilterJoinTable annotations.

@Filters

The @Filters annotation is used to group multiple @Filter annotations.

@ForeignKey

The @ForeignKey annotation is deprecated. Use the JPA 2.1 @ForeignKey annotation instead.

@Formula

The @Formula annotation is used to specify an SQL fragment that is executed in order to populate a given entity attribute.

Read more about this annotation at @Formula mapping official documentation.

@Generated

The @Generated annotation is used to specify that the currently annotated entity attribute is generated by the database.

Read more about this annotation at @Generated mapping official documentation.

@GeneratorType

The @GeneratorType annotation is used to provide a ValueGenerator and a GenerationTime for the currently annotated generated attribute.

Read more about this annotation at @GeneratorType mapping official documentation.

@GenericGenerator

The @GenericGenerator annotation can be used to configure any Hibernate identifier generator.

Read more about this annotation at @GenericGenerator mapping official documentation.

@GenericGenerators

The @GenericGenerators annotation is used to group multiple @GenericGenerator annotations.

@Immutable

The@Immutable annotation is used to specify that the annotated entity, attribute, or collection is immutable.

Read more about this annotation at @Immutable mapping official documentation. 

@Index

The @Index annotation is deprecated. Use the JPA @Index annotation instead.

@IndexColumn

The @IndexColumn annotation is deprecated. Use the JPA @OrderColumn annotation instead.

@JoinColumnOrFormula

The @JoinColumnOrFormula annotation is used to specify that the entity association is resolved either through a FOREIGN KEY join (e.g. @JoinColumn) or using the result of a given SQL formula (e.g. @JoinFormula).
Read more about this annotation at @JoinColumnOrFormula mapping section for more info.

@JoinColumnsOrFormulas

The @JoinColumnsOrFormulas annotation is used to group multiple @JoinColumnOrFormula annotations.

@JoinFormula

The @JoinFormula annotation is used as a replacement for @JoinColumn when the association does not have a dedicated FOREIGN KEY column.
Read more about this annotation at @JoinFormula mapping official documentation.

@LazyCollection

The @LazyCollection annotation is used to specify the lazy fetching behavior of a given collection.

The TRUE and FALSE values are deprecated since you should be using the JPA FetchType attribute of the @ElementCollection, @OneToMany, or @ManyToMany collection.

Read more about this annotation at @LazyCollection mapping official documentation.

@LazyGroup

The @LazyGroup annotation is used to specify that an entity attribute should be fetched along with all the other attributes belonging to the same group.

To load entity attributes lazily, bytecode enhancement is needed. By default, all non-collection attributes are loaded in one group named "DEFAULT."

This annotation allows defining different groups of attributes to be initialized together when access one attribute in the group.

Read more about this annotation at @LazyGroup mapping official documentation.

@LazyToOne

The @LazyToOne annotation is used to specify the laziness options, represented by LazyToOneOption, available for a @OneToOne or @ManyToOne association.

Read more about this annotation at @LazyToOne mapping example section for more info.

@ListIndexBase

The @ListIndexBase annotation is used to specify the start value for a list index, as stored in the database.

By default, List indexes are stored starting at zero. This is generally used in conjunction with @OrderColumn.

Read more about this annotation at @ListIndexBase mapping official documentation. 

@Loader

The @Loader annotation is used to override the default SELECT query used for loading an entity loading.

Read more about this annotation at Custom CRUD mapping official documentation. 

@ManyToAny

The @ManyToAny annotation is used to specify a many-to-one association when the target type is dynamically resolved.

Read more about this annotation at @ManyToAny mapping  official documentation.

@MapKeyType

The @MapKeyType annotation is used to specify the map key type.

Read more about this annotation at @MapKeyType mapping section for more info.

@MetaValue

The @MetaValue annotation is used by the @AnyMetaDef annotation to specify the association between a given discriminator value and an entity type.

Read more about this annotation at @Any mapping official documentation. 

@NamedNativeQueries

The @NamedNativeQuery annotation extends the JPA @NamedNativeQuery with Hibernate specific features.

Read more about this annotation at Hibernate @NamedNativeQuery section for more info.

@NamedQueries

The @NamedQueries annotation is used to group multiple @NamedQuery annotations.

@NamedQuery

The @NamedQuery annotation extends the JPA @NamedQuery with Hibernate specific features.

Read more about this annotation at @NamedQuery  official documentation.

@Nationalized

The @Nationalized annotation is used to specify that the currently annotated attribute is a character type (e.g. String, Character, Clob) that is stored in a nationalized column type (NVARCHAR, NCHAR, NCLOB).

Read more about this annotation at @Nationalized mapping official documentation.

@NaturalId

The @NaturalId annotation is used to specify that the currently annotated attribute is part of the natural id of the entity.

Read more about this annotation at Natural Ids official documentation.

@NaturalIdCache

The @NaturalIdCache annotation is used to specify that the natural id values associated with the annotated entity should be stored in the second-level cache.

Read more about this annotation at @NaturalIdCache mapping official documentation.

@NotFound

The @NotFound annotation is used to specify the NotFoundAction strategy for when an element is not found in a given association.

Read more about this annotation at @NotFound mapping official documentation.

@OnDelete

The @OnDelete annotation is used to specify the delete strategy employed by the currently annotated collection, array, or joined subclasses. This annotation is used by the automated schema generation tool to generate the appropriate FOREIGN KEY DDL cascade directive.

Read more about this annotation at @OnDelete cascade official documentation.

@OptimisticLock

The @OptimisticLock annotation is used to specify if the currently annotated attribute will trigger an entity version increment upon being modified.

Read more about this annotation at Excluding attributes official documentation.

@OptimisticLocking

The @OptimisticLocking annotation is used to specify the currently annotated an entity optimistic locking strategy.

Read more about this annotation at Versionless optimistic locking official documentation.

@OrderBy

The @OrderBy annotation is used to specify a SQL ordering directive for sorting the currently annotated collection.

It differs from the JPA @OrderBy annotation because the JPA annotation expects a JPQL order-by fragment, not an SQL directive.

Read more about this annotation at @OrderBy mapping official documentation.

@ParamDef

The @ParamDef annotation is used in conjunction with @FilterDef so that the Hibernate Filter can be customized with runtime-provided parameter values.

Read more about this annotation at Filter mapping official documentation.

@Parameter

The @Parameter annotation is a generic parameter (basically a key/value combination) used to parametrize other annotations, like @CollectionType, @GenericGenerator, @Type, and @TypeDef.

@Parent

The @Parent annotation is used to specify that the currently annotated embeddable attribute references back the owning entity.

Read more about this annotation at @Parent mapping official documentation.

@Persister

The @Persister annotation is used to specify a custom entity or collection persister.

For entities, the custom persister must implement the EntityPersister interface.

For collections, the custom persister must implement the CollectionPersister interface.

Read more about this annotation at @Persister mapping official documentation.

@Polymorphism

The @Polymorphism annotation is used to define the PolymorphismType Hibernate will apply to entity hierarchies.

Read more about this annotation at @Polymorphism.

@Proxy

The @Proxy annotation is used to specify a custom proxy implementation for the currently annotated entity.

Read more about this annotation at @Proxy mappingofficial documentation.

@RowId

The @RowId annotation is used to specify the database column used as a ROWID pseudocolumn. For instance, Oracle defines the ROWID pseudocolumn as something that provides the address of every table row.

Read more about this annotation at @RowId mapping official documentation.

@SelectBeforeUpdate

The @SelectBeforeUpdate annotation is used to specify that the currently annotated entity state be selected from the database when determining whether to perform an update when the detached entity is reattached.

See the OptimisticLockType.DIRTY mapping official documentation 

@Sort

The @Sort annotation is deprecated. Use the Hibernate specific @SortComparator or @SortNatural annotations instead.

@SortComparator

The @SortComparator annotation is used to specify a Comparator for sorting the Set/Map in-memory.

Read more about this annotation at @SortComparator mapping official documentation 

@SortNatural

The @SortNatural annotation is used to specify that the Set/Map should be sorted using natural sorting.

Read more about this annotation at @SortNatural mapping  official documentation

@Source

The @Source annotation is used in conjunction with a @Version timestamp entity attribute indicating the SourceType of the timestamp value.

Read more about this annotation at Database-generated version timestamp mapping official documentation 

@SQLDelete

The @SQLDelete annotation is used to specify a custom SQL DELETE statement for the currently annotated entity or collection.

See the Custom CRUD mapping official documentation.

@SQLDeleteAll

The @SQLDeleteAll annotation is used to specify a custom SQL DELETE statement when removing all elements of the currently annotated collection.

Read more about this annotation at Custom CRUD mapping official documentation.

@SqlFragmentAlias

The @SqlFragmentAlias annotation is used to specify an alias for a Hibernate @Filter.

The alias (e.g. myAlias) can then be used in the @Filter condition clause using the {alias} (e.g. {myAlias}) placeholder.

Read more about this annotation at @SqlFragmentAlias mapping official documentation.

@SQLInsert

The @SQLInsert annotation is used to specify a custom SQL INSERT statement for the currently annotated entity or collection.

Read more about this annotation at Custom CRUD mapping official documentation.

@SQLUpdate

The @SQLUpdate annotation is used to specify a custom SQL UPDATE statement for the currently annotated entity or collection.

Read more about this annotation at Custom CRUD mapping official documentation.

@Subselect

The @Subselect annotation is used to specify an immutable and read-only entity using a custom SQL SELECT statement.

Read more about this annotation at Mapping the entity to a SQL query official documentation.

@Synchronize

The @Synchronize annotation is usually used in conjunction with the @Subselect annotation to specify the list of database tables used by the @Subselect SQL query.

Read more about this annotation at Mapping the entity to a SQL query official documentation.

@Table

The @Table annotation is used to specify additional information to a JPA @Table annotation, like custom INSERT, UPDATE or DELETE statements or a specific FetchMode.

Read more about this annotation at @SecondaryTable mapping official documentation.

@Tables

The @Tables annotation is used to group multiple @Table annotations.

@Target

The @Target annotation is used to specify an explicit target implementation when the currently annotated association is using an interface type.

Read more about this annotation at @Target mapping official documentation.

@Tuplizer

The @Tuplizer annotation is used to specify a custom tuplizer for the currently annotated entity or embeddable.

Read more about this annotation at @Tuplizer mapping section for more info.

@Tuplizers

The @Tuplizers annotation is used to group multiple @Tuplizer annotations.

@Type

The @Type annotation is used to specify the Hibernate @Type used by the currently annotated basic attribute.

See the @Type mapping section for more info.

@TypeDef

The @TypeDef annotation is used to specify a @Type definition, which can later be reused for multiple basic attribute mappings.

Read more about this annotation at @TypeDef mapping offiial documentation. 

@TypeDefs

The @TypeDefs annotation is used to group multiple @TypeDef annotations.

@UpdateTimestamp

The @UpdateTimestamp annotation is used to specify that the currently annotated timestamp attribute should be updated with the current JVM timestamp whenever the owning entity gets modified.

See the @UpdateTimestamp mapping official documentation. 

@ValueGenerationType

The @ValueGenerationType annotation is used to specify that the current annotation type should be used as a generator annotation type.

Read more about this annotation at @ValueGenerationType mapping section for more info.

@Where

The @Where annotation is used to specify a custom SQL WHERE clause used when fetching an entity or a collection.

Read more about this annotation at @Where mapping official documentation.

@WhereJoinTable

The @WhereJoinTable annotation is used to specify a custom SQL WHERE clause used when fetching a join collection table.

Further Learning

Developers! Quickly and easily gain access to the tools and information you need! Explore, test and combine our data quality APIs at Melissa Developer Portal – home to tools that save time and boost revenue. 

Topics:
java ,hibernate ,annotations ,mapping ,mapping annotations ,links

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}