Hibernate Best Practices Tutorial (original) (raw)

Hibernate is the popular object relation mapping implementation. This feature makes it special among the developers and in this tutorial we will see the best practices to create better Hibernate applications.

1. Hibernate Introduction

Hibernate Best Practices - Hibernate Overview

Fig. 1: Hibernate Overview

1.1 Hibernate Architecture

There are 4 layers in the Hibernate architecture i.e. Java Application Layer, Hibernate Framework Layer, Backend API Layer, and the Database Layer. Let’s understand the diagram of Hibernate architecture.

Hibernate Best Practices - Architectural Diagram

Fig. 2: Hibernate Architectural Diagram

For creating the first Hibernate application, we must know the elements of the Hibernate architecture. They are as follows:

Element Description
SessionFactory The SessionFactory is a factory of session and client of Connection Provider. It holds second level cache (optional) of data.
Session The session object provides an interface between the application and data stored in the database. It is a short-lived object and wraps the JDBC connection. It is a factory of Transaction, Query and Criteria and holds the first-level cache of data. The Session interface provides methods to INSERT, UPDATE, and DELETE the objects.
Transaction The transaction object specifies the atomic unit of work and is an optional parameter in the Hibernate framework.
ConnectionProvider It is a factory of JDBC connections and abstracts the application from DriverManager or DataSource. It is an optional parameter in the Hibernate framework.
TransactionFactory It is a factory of Transaction and is again an optional parameter in the Hibernate framework.

1.2 Hibernate Benefits

There are many advantages of using the Hibernate framework, e.g.

Let us explore the different Hibernate strategies that can be adopted to improve the performance of an application.

2.1 Use of Model classes

While writing SQL Select query, developers can choose the columns they need for implementation. JPA and Hibernate support specific columns than just entities. There are of 3 types and each has its own usage.

2.1.1 Entity

An entity is the most common implementation. Developers can use it if they need all entity attributes or to perform the SQL Update or Delete operations that affect a minimal entity number.

1 em.find(Person.class, 4);

2.1.2 Model class a.k.a POJO

The POJO is similar to Entity class but it represents a specific record in the database.

1 List list= em.createQuery(“SELECT new Bookdetails(book.isbn, book.author) FROM Bookdetails book”, Bookdetails.class).getResultList();

2.2 Using the Query

ORM frameworks offer multiple options to create an SQL query that matches their requirement. Let us understand them one by one.

2.2.1 find() method

This method is the easiest implements to find a record from the database by its primary key. This method not only provides security and performance benefits. It is also:

1 em.find(Person.class, 5);

2.2.2 Java Persistence Query Language (JPQL)

The Java Persistence Query Language (JPQL) is similar to SQL queries but it executes on entity class and their relations but not directly on database tables. This approach offers low and moderate complexity.

1 TypedQuery tq = em.createQuery(“SELECT book FROM Book book JOIN book.author WHERE book.title = :title”, Book.class);

2.2.3 Criteria API

The Hibernate’s Criteria API generates dynamic queries at runtime. Developers can use this if the query structure depends on user input. Let us understand this with the help of an example.

01020304050607080910 CriteriaBuilder cb = em.getCriteriaBuilder();CriteriaQuery q = cb.createQuery(Book.class);Root book = q.from(Book.class);q.select(book); if (!input.getTitle().isEmpty()) { SetJoin join= book.join(Book.class); q.where(cb.equal(join.get(Booktitle.class), input.getTitle()));}</book,>

2.2.4 Native SQL Queries

Native Queries provide developers the way to write and execute the database statements. This is the best way to write complex queries in the Hibernate framework.

1 Myclass e = (Myclass) em.createNativeQuery(“SELECT * FROM myClass e WHERE e.name =“abc“, Myclass.class).getSingleResult();

2.3 Use Bind Parameters

Using parameter bindings for the query offers several advantages over the regular SQL query strings.

These are represented by a number starting with 1 and prefixed with ?.

1 Query q = em.createNativeQuery(“SELECT c.firstname, c.lastname FROM Employee c WHERE c.id = ?”);q.setParameter(1, 1);

2.4 Do not use Eager Loading

Eager loading the records from the database is another reason that affects Hibernate performance.

12 @ManyToMany(mappedBy = “authors”, fetch = FetchType.EAGER)private Set books = new HashSet();

The framework fetches the related entities from the database based on the relationship and the defined fetch mode. This result in confusion as hibernate fetches the related entities data which may be required from the given test case. To overcome this issue developers should use the fetching mode as FetchType.LAZY.

2.5 JDBC Batching

Jdbc allows batching the multiple SQL statements and sending them to the database in a single request. This approach saves multiple trips for all the SQL operations and reduces the response time.

2.6 Automatic Primary Key Generator

Hibernate use the existing database features to auto-generate the unique id identifier otherwise called as Primary key column values. The following code snippet will help us understand the use of @Id annotation.

12 @Id@GeneratedValue@Column(name = “id”, updatable = false, nullable = false)private Long id;

3. Summary

Here are some points that can help us while using the Hibernate Framework:

That’s all for this post. Happy Learning!!

4. Conclusion

Following is an extract of good practices in Hibernate. Developers can use these tips to implement in their application and offer better performance. I hope this article served you whatever you were looking for.

Photo of Yatin

An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).