Here is a comprehensive list of annotations used in JPA and Hibernate, grouped by functionality, along with brief explanations and examples for each.
Core JPA Annotations
Entity and Identifier
@Entity
Marks a class as a JPA entity (mapped to a database table).@Table
Specifies the database table for an entity.@Id
Specifies the primary key.@GeneratedValue
Defines the generation strategy for primary keys (e.g., AUTO, IDENTITY, SEQUENCE, TABLE).@SequenceGenerator
Defines a sequence generator for primary key generation.
Field Mapping
@Column
Customizes column mapping (e.g., name, length, nullable).@Transient
Marks a field as not persistent (excluded from database).@Lob
Maps large objects likeCLOB
orBLOB
.@Enumerated
Maps enums (default:ORDINAL
, alternative:STRING
).@Temporal
Mapsjava.util.Date
orCalendar
to SQL date/time types (DATE
,TIME
,TIMESTAMP
).@Embedded
and@Embeddable
Embeds a reusable object in the entity.@AttributeOverride
Overrides column mapping for an embedded field.
Relationships
@OneToOne
Defines a one-to-one relationship.@OneToMany
Defines a one-to-many relationship.@ManyToOne
Defines a many-to-one relationship.@ManyToMany
Defines a many-to-many relationship.@JoinColumn
Specifies the foreign key column.@JoinTable
Defines a join table for many-to-many relationships.@Cascade
(Hibernate-specific)
Controls cascading operations (e.g., persist, merge).
Inheritance
@Inheritance
Defines inheritance strategy (SINGLE_TABLE
,TABLE_PER_CLASS
,JOINED
).@DiscriminatorColumn
and@DiscriminatorValue
Used in theSINGLE_TABLE
inheritance strategy.
Lifecycle Callbacks
@PrePersist
,@PostPersist
Callback methods before/after persisting an entity.@PreUpdate
,@PostUpdate
Callback methods before/after updating an entity.@PreRemove
,@PostRemove
Callback methods before/after removing an entity.@PostLoad
Callback after loading an entity.
Queries
@NamedQuery
Defines a named HQL query.@NamedNativeQuery
Defines a named native SQL query.
Validation and Converters
@NotNull
,@Size
,@Pattern
(Bean Validation)
Adds validation constraints.@Convert
Converts a field value using a custom converter.
Caching
@Cacheable
Marks an entity as cacheable (used with second-level cache).@Cache
(Hibernate-specific)
Configures caching behavior.
Filters (Hibernate-Specific)
@FilterDef
and@Filter
Adds runtime filters to entities.
Auditing (Hibernate Envers)
@Audited
Enables auditing of entity changes.@NotAudited
Excludes specific fields from auditing.
Comments
Post a Comment