Flat Preloader Icon

Hibernate – Annotations

Hibernate Annotations are a way to define the Object-Relational Mapping (ORM) mapping between Java classes and database tables, columns, and relationships using Java annotations. Annotations provide a more concise and readable way to configure Hibernate mappings compared to XML configuration files.
  • Hibernate supports a wide range of annotations for various mapping scenarios. Here are some commonly used Hibernate annotations:


  • Used to annotate a Java class to indicate that it’s a persistent entity. Each entity class corresponds to a database table.
public class Product {
    // ...



  • Specifies the name of the database table associated with the annotated entity class. You can also specify additional attributes such as the schema name.
@Table(name = "products")
public class Product {
    // ...



  • Marks a field or property as the primary key of the entity. It’s often used in conjunction with one of the generation strategy annotations like @GeneratedValue.
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;



  • Specifies the strategy for generating primary key values. Common strategies include GenerationType.IDENTITY, GenerationType.SEQUENCE, and GenerationType.AUTO.
					@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;



  • Defines the mapping between an entity attribute and a database column. You can specify attributes like column name, length, nullable, etc.
					@Column(name = "product_name", length = 100, 
nullable = false)
private String name;

  • name attribute permits the name of the column to be explicitly specified.
  • length attribute permits the size of the column used to map a value particularly for a String value.
  • ullable attribute permits the column to be marked NOT NULL when the schema is generated.>/li>
  • unique attribute permits the column to be marked as containing only unique values.

@OneToMany and @ManyToOne:

  • Used to define one-to-many and many-to-one relationships between entities. These annotations help establish associations between tables.
					@OneToMany(mappedBy = "category")
private Set<Product> products;

@JoinColumn(name = "category_id")
private Category category;



  • Indicates a many-to-many relationship between entities. It requires a join table to map the relationship.
    name = "user_role",
    joinColumns = @JoinColumn(name = "user_id"),
    inverseJoinColumns = @JoinColumn(name = "role_id")
private Set<Role> roles;



  • Specifies the mapping of a join column for relationships. It is often used in conjunction with @ManyToOne or @OneToOne.
@JoinColumn(name = "customer_id")
private Customer customer;



  • Maps an enumerated (enum) type to a database column. You can specify whether the enum should be stored as a string or an integer in the database.
private Status status;



  • Used to map java.util.Date or java.util.Calendar fields to a database date or timestamp column.
private Date birthDate;