Tutorial

Hibernate Tutorial

Published on August 3, 2022
author

Pankaj

Hibernate Tutorial

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.

Hibernate Tutorial - Common Error Fixes

  1. How to configure hibernate.cfg.xml to work offline
  2. org.hibernate.AnnotationException: No identifier specified for entity Class
  3. org.hibernate.HibernateException: get is not valid without active transaction
  4. org.hibernate.HibernateException: No CurrentSessionContext configured
  5. Hibernate Program Not Terminating
  6. Access to DialectResolutionInfo cannot be null when ‘hibernate.dialect’ not set

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar
Pankaj

author

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
July 14, 2020

Hello sir…needed some help with spring boot

- Aishwarya

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    March 2, 2019

    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…

    - Daniel James

      JournalDev
      DigitalOcean Employee
      DigitalOcean Employee badge
      March 1, 2019

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

      - ShuhaoWen

        JournalDev
        DigitalOcean Employee
        DigitalOcean Employee badge
        May 8, 2018

        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

        - Eashwari

          JournalDev
          DigitalOcean Employee
          DigitalOcean Employee badge
          February 13, 2018

          Really Helpful sir

          - Samuel

            JournalDev
            DigitalOcean Employee
            DigitalOcean Employee badge
            December 2, 2017

            good article

            - rbbkp

              JournalDev
              DigitalOcean Employee
              DigitalOcean Employee badge
              September 19, 2017

              Nice tutorial

              - Venugopal

                JournalDev
                DigitalOcean Employee
                DigitalOcean Employee badge
                June 18, 2017

                Nice

                - Annamalai T

                  JournalDev
                  DigitalOcean Employee
                  DigitalOcean Employee badge
                  April 23, 2017

                  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 .

                  - amit yadav

                    JournalDev
                    DigitalOcean Employee
                    DigitalOcean Employee badge
                    January 2, 2017

                    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

                    - خرید vps

                      Try DigitalOcean for free

                      Click below to sign up and get $200 of credit to try our products over 60 days!

                      Sign up

                      Join the Tech Talk
                      Success! Thank you! Please check your email for further details.

                      Please complete your information!

                      Become a contributor for community

                      Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

                      DigitalOcean Documentation

                      Full documentation for every DigitalOcean product.

                      Resources for startups and SMBs

                      The Wave has everything you need to know about building a business, from raising funding to marketing your product.

                      Get our newsletter

                      Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

                      New accounts only. By submitting your email you agree to our Privacy Policy

                      The developer cloud

                      Scale up as you grow — whether you're running one virtual machine or ten thousand.

                      Get started for free

                      Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

                      *This promotional offer applies to new accounts only.