All Hibernate Annotations : Mapping Annotations


This article provides a quick overview of all Hibernate mapping annotations. These Hibernate mapping annotations referred from 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 :
  1. @AccessType
  2. @Any
  3. @AnyMetaDef
  4. @AnyMetaDefs
  5. @AttributeAccessor
  6. @BatchSize
  7. @Cache
  8. @Cascade
  9. @Check
  10. @CollectionId
  11. @CollectionType
  12. @ColumnDefault
  13. @Columns
  14. @ColumnTransformer
  15. @ColumnTransformers
  16. @CreationTimestamp
  17. @DiscriminatorFormula
  18. @DiscriminatorOptions
  19. @DynamicInsert
  20. @DynamicUpdate
  21. @Entity @Fetch
  22. @FetchProfile 
  23. @FetchProfile.FetchOverride 
  24. @FetchProfiles 
  25. @Filter 
  26. @FilterDef 
  27. @FilterDefs 
  28. @FilterJoinTable 
  29. @FilterJoinTables 
  30. @Filters 
  31. @ForeignKey 
  32. @Formula
  33. @Generated
  34. @GeneratorType
  35. @GenericGenerator
  36. @GenericGenerators
  37. @Immutable
  38. @Index
  39. @IndexColumn
  40. @JoinColumnOrFormula
  41. @JoinColumnsOrFormulas
  42. @JoinFormula
  43. @LazyCollection
  44. @LazyGroup
  45. @LazyToOne
  46. @ListIndexBase
  47. @Loader
  48. @ManyToAny
  49. @MapKeyType
  50. @MetaValue
  51. @NamedNativeQueries
  52. @NamedQueries
  53. @NamedQuery
  54. @Nationalized
  55. @NaturalId
  56. @NaturalIdCache
  57. @NotFound
  58. @OnDelete
  59. @OptimisticLock
  60. @OptimisticLocking
  61. @OrderBy
  62. @ParamDef
  63. @Parameter
  64. @Parent
  65. @Persister
  66. @Polymorphism
  67. @Proxy
  68. @RowId
  69. @SelectBeforeUpdate
  70. @Sort
  71. @SortComparator
  72. @SortNatural
  73. @Source
  74. @SQLDelete
  75. @SQLDeleteAll
  76. @SqlFragmentAlias
  77. @SQLInsert
  78. @SQLUpdate
  79. @Subselect
  80. @Synchronize
  81. @Table
  82. @Tables
  83. @Target
  84. @Tuplizer
  85. @Tuplizers
  86. @Type
  87. @TypeDef
  88. @TypeDefs
  89. @UpdateTimestamp
  90. @ValueGenerationType
  91. @Where
  92. @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 one of several entity types.
See the @Any mapping section for more info.

@AnyMetaDef

The @AnyMetaDef annotation is used to provide metadata about an @Any or @ManyToAny mapping.
See the @Any mapping section for more info.

@AnyMetaDefs

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

@AttributeAccessor

The @AttributeAccessor annotation is used to specify a custom PropertyAccessStrategy.
Should only be used to name a custom PropertyAccessStrategy. For property/field access type, the JPA @Accessannotation should be preferred.
However, if this annotation is used with either value="property" or value="field", it will act just as the corresponding usage of the JPA @Access annotation.

@BatchSize

The @BatchSize annotation is used to specify the size for batch loading the entries of a lazy collection.
See the Batch fetching section for more info.

@Cache

The @Cache annotation is used to specify the CacheConcurrencyStrategy of a root entity or a collection.
See the Caching chapter for more info.

@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, prefer using the javax.persistence.CascadeType instead.
When combining both JPA and Hibernate CascadeType strategies, Hibernate will merge both sets of cascades.

See the Cascading chapter for more info.

@Check

The @Check annotation is used to specify an arbitrary SQL CHECK constraint which can be defined at the class level.
See the Database-level checks chapter for more info.

@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.
See the Custom collection types chapter for more info.

@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.

See the Default value for a database column chapter for more info.

@Columns

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

See the @Columns mapping section for more info.

