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:

@Entity:

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

				
			

@Table:

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

				
			

@Id:

  • 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.
				
					@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

				
			

@GeneratedValue:

  • 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;

				
			

@Column:

  • 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;

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

				
			

@ManyToMany:

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

				
			

@JoinColumn:

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

				
			

@Enumerated:

  • 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.
				
					@Enumerated(EnumType.STRING)
private Status status;

				
			

@Temporal:

  • Used to map java.util.Date or java.util.Calendar fields to a database date or timestamp column.
				
					@Temporal(TemporalType.DATE)
private Date birthDate;