Hibernate Tutorial
Core Hibernate
Hibernate Mapping
Hibernate Annotations
Hibernate with Spring Framework
Hibernate with Database
Hibernate Log4j
Inheritance Mapping
One-to-many is a common relationship in database modeling where one record in a table can be associated with multiple records in another table. A typical example is a Department
having multiple Employee
records.
In this tutorial, we will learn how to implement a one-to-many mapping using Hibernate.
First, ensure you have Hibernate's dependencies in your project. For Maven users, this primarily means including hibernate-core
.
department
table: To store department details.employee
table: To store employee details. It will have a foreign key (department_id
) referring to the department
table.@Entity @Table(name="department") public class Department { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name="id") private int id; @Column(name="name") private String name; @OneToMany(mappedBy="department", cascade=CascadeType.ALL) private List<Employee> employees; // constructors, getters, setters... }
@Entity @Table(name="employee") public class Employee { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name="id") private int id; @Column(name="first_name") private String firstName; @Column(name="last_name") private String lastName; @ManyToOne @JoinColumn(name="department_id") private Department department; // constructors, getters, setters... }
@OneToMany(mappedBy="department", cascade=CascadeType.ALL)
: The mappedBy
attribute refers to the property name of the association on the owner side (Employee
in this case). The cascade
attribute specifies what should happen when the Department
entity is persisted, updated, or deleted. In this example, any changes to a Department
will cascade to its associated Employee
records.
@ManyToOne
: Indicates that it's a many-to-one relationship.
@JoinColumn
: Specifies the name of the foreign key column.
Session session = sessionFactory.getCurrentSession(); session.beginTransaction(); // create a department and employees Department tempDepartment = new Department("IT"); Employee tempEmployee1 = new Employee("John", "Doe"); Employee tempEmployee2 = new Employee("Mary", "Public"); // associate employees with the department tempEmployee1.setDepartment(tempDepartment); tempEmployee2.setDepartment(tempDepartment); // add employees to the department tempDepartment.addEmployee(tempEmployee1); tempDepartment.addEmployee(tempEmployee2); // save the department (due to CascadeType.ALL, this will also save the employees) session.save(tempDepartment); session.getTransaction().commit();
Session session = sessionFactory.getCurrentSession(); session.beginTransaction(); // fetch department with id=1 Department tempDepartment = session.get(Department.class, 1); // print employees of the department for(Employee tempEmployee : tempDepartment.getEmployees()) { System.out.println(tempEmployee.getFirstName()); } session.getTransaction().commit();
By default, one-to-many relationships are lazily fetched in Hibernate. This means:
Department
, the associated Employee
records are not fetched until you access the collection (tempDepartment.getEmployees()
). This is done to optimize performance.Mapping one-to-many relationships in Hibernate is relatively straightforward. By defining your entities correctly and understanding the lifecycle of cascading operations, you can effectively map and manipulate complex relational data in your application. Always remember to decide the fetching strategy based on performance needs (e.g., lazy vs. eager fetching).
Hibernate one-to-many mapping example:
@OneToMany
annotation.// In the parent entity (Department) @OneToMany(mappedBy = "department") private List<Employee> employees;
Configuring one-to-many associations in Hibernate:
@OneToMany
annotation.@OneToMany(mappedBy = "department") private List<Employee> employees;
Mapping one-to-many relationships with Hibernate annotations:
@OneToMany
annotation on the reference variable in the parent entity.@OneToMany(mappedBy = "department") private List<Employee> employees;
Lazy loading with Hibernate one-to-many mapping:
fetch = FetchType.LAZY
to enable lazy loading.@OneToMany(mappedBy = "department", fetch = FetchType.LAZY) private List<Employee> employees;
Bidirectional one-to-many mapping in Hibernate:
@ManyToOne
in the child entity and @OneToMany
in the parent entity.// In the parent entity (Department) @OneToMany(mappedBy = "department") private List<Employee> employees;
// In the child entity (Employee) @ManyToOne @JoinColumn(name = "department_id") private Department department;
Cascading operations in Hibernate one-to-many mapping:
cascade
attribute in the @OneToMany
annotation.@OneToMany(mappedBy = "department", cascade = CascadeType.ALL) private List<Employee> employees;
Fetching strategies in Hibernate one-to-many associations:
select
, join
, subselect
.fetch
attribute.@OneToMany(mappedBy = "department", fetch = FetchType.LAZY) private List<Employee> employees;
HQL queries for one-to-many associations in Hibernate:
String hql = "FROM Department d JOIN d.employees e WHERE e.salary > :minSalary"; Query query = session.createQuery(hql); query.setParameter("minSalary", 50000); List<Object[]> result = query.list();
Handling orphan removal with Hibernate one-to-many mapping:
orphanRemoval = true
in the @OneToMany
annotation.@OneToMany(mappedBy = "department", cascade = CascadeType.ALL, orphanRemoval = true) private List<Employee> employees;