Hibernate Tutorial

Filed Under: Hibernate

Recently I have written a lot of hibernate tutorial. Hibernate is one of the best Java ORM tool in the current market. So this post is like an index of all the posts for hibernate tutorials and examples. You can go through these hibernate tutorials in sequence to learn hibernate from scratch. Most probably I will be adding more hibernate tutorials to the list, so you might want to bookmark it and check it once in a while.

Hibernate Tutorial

Hibernate Tutorial

  1. Hibernate Tutorial for Beginners

    Hibernate supports JPA annotations and it’s very flexible. We can configure it using XML, property files as well as programmatically. This tutorial is a great way to get you started with hibernate framework. This tutorial focuses on different configurations required for hibernate and provide examples of simple XML based mapping as well as JPA annotations based mapping. You will also learn different ways to initialize SessionFactory and important components of the hibernate framework.

  2. Hibernate One-to-One Mapping

    Most of the times, database tables are associated with each other. There are many forms of association – one-to-one, one-to-many and many-to-many are at the broad level, that can be further divided into unidirectional and bidirectional mappings. This tutorial guides you through implementing Hibernate One to One Mapping using XML configuration as well as using JPA annotations configuration.

  3. Hibernate One-to-Many Mapping

    In simple terms, one to many mapping means that one row in a table can be mapped to multiple rows in another table. For example, think of a Cart system where we have another table for Items. A cart can have multiple items, so here we have one to many mapping. In this tutorial you will learn how to implement One to Many Mapping using XML based configuration and then using Hibernate Annotations.

  4. Hibernate Many-to-Many Mapping

    Many-to-Many mapping is usually implemented in database using a Join Table, for example we can have Cart and Item table and Cart_Items table for many-to-many mapping. Every cart can have multiple items and every item can be part of multiple carts, so we have a many to many mapping here. This tutorial explains about hibernate many-to-many unidirectional as well as bidirectional mapping.

  5. Hibernate Query Language (HQL)

    Hibernate Framework comes with a powerful object-oriented query language – Hibernate Query Language (HQL). It’s very similar to SQL except that we use Objects instead of table names, that makes it more close to object oriented programming. This article explains about HQL and different clauses supported by HQL language.

  6. Hibernate Native SQL Query

    Hibernate provide option to execute native SQL queries through the use of SQLQuery object. This is very handy when we want to execute database specific queries that are not supported by Hibernate API such as query hints or the CONNECT keyword in Oracle Database.

  7. Hibernate Named Query

    If there are a lot of HQL or Native SQL Queries, then they will cause a code mess because all the queries will be scattered throughout the project. That’s why Hibernate provides Named Query that we can define at a central location and use them anywhere in the code. We can created named queries for both HQL and Native SQL.

    Hibernate Named Queries can be defined in Hibernate mapping files using query or sql-query element or through the use of JPA annotations @NamedQuery and @NamedNativeQuery.

  8. Hibernate Criteria

    Hibernate provides Criteria API that is more object oriented for querying the database and getting results. We can’t use Criteria to run update or delete queries or any DDL statements. It’s only used to fetch the results from the database using more object oriented approach.

    Some of the common usage of Criteria API are Projection that we can use for aggregate functions such as sum(), min(), max() etc, ProjectionList to fetch selected columns only, ordering the results etc.

  9. Hibernate First Level Cache

    Hibernate Cache can be very useful in gaining fast application performance if used correctly. The idea behind cache is to reduce the number of database queries, hence reducing the throughput time of the application.
    Hibernate first level cache is associated with the Session object. Hibernate first level cache is enabled by default and there is no way to disable it. However hibernate provides methods through which we can delete selected objects from the cache or clear the cache completely. Any object cached in a session will not be visible to other sessions and when the session is closed, all the cached objects will also be lost.

  10. Hibernate Second Level Cache with EHCache

    Hibernate Second Level cache providers include EHCache and Infinispan, but EHCache is more popular because it’s easy to integrate and supports all the hibernate caching strategies. This tutorial provides a complete example to integrate EHCache with Hibernate framework.

  11. Hibernate get vs load

    Hibernate Session provide different methods to fetch data from database. Two of them are – get() and load(). There are also a lot of overloaded methods for these, that we can use in different circumstances.

    At first look both get() and load() seems similar because both of them fetch the data from database. However there are few differences between them, this tutorial explains about them with example code.

  12. Hibernate save vs persist

    Hibernate Session is the interface between java application and hibernate framework. This tutorial explains about Session important methods for saving and updating data in tables – save, saveOrUpdate, persist, update and merge.

  13. Hibernate openSession vs getCurrentSession

    Hibernate SessionFactory is the factory class through which we get sessions and perform database operations. Hibernate SessionFactory provides three methods through which we can get Session object – getCurrentSession(), openSession() and openStatelessSession(). This tutorial explains about each one of them with example code.

  14. Hibernate log4j integration

    Hibernate 4 uses JBoss logging but log4j is the most popular logging framework. A quick tutorial explaining how to integrate log4j logging with hibernate framework.

  15. Hibernate Tomcat JNDI DataSource Example

    Most of the times hibernate framework is used in web applications running in Tomcat or any other servlet container. Hibernate can use the DataSource defined as JNDI resource in the container, this is the preferred approach to let servlet container handle the database connections using connection pooling.

  16. Spring Hibernate Integration

    Spring is one of the most used Java EE Framework and Hibernate is the most popular ORM framework. That’s why Spring Hibernate combination is used a lot in enterprise applications. This tutorial uses Spring 4 and integrate it with Hibernate 3 and then update the same project to use Hibernate 4.

  17. Spring MVC Hibernate Example

    This tutorial moves forward and explains how to integrate Hibernate with Spring MVC and use Spring declarative transaction management, rather than using hibernate transaction management.

  18. Struts2 Hibernate Integration Example

    This tutorial explains the general way to integrate Hibernate with any web application through the use of ServletContextLister, the example is using Struts2 with Hibernate but behind the scene integration is done using only Servlet technology. This is different from Spring integration because Struts2 doesn’t provide any built-in support for hibernate integration.

  19. Hibernate Validator Example

    Data validation is an integral part of any application. It’s a cross cutting task that happens at presentation layer, business layer as well as persistent layer. That’s why JSR-303 provides annotation based standard for applying validation for java bean properties. Hibernate Validator provides support for JSR-303 and this tutorial shows it’s usage with a simple example.

  20. Hibernate Tools Eclipse Plugin

    If you have worked on hibernate projects, you must be familiar with a lot of properties we need for hibernate mapping and configuration file. Without any proper tool that can guide us in looking for correct properties, it will become very hard to correctly configure our application. This is when Hibernate Tools Eclipse Plugin comes handy and a must have plugin for hibernate projects.

  21. Hibernate Interview Questions

    Having a good knowledge of Hibernate framework is a plus point for Java based interviews, that’s why I wrote this post. It contains most of the important questions related to Hibernate framework with detailed answers. You should go through these before going for interview to brush up your knowledge.

  22. Hibernate Tutorial – Common Error Fixes

  23. How to configure hibernate.cfg.xml to work offline
  24. org.hibernate.AnnotationException: No identifier specified for entity Class
  25. org.hibernate.HibernateException: get is not valid without active transaction
  26. org.hibernate.HibernateException: No CurrentSessionContext configured
  27. Hibernate Program Not Terminating
  28. Access to DialectResolutionInfo cannot be null when ‘hibernate.dialect’ not set


  1. Aishwarya says:

    Hello sir..needed some help with spring boot

  2. Daniel James says:

    I just wanted to say that I’ve gone through a number of tutorials on Hibernate and yours is the only one that actually worked, *and* is the only one that really explains some of the steps so that I can apply the information to my own projects. Thank you so much…

  3. ShuhaoWen says:

    I don’t know why the second level cache doesn’t work .

  4. Eashwari says:

    How to download hibernate tutorial , i already subscribed and downloaded your Design patterns pdf , which is very excellent and more understandable manner.

    same way i want to download Hibernate tutorial please guide me

  5. Samuel says:

    Really Helpful sir

  6. rbbkp says:

    good article

  7. Venugopal says:

    Nice tutorial

  8. Annamalai T says:


  9. amit yadav says:

    sir i want to perform only read or fetch operation in hibernate.If i perform insert,delete ,update,save i want exception on that.

    Please help me .

  10. خرید vps says:

    Wonderful post.This is nice post and gives in depth information. I like to read this post because I met so many new facts about it actually.Thanks a lot. I bookmark your blog because I found very good information on your blog, Thanks for sharing

  11. Agashi24 says:

    Can you explain about the n+1 problem in hibernate?

  12. jirari mehdi says:

    Hi everybody .

    How to map a list of Integer with Hibernate XML file. like

    List listMonth =new ArrayList();

    Best regards

  13. Pankaj Patil says:

    Hi Pankaj Sir,

    Is there any possible way to integrate hibernate with ejb? if yes please elaborate with the example.

    1. Pavan says:

      What do you mean by integrating hibernate with EJB?

      EJB is an J2EE specification and there are multiple implementations (by different vendors)

      Hibernate is one of the implementations of EJB3 specification. (Though hibernate existed as a successful ORM framework before EJB3 and has many more features which are not part of the core specification, like Criteria Queries). In fact EJB3 borrowed many of the core concepts from Hibernate.

      So if you are using Hibernate, you can either use Hibernate Framework specific Interfaces or go with EJB3 Interfaces. (e.g Entitymanager in place of Sessionfactory & Entity in place of Session). This keeps your application open to switch to a different implementation of EJB (like OpenJPA) if you would like to in future.

      1. Pankaj says:

        Thanks for the explanation Pavan.

    2. Pankaj says:

      I have not worked on EJB, I will see if something simple example I can work out for you.

  14. chellappa says:

    I am chellappa,your tutorials was nice and easy to understand. can u tell me jsf and hibernate (one to many example) integration

    1. Pankaj says:

      I am creating Hibernate Tutorial eBook PDF that will be free for my email subscribers to download. Please subscriber to get it first. Check this article:


  15. vijay says:

    you are doing good job ,nice website and easy to understand thanx bro ……..keep it up

    1. Pankaj says:

      Thanks Vijay for nice words, subscriber to get the Hibernate Tutorial eBook. It’s work in progress and will be free download for my email subscribers.

  16. Morocco says:

    thx for your efforts, I really like the way u devellop with and hope that u’ll post more article on creating a big project

    1. Pankaj says:

      Thanks, I have written a lot of integration projects. Check them out and download to explore more.

  17. sujan says:

    Hi pankaj
    i have been following your tutorial from one year its fentastic collection nice website.

    1. Pankaj says:

      Thanks Sujan, appreciating your nice words.

  18. Shahid says:

    Excellent collection of Hibernate tutorials with examples under one roof. Fantastic job Pankaj….kudos to you!

    1. Pankaj says:

      Thanks Shahid for appreciating my work. Let me know if you need any specific hibernate tutorial.

Comments are closed.

Generic selectors
Exact matches only
Search in title
Search in content