JPA and Hibernate Annotations Cheat Sheet

Here's a list of commonly used annotations in JPA (Java Persistence API) and Hibernate.

JPA Annotations

@Entity: Specifies that a class is an entity and is mapped to a database table. 

@Table: Specifies the table name associated with an entity. 

@Id: Marks a field as the primary key of an entity. 

@GeneratedValue: Specifies the strategy for generating primary key values. 

@Column: Specifies the mapping for a database column. 

@Transient: Excludes a field from being persisted in the database. 

@OneToOne: Defines a one-to-one relationship between two entities. 

@OneToMany: Defines a one-to-many relationship between two entities. 

@ManyToOne: Defines a many-to-one relationship between two entities. 

@ManyToMany: Defines a many-to-many relationship between two entities. 

@JoinColumn: Specifies the foreign key column for a relationship. 

@Embedded: Specifies a persistent field or property of an entity whose value is an instance of an embeddable class. 

@NamedQuery: Declares a named query for an entity. 

@NamedNativeQuery: Declares a named native SQL query for an entity. 

@Version: Specifies the version field for optimistic locking. 

@Temporal: Specifies the type of a date or time field. 

@Lob: The @Lob annotation is used to map a property or field to a large object column in the database.

Hibernate Annotations

@Cascade: Specifies the cascade behavior for associations. 

@Fetch: Specifies the fetching strategy for associations. 

@LazyToOne: Specifies the lazy loading behavior for a to-one association. 

@LazyCollection: Specifies the lazy loading behavior for a collection association. 

@BatchSize: Specifies the batch size for loading a collection association. 

@Cacheable: Enables caching for an entity or a collection. 

@Cache: Specifies the cache region and cache strategy for an entity or a collection. 

@Formula: Defines a computed property using an SQL formula. 

@NaturalId: Marks a property as a natural identifier. 

@Filter: Defines a filter condition to be applied to a collection association. 

@Where: Specifies a SQL WHERE condition to be applied to a collection association. 

@Type: Specifies the Hibernate type for a property. 

@Any: Maps a polymorphic association to any entity type. 

@TypeDef: Defines a custom Hibernate type. 

Download JPA and Hibernate Annotations Cheat Sheet


Please note that these annotations are applicable to both JPA and Hibernate, as Hibernate is the most widely used implementation of JPA. However, Hibernate also provides additional annotations beyond the JPA specification. 

Keep in mind that this is not an exhaustive list, and there are more annotations available in JPA and Hibernate. The specific annotations you use will depend on your requirements and the mapping between your Java objects and the database schema.

Comments