@ColumnTransformer

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

See the @ColumnTransformer mapping section for more info.

@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.

See the @CreationTimestamp mapping section for more info.

@DiscriminatorFormula

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

See the @DiscriminatorFormula section for more info.

@DiscriminatorOptions

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

See the Discriminator section for more info.

@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.

See the @CreationTimestamp mapping section for more info on how @DynamicInsert works.

@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.

See the @DynamicUpdate section for more info.

@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:

See the @Fetch mapping section for more info.

@FetchProfile

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

Fetch mapping section for more info.

@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.

Fetch profile section for more info.

@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.

See the Filter mapping section for more info.

@FilterDef

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

See the Filter mapping section for more info.

@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.

See the FilterJoinTable mapping section for more info.

@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.

See the @Formula mapping section for more info.

@Generated

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

See the @Generated mapping section for more info.

@GeneratorType

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

See the @GeneratorType mapping section for more info.

@GenericGenerator

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

See the @GenericGenerator mapping section for more info.

@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.
See the @Immutable mapping section for more info.

@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).
See the @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.
See the @JoinFormula mapping section for more info.

@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.

The EXTRA value has no equivalent in the JPA specification, and it’s used to avoid loading the entire collection even when the collection is accessed for the first time. Each element is fetched individually using a secondary query.

See the @LazyCollection mapping section for more info.

@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.

See the @LazyGroup mapping section for more info.

@LazyToOne

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

See the @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. Generally used in conjunction with @OrderColumn.

See the @ListIndexBase mapping section for more info.

@Loader

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

See the Custom CRUD mapping section for more info.

@ManyToAny

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

See the @ManyToAny mapping section for more info.

@MapKeyType

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

See the @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.

See the @Any mapping section for more info.

@NamedNativeQueries

The @NamedNativeQueries annotation is used to group multiple @NamedNativeQuery annotations.
@NamedNativeQuery
The @NamedNativeQuery annotation extends the JPA @NamedNativeQuery with Hibernate specific features.

See the 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.

See the @NamedQuery section for more info.

@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).

See the @Nationalized mapping section for more info.

@NaturalId

The @NaturalId annotation is used to specify that the currently annotated attribute is part of the natural id of the entity.
See the Natural Ids section for more info.

@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.

See the @NaturalIdCache mapping section for more info.

@NotFound

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

See the @NotFound mapping section for more info.

@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.

See the @OnDelete cascade chapter for more info.

@OptimisticLock

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

See the Excluding attributes section for more info.

@OptimisticLocking

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

See the Versionless optimistic locking section for more info.

@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.

See the @OrderBy mapping section for more info.

@ParamDef

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

See the Filter mapping section for more info.

@Parameter

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

@Parent

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

See the @Parent mapping section for more info.

@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.

See the @Persister mapping section for more info.

@Polymorphism

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

See the @Polymorphism section for more info.

@Proxy

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

See the @Proxy mapping section for more info.

@RowId

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

See the @RowId mapping section for more info.

@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 section for more info on how @SelectBeforeUpdate works.

@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.

See the @SortComparator mapping section for more info.

@SortNatural

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

See the @SortNatural mapping section for more info.

@Source

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

See the Database-generated version timestamp mapping section for more info.

@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 section for more info.

@SQLDeleteAll

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

See the Custom CRUD mapping section for more info.

@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.

See the @SqlFragmentAlias mapping section for more info.

@SQLInsert

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

See the Custom CRUD mapping section for more info.

@SQLUpdate

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

See the Custom CRUD mapping section for more info.

@Subselect

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

See the Mapping the entity to a SQL query section for more info.

@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.

See the Mapping the entity to a SQL query section for more info.

@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.

See the @SecondaryTable mapping section for more info about Hibernate-specific @Table mapping.

@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.

See the @Target mapping section for more info.

@Tuplizer

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

See the @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.

See the @TypeDef mapping section for more info.

@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 section for more info.

@ValueGenerationType

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

See the @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.

See the @Where mapping section for more info.

@WhereJoinTable

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

Further Learning:

Comments