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 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
SessionFactoryand important components of the hibernate framework.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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
sql-queryelement or through the use of JPA annotations @NamedQuery and @NamedNativeQuery.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- How to configure hibernate.cfg.xml to work offline
- org.hibernate.AnnotationException: No identifier specified for entity Class
- org.hibernate.HibernateException: get is not valid without active transaction
- org.hibernate.HibernateException: No CurrentSessionContext configured
- Hibernate Program Not Terminating
- Access to DialectResolutionInfo cannot be null when ‘hibernate.dialect’ not set