Hibernate N+1 Select Problem

Hibernate N + 1 Select Problem

The N + 1 Select problem is a performance issue in Hibernate. In this problem, a Java application makes N + 1 database calls (N = number of objects fetched). For example, if N= 2, the application makes 3 (N+1= 3) database calls.

Example

Let’s understand this problem with the help of an example. In this example, we are taking two POJO classes, Employee.java and Department.java. There exists a one-to-many association mapping between the two POJO classes. So that, one Department (Parent) can have many Employees (Child).

The Department and Employee classes are written below:

Department.java

import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name="deptmnt")
public class Department {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="d_id")
private int did;
@Column(name="d_name")
private String dname;
@OneToMany(mappedBy="dep", cascade=CascadeType.PERSIST)
private List emp = new ArrayList();
public int getDid() {
return did;
}
public void setDid(int did) {
this.did = did;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
public List getEmp() {
return emp;
}
public void setEmp(List emp) {
this.emp = emp;
}
} 

In the Department class, department id (did), department name (dname), and a List of employees are present.

Employee.java

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name="employee")
public class Employee {
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
@Column(name="e_id")
private int eid;
@Column(name="e_name")
private String ename;
@ManyToOne
@JoinColumn(name="dept_id")
private Department dep;
public int getEid() {
return eid;
}
public void setEid(int eid) {
this.eid = eid;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public Department getDep() {
return dep;
}
public void setDep(Department dep) {
this.dep = dep;
}
} 

In the Employee class, employee id (eid), employee name (ename), and an object of department class are present.

Following code is of the configuration file, which contains information about the database and the mapping classes.

hibernate.cfg.xml



org.hibernate.dialect.MySQL5Dialect
com.mysql.jdbc.Driver
jdbc:mysql://localhost:3306/test2
root
root
create
true 
true



 

Now, we are going to create the main class, which stores the object of the POJO classes. Following code is of the App.java.

App.java

import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class App 
{
public static void main( String[] args )
{
Configuration cfg= new Configuration();
cfg.configure("hibernate.cfg.xml");
SessionFactory fact= cfg.buildSessionFactory();
Session sess= fact.openSession();
sess.beginTransaction();
Employee e1 = new Employee();
e1.setEname("jyotika");
Employee e2= new Employee();
e2.setEname("shubham");
Employee e3= new Employee();
e3.setEname("nihal");
Employee e4= new Employee();
e4.setEname("neha");
Department d1= new Department();
d1.setDname("IT");
d1.getEmp().add(e1);
d1.getEmp().add(e2);
e1.setDep(d1);
e2.setDep(d1);
Department d2= new Department();
d2.setDname("Accounts");
d2.getEmp().add(e3);
d2.getEmp().add(e4);
e3.setDep(d2);
e4.setDep(d2);
sess.persist(d1);
sess.persist(d2);
List depart= sess.createQuery("From Department", Department.class).getResultList();
//List depart= sess.createQuery("From Department d JOIN fetch d.emp", Department.class).getResultList();
for (Department dep: depart){
System.out.println("Department details:::::");
System.out.println(dep.getDid()+"\t"+dep.getDname());
List<Employee> emp = dep.getEmp();
System.out.println("Employees details::::::");
for (Employee employee : emp) {
System.out.println(employee.getEid() + "\t" + employee.getEname());
}
}
sess.getTransaction().commit();
sess.close();
System.out.println("done");
}
} 

OUTPUT

The above output encountered the N+1 select problem, as separate select queries are executed for different objects. The first SELECT query selects the values of the Department (parent), and the rest two is for the Employee class (child).

The N+1 problem occurs due to the following code:

List depart= sess.createQuery("From Department", Department.class).getResultList();

To resolve the N+1 Select problem we can use the following approaches:

  1. HQL JOIN fetch

In place of the above code, we can use JOIN fetch to resolve the N+1 problem.

 List depart=sess.createQuery("From Department d JOIN fetch d.emp",Department.class)

.getResultList();

  • Criteria Query

We can also use Criteria query to resolve the N+1 select Problem. Following code shows the use of criteria query:

 CriteriaBuilder builder = sess.getCriteriaBuilder();
CriteriaQuery<Department> query = builder.createQuery(Department.class);
Root root = query.from(Department.class);
root.fetch("employees", JoinType.INNER); 

After resolving the N+1 problem, only one select query is executed for all the objects.

OUTPUT

Database Tables

Employee

Department


Related Topics

Hibernate GeneratedValue Strategies

Hibernate GeneratedValue Strategies Hibernate supports some generation strategies to generate a primary key in the database table. We can access the strategy with the help of @GeneratedValue annotation. @GeneratedValue The @GeneratedValue annotation specifies how to...

3 minutes read.

Hibernate One-to-One Mapping Example

One-to-One Hibernate Mapping In One-to-One association mapping, one object of a persistent class is related to one object of another persistent class. Here, we are going to create an example of one-to-one mapping using...

3 minutes read.

Hibernate Inheritance - Single Table

Hibernate Inheritance In this inheritance strategy, only one table is created for all the classes involved in the hierarchy with an additional column known as a discriminator column. The discriminator column helps indifferentiating between...

3 minutes read.

Hibernate vs. JDBC

What is Hibernate? Hibernate is an Object-Relational Mapping (ORM) tool which reduces the difficulty in the application development. Hibernate provides a framework that interacts with the data stored in the databases. It also uses...

2 minutes read.

Hibernate Many-to-Many Mapping

Many-to-Many Hibernate Mapping with Example In Many-to-Many association mapping, more than one objects of a persistent class are associated with more than one objects of another persistent class. For example, many (categories) are related to...

3 minutes read.

Hibernate Inheritance Table Per Class

In this inheritance strategy, table per class is generated. It means a separate table is generated for each POJO class involved in the hierarchy. Unlike the SINGLE_TABLE strategy, there are no nullable values...

3 minutes read.

Hibernate Merge

Hibernate Merge Merge is a method available in Hibernate which is used to update the existing records. However, it creates a copy from the passed entity objects and returns it. In other words, the merge()...

2 minutes read.

Hibernate Named Query Using XML

A named query is a technique used to assemble all the queries (Native SQL and HQL) in a specific location and refer them by some name. It helps in reducing the mess...

3 minutes read.

Hibernate Lifecycle

Hibernate Lifecycle In Hibernate, the mapped instances of the entity classes have a lifecycle. Either we can create a new object, or we can fetch the existing data from the database. The value objects do...

3 minutes read.

Hibernate N+1 Select Problem

Hibernate N + 1 Select Problem The N + 1 Select problem is a performance issue in Hibernate. In this problem, a Java application makes N + 1 database calls (N = number...

4 minutes read.

Lazy Loading in Hibernate

Lazy loading is a fetching technique used for all the entities in Hibernate. It decides whether to load a child class object while loading the parent class object. When we use association mapping...

3 minutes read.

Hibernate Mapping

Hibernate Mapping Association- It means that two or more things are related to each other by some specific relation. In other words, it specifies how the objects are associated with each other. Hibernate mapping is one...

2 minutes read.

Hibernate Web Application with Example

Let us create a hibernate web Application. Here we need JSP file for presentation. There are steps given below for the web app creation. index.jsp This page will show a registration form. It takes the...

2 minutes read.

Hibernate Composite Primary Key

Hibernate Composite Primary Key A Composite Primary Key is a combination of one or more columns that forms a primary key. When a database table contains more than one primary key column, it is...

8 minutes read.

Annotation Example in Hibernate

Hibernate Annotation Example Hibernate is an ORM (Object-Relational Mapping) tool which simplifies the application development. Hibernate provides a framework which interacts with the data stored in the databases, and it also uses...

3 minutes read.

Hibernate Annotation with Example

Hibernate is an ORM (Object-Relational Mapping) tool which simplifies the application development. Hibernate provides a framework which interacts with the data stored in the databases and it also uses the specifications of the...

2 minutes read.

Hibernate Configuration Properties

Hibernate Configuration Hibernate Configuration is a Java class, which allows a Java application to specify configuration parameters used in the application. As Hibernate is designed to serve in different environments, it needs a broad...

6 minutes read.

Hibernate One-to-Many Mapping

One-to-Many Hibernate Mapping Example In One-to-many association mapping, only one object of a persistent class is related to many objects of another persistent class. It is a relationship in which one (parent) is related...

3 minutes read.

Cascade in Hibernate

Cascading is a feature in Hibernate, which is used to manage the state of the mapped entity whenever the state of its relationship owner (superclass) affected. When the relationship owner (superclass) is saved/...

3 minutes read.

Hibernate Criteria Query Language (HCQL)

Hibernate Criteria Query Language (HCQL) is mainly used for searching and fetching records. It works on the filtration rules and logical conditions. The Criteria interface, Restriction class, and Order class are available in...

4 minutes read.