Annotations used for Hibernate mapping
Sure! Let’s go through some important annotations used in Hibernate mapping, explain them, and then provide Java examples for each Hibernate mapping.
Table of Contents
1. @Entity
 Explanation:
The @Entity annotation is used to mark a class as an entity, which means it is a JPA entity and should be mapped to a database table. It tells Hibernate to manage the class and persist its instances to the database.
Example
Java Example:
java
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class User {
@Id
private Long id;
private String name;
private String email;
// Getters and setters
}
2. @Table
Explanation:
- The @Table annotation is used to specify the name of the database table that will be used for the entity.
- It can be used to define other table attributes such as schema, catalog, and unique constraints.
Example
Java Example:
java
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "users")
public class User {
@Id
private Long id;
private String name;
private String email;
// Getters and setters
}
3. @Id
Explanation:
- The @Id annotation is used to specify the primary key of an entity.
- This field or property should be unique and not null.
Example
Java Example:
java
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class User {
@Id
private Long id;
private String name;
private String email;
// Getters and setters
}
4. @GeneratedValue
Explanation:
- The @GeneratedValue annotation is used to specify the generation strategy for the primary key values.
- Common strategies include AUTO, IDENTITY, SEQUENCE, and TABLE.
Example
Java Example:
java
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
private String email;
// Getters and setters
}
5. @Column
Explanation:
- The @Column annotation is used to specify the details of the column to which a field or property will be mapped.
- It can define attributes like column name, nullable, unique, length, etc.
Example
Java Example:
java
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "user_name", nullable = false)
private String name;
@Column(unique = true)
private String email;
// Getters and setters
}
6 .@OneToOne
Explanation:
- The @OneToOne annotation is used to define a one-to-one relationship between two entities.
- It can be unidirectional or bidirectional.
Example
Java Example:
java
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToOne;
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
private String email;
@OneToOne
private Address address;
// Getters and setters
}
@Entity
public class Address {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String street;
private String city;
// Getters and setters
}
7. @ManyToOne
Explanation:
- The @ManyToOne annotation is used to define a many-to-one relationship between two entities.
- This annotation is typically used when multiple instances of one entity are related to a single instance of another entity.
Example
Java Example:
java
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
@Entity
public class Order {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String product;
@ManyToOne
private User user;
// Getters and setters
}
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
private String email;
// Getters and setters
}
8. @ManyToMany
Explanation:
- The @ManyToMany annotation is used to define a many-to-many relationship between two entities.
- This relationship requires a join table to manage the associations. Java Example:
Example
java
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import java.util.Set;
@Entity
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
@ManyToMany
private Set<Course> courses;
// Getters and setters
}
@Entity
public class Course {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String title;
@ManyToMany(mappedBy = "courses")
private Set<Student> students;
// Getters and setters
}