---
title: Hibernate Performance Optimization (Part – 2)
description: Hibernate Performance Optimization (Part – 2)
---

[![e-Zest logo\_An accion lab company\_Colour](https://blog.e-zest.com/hubfs/images/Logos%20with%20Tagline/Corporate/SVG/e-Zest%20logo_An%20accion%20lab%20company_Colour.svg "e-Zest logo_An accion lab company_Colour")](https://www.e-zest.com/)

- [WORK](https://www.e-zest.com/our-work)
- [SERVICES](https://www.e-zest.com/our-services) 
    - [Product Engineering](https://www.e-zest.com/digital-product-engineering-services)
    - [Enterprise Data Services](https://www.e-zest.com/enterprise-data-services)
    - [Digital Commerce](https://www.e-zest.com/digital-commerce-services)
    - [Digital Operations](https://www.e-zest.com/digital-operations)
    - [Digital Consulting](https://www.e-zest.com/digital-transformation-consulting-services)
    - [Digital Experience Design](https://www.e-zest.com/digital-experience-design)
- [INSIGHTS](https://blog.e-zest.com/)
- [CAREERS](https://www.e-zest.com/careers)
- [ENTERPRISE DATA SERVICES](https://www.e-zest.com/enterprise-data-services)
- [GENERATIVE AI](https://www.e-zest.com/generative-ai)
- [Healthcare](https://healthcare.e-zest.com)
- [Contact Us](https://www.e-zest.com/contact-us)

- ![Elevating Product Engineering with AI and ML](https://blog.e-zest.com/hubfs/Elevating%20Product%20Engineering%20with%20AI%20and%20ML.jpg)

  [BLOGS](https://blog.e-zest.com)
  
  #### Elevating Product Engineering with AI and ML

  As technology evolves and customer choices broaden, there is an increasing ...

  [Read more](https://blog.e-zest.com/elevating-product-engineering-with-ai-and-ml)
- ![e-Zest – An Accion Labs Company gets Recognition as a Contender in the ISG Provider Lens™ Study on Digital Engineering Services 2024](https://blog.e-zest.com/hubfs/e-Zest%20%E2%80%93%20An%20Accion%20Labs%20Company%20-%20Gets%20Recognition%20as%20a%20Contender%20in%20the%20ISG%20Provider%20LensTM%20Study%20on%20Digital%20Engineering%20Services%202024.jpg)

  [NEWS](https://blog.e-zest.com/news)
  
  #### e-Zest – An Accion Labs Company gets Recognition as a Contender in the ISG Provider Lens™ Study on Digital Engineering Services 2024

  We are delighted to announce that e-Zest – An Accion Labs Company has been ...

  [Read more](https://blog.e-zest.com/news/e-zest-an-accion-labs-company-gets-recognition-as-a-contender-in-the-isg-provider-lens-study-on-digital-engineering-services-2024)
- ![e-Zest Solutions supports Digital Pune Mission](https://blog.e-zest.com/hubfs/blog-files/Digital%20Literacy%20Bus%20e-Zest.png)

  [CSR](https://blog.e-zest.com/csr)
  
  #### e-Zest Solutions supports Digital Pune Mission

  **Pune, India:** e-Zest Solutions, a leading digital technology innovation ...

  [Read more](https://blog.e-zest.com/csr/e-zest-solutions-supports-digital-pune-mission)

- [Work](https://www.e-zest.com/our-work)
- [Services](https://www.e-zest.com/our-services)
- [Insights](https://blog.e-zest.com)
- [Careers](https://www.e-zest.com/careers)
- [Enterprise Data](https://www.e-zest.com/enterprise-data-services)
- [Generative AI](https://www.e-zest.com/generative-ai)
- [Innovations](https://www.e-zest.com/innovations)
- [Healthcare](https://healthcare.e-zest.com)
- [About](https://www.e-zest.com/about-us)
- [Contact](https://www.e-zest.com/contact-us)

- [Blog](https://blog.e-zest.com/)
- [News](https://blog.e-zest.com/news)
- [CSR](https://blog.e-zest.com/csr)

- [![Linkedin Icon](https://blog.e-zest.com/hubfs/icons/linkedin_icon_white.svg)](https://www.linkedin.com/company/e-zest-solutions/)
- [![Twitter Icon](https://blog.e-zest.com/hubfs/twitter-logo-silhouette%20(5).svg)](https://twitter.com/ezest)
- [![Facebook Icon](https://blog.e-zest.com/hubfs/facebook-app-logo%20(4).svg)](https://www.facebook.com/61259256638)
- [![Instagram Icon](https://blog.e-zest.com/hubfs/instagram-logo%20(4).svg)](https://www.instagram.com/ezestsolutions/)
- [![Youtube Icon](https://blog.e-zest.com/hubfs/icons/youtube_icon_white.svg)](https://www.youtube.com/user/ezestcorporate)

[![Go to top icon](https://blog.e-zest.com/hubfs/AA%20Assets/top%20button.svg)](https://blog.e-zest.com/hibernate-performance-optimization-part-2/#top)

Hit enter to search or ESC to close

Insights

e-Zest members share technology ideas to foster digital transformation.

- [Blog](https://blog.e-zest.com)
- [News](https://blog.e-zest.com/news)
- [Podcasts](https://www.e-zest.com/podcasts)
- [Healthcare](https://blog.e-zest.com/healthcare)
- [CSR](https://blog.e-zest.com/csr)
- [Whitepaper](https://blog.e-zest.com/whitepaper)
- [Playbook](https://blog.e-zest.com/playbook)
- [Media Kit](https://www.e-zest.com/media-kit)

# Hibernate Performance Optimization (Part – 2)

[Neetesh Kadam](https://blog.e-zest.com/author/neetesh-kadam)  Aug 22, 2012

[hibernate](https://blog.e-zest.com/topic/hibernate) [Hibernate Performance Optimization](https://blog.e-zest.com/topic/hibernate-performance-optimization) [Tuning Hibernate](https://blog.e-zest.com/topic/tuning-hibernate) [Technology](https://blog.e-zest.com/topic/technology)

- <https://www.linkedin.com/shareArticle?mini=true&url=https://blog.e-zest.com/hibernate-performance-optimization-part-2/&title=&summary=&source=>
- <https://twitter.com/intent/tweet?source=https://blog.e-zest.com/hibernate-performance-optimization-part-2/&text=Hibernate%20Performance%20Optimization%20(Part%20–%202)>
- <https://www.facebook.com/sharer/sharer.php?u=https://blog.e-zest.com/hibernate-performance-optimization-part-2/>
- <https://www.instagram.com/ezestsolutions/?u=https://blog.e-zest.com/hibernate-performance-optimization-part-2/>

Hibernate collection can be eager or lazy fetched which decides whether to initialize association immediately or to defer its initialization till the collection is accessed. Hibernate provides different options (fetching strategies) which decides the queries which hibernate generates to load associated collection.

Hibernate by default uses "select" fetching strategy. That means when you iterate through the collection one by one in persistent context then as soon as you access non-identifier field; hibernate will execute select query to load that item in collection. So in this way if there are 100 items in collection then for each item hibernate will fire 100 select queries to load associations. This may be useful for some use cases but many times hitting database 100 times for loading single collection is not desirable. Our aim is to reduce database hits.

Hibernate provides different options to reduce these database hits. Consider the example of Department entity which has collection of employee objects.

1. **Select Fetch:**

So consider the example of Department where we have set of employees with fetchMode Select.

```

@OneToMany(fetch=FetchType.LAZY)
@Fetch(FetchMode.SELECT)
private Set&lt;Employee&gt; employees;
```

If there are 10 departments and 10 employees in each department then with this fetch mode hibernate will generate 11 select statements. Check the following code

```

DetachedCriteria criteria = DetachedCriteria.forClass(Department.class);
criteria.add(Expression.like("name", "Dept%"));
criteria.setResultTransformer(DetachedCriteria.DISTINCT_ROOT_ENTITY);
List&lt;Department&gt; depts = getHibernateTemplate().findByCriteria(criteria);
```

After completion of last line hibernate will load departments with name like Dept\*. Since we have employee collection which is lazy, hibernate will just instantiate proxy for this and it will not generate any select to load employee collection. The select statement generated by hibernate will look something like

```

select * from Department where deptName like "Dept%"
```

and now if you iterate through employee set and access non-identifier property, like following code.

```

for(Department department: depts)
{
department.getEmployees().iterator().next().getName();
}
```

then following queries will be generated

```

select * from Employee emp inner join Department dept on emp.deptId = dept.id where dept.id = 1;
select * from Employee emp inner join Department dept on emp.deptId = dept.id where dept.id = 2;
select * from Employee emp inner join Department dept on emp.deptId = dept.id where dept.id = 3;
so on....
```

That means if there are 100 departments then 100 select will be generated with the execution of above code.

2. **Batch Fetch:**

```

@OneToMany(fetch=FetchType.LAZY)
@BatchSize(size=2)
private Set&lt;Employee&gt; employees;
```

Mentioning batch size will cause hibernate to load entities in batch; so here queries will be generated something like this

```
select * from Department where deptName like "Dept%"
```

```

select * from Employee emp inner join Department dept on emp.deptId = dept.id where dept.id in (1,2)
select * from Employee emp inner join Department dept on emp.deptId = dept.id where dept.id in (3,4)
so on....
```

This batch fetching is used to optimize runtime memory allocation, so that entire collection doesn't gets loaded at once. So by mentioning batch size to 2, number of select gets reduced to 50 (assuming we have 100 departments.)

3. **Subselect Fetch:**

```

@OneToMany(fetch=FetchType.LAZY)
@Fetch(FetchMode.SUBSELECT)
private Set<Employee> employees;
```

With this fetch mode hibernate will generate 2 select statements.

```
select * from Department where deptName like "Dept%"
```

```
select * from Employee emp inner join Department dept on emp.deptId = dept.id where dept.id in (select id from Department where deptName like "Dept%")
```

4. **Join Fetch:**

```

@OneToMany(fetch=FetchType.LAZY)
@Fetch(FetchMode.JOIN)
private Set&lt;Employee&gt; employees;
```

This is as good as eager fetch. Here hibernate uses JOIN to fetch data from department and employee tables. Query generated will be something like this

```

select * from Department dept inner join department_employee dept_emp on dept.id=dept_emp.id inner join employee emp on dept_emp.id= emp.id  where deptName like "Dept%"
```

Instead of fetch type lazy if eager fetch is used, then base on fetch mode hibernate will generate select statement without waiting for the code to access collection.

**Conclusion:**

Selecting fetch mode wisely will help you to reduce number of database hits. Each fetch strategy has its own advantages and disadvantages. ‘Select’ is a default fetch strategy, you need to explicitly mention fetch strategy to improve performance.

- <https://twitter.com/intent/tweet?source=https://blog.e-zest.com/hibernate-performance-optimization-part-2/&text=Hibernate%20Performance%20Optimization%20(Part%20–%202)>
- <https://www.facebook.com/sharer/sharer.php?u=https://blog.e-zest.com/hibernate-performance-optimization-part-2/>
- <https://www.linkedin.com/shareArticle?mini=true&url=https://blog.e-zest.com/hibernate-performance-optimization-part-2/&title=&summary=&source=>

### Similar Blog

[Using Composite Id in Hibernate 4.2.1.Final **Using composite Id with auto-generated values** By Madhura Oak Jun 25, 2013](https://blog.e-zest.com/using-composite-id-in-hibernate-4-2-1-final/)

[![Configuring Unicode support in Spring + Hibernate + MySQL + JSP Application](https://blog.e-zest.com/hs-fs/file-3395200359-jpg/blog-files/utf-8.jpg) Configuring Unicode support in Spring + Hibernate + MySQL + JSP Application In this blog I will show you how I have configured Unicode in Spring Hibernate and MySQL project where we are using JSP for the ... By Neetesh Kadam Oct 08, 2012](https://blog.e-zest.com/configuring-unicode-support-in-spring-hibernate-mysql-jsp-application/)

[Hibernate Performance Optimization (Part - 1) To work effectively with hibernate you need to understand how hibernate executes queries. You can get the work done by using ... By Neetesh Kadam Jul 31, 2012](https://blog.e-zest.com/hibernate-performance-optimization-part-1/)

[Monitoring database connection pooling **Java Management Extensions (JMX):** By Nitin Umranikar Jun 24, 2008](https://blog.e-zest.com/monitoring-database-connection-pooling/)

[![Configuring Unicode support in Spring + Hibernate + MySQL + JSP Application](https://blog.e-zest.com/hs-fs/file-3395200359-jpg/blog-files/utf-8.jpg) Configuring Unicode support in Spring + Hibernate + MySQL + JSP Application In this blog I will show you how I have configured Unicode in Spring Hibernate and MySQL project where we are using JSP for the ... By Neetesh Kadam Oct 08, 2012](https://blog.e-zest.com/configuring-unicode-support-in-spring-hibernate-mysql-jsp-application/)

[Hibernate Performance Optimization (Part - 1) To work effectively with hibernate you need to understand how hibernate executes queries. You can get the work done by using ... By Neetesh Kadam Jul 31, 2012](https://blog.e-zest.com/hibernate-performance-optimization-part-1/)

[![About Apache Solr](https://blog.e-zest.com/hs-fs/file-3395197959-jpg/blog-files/solr-indexing80.jpg) About Apache Solr **Introduction:** By Neetesh Kadam Jul 02, 2012](https://blog.e-zest.com/about-apache-solr/)

## Helping Organizations Thrive in the Digital Age

[Contact Us](https://www.e-zest.com/contact-us)

- [About](https://www.e-zest.com/about-us)
- [Services](https://www.e-zest.com/our-services)
- [Work](https://www.e-zest.com/our-work)
- [Insights](https://blog.e-zest.com/)
- [Healthcare](https://healthcare.e-zest.com/)
- [Accion Labs](https://www.accionlabs.com/)

- <https://www.linkedin.com/company/e-zest-solutions/>
- <https://twitter.com/ezest>
- <https://www.facebook.com/eZest/>
- <https://www.instagram.com/ezestsolutions/>
- <https://www.youtube.com/user/ezestcorporate>

- Dallas
- Detroit
- Hannover
- Vienna
- London
- Pune

 © 2025 e-Zest Solutions

- [Privacy Policy](https://www.e-zest.com/privacypolicy)
- [Cookie Policy](https://www.e-zest.com/cookie-policy)

 Contact us

 Contact us

e-Zest is a leading digital innovation partner for enterprises and technology companies that utilizes emerging technologies for creating engaging customers experiences. Being a customer-focused and technology-driven company, it always helps clients in crafting holistic business value for their software development efforts. It offers software development and consulting services for cloud computing, enterprise mobility, big data and analytics, user experience and digital commerce.