AUTHOR
Adservio team
DATE
January 25, 2022
CATEGORY
Digital Delivery
Hibernate Performance Best Practices

Digital Delivery

12 min

Hibernate Performance Best Practices

Performance tuning is important for all application development, but it's especially important for data-driven web applications. A poorly performing web application can result in slow response times, delays in data retrieval, and frustrated users. However, Hibernate can be challenging to configure and tune. Improper usage of Hibernate features and techniques can lead to performance degradation and even application failure.

Before we dive into explaining the best practices let us remind ourselves that by using the Hibernate mode, lines of code are reduced by maintaining object-table mapping and returning results to the application in the form of Java objects.

Consequently, it frees programmers from having to manually handle persistent data, reducing development time and maintenance costs.

These are the configuration settings you need to know to debug and optimize your system's performance with Hibernate.

Java Persistence with JPA and Hibernate

The Java Persistence API (JPA) bridges the gap between relational databases and object-oriented programming. Java objects in a relational database are called entities.

These entity tables have columns and rows where they are placed, with foreign keys to define the relationships between tables- OneToOne, OneToMany, and ManyToMany.

The relational model is flat but developers can use SQL queries to retrieve data from individual tables or across multiple tables using foreign key constraints.

Object-oriented design has no limitations on the number of relationships an object may have - instead, it defines how objects relate through attributes and behavior.

Let us see few of the well known best practices among devs for securing a high performance.

Hibernate performance best practices for indexing Tables or Columns

If you're running queries against a table, the first step is to index it. These Hibernate performance tuning best practices will help improve query performance, especially if your application is fetching data from the database for display on the web.

Indexes allow you to quickly access data without having to scan through every row in the table. How do they work? Without an index, your application would have to scan through each row of content until it found the specified record.

With an index, your application could simply go straight to the correct row of content and retrieve what it needs.

The most commonly used type of index is a B-Tree Index. This form of index allows efficient retrieval by sorting records by key values with keys that are at or near the beginning of the file list (the root node).

In other words, if you wanted information on a record with a name value of George S., all you would have to do is look in the key "S." This approach is much faster than reading through every row.

Reducing selectivity on queries

Selectivity is the percentage of rows that satisfy a query. High selectivity means it's more likely for a given row to match the search criteria and will usually result in faster queries.

If you have low selectivity, then it's less likely that a given row will match the search criteria and this will usually result in slower queries.

The Hibernate performance best practice to reduce selectivity is to use SQL queries with indexes. An index allows you to find matching records without having to scan the entire table, which can drastically improve performance on some queries.

For example, if you wanted to find all of your users who were over 27 years old, you could create an index on their age column. This would allow you to quickly query for all users over 27 without having to scan through every single user record in your database.

Indexing, while helpful for performance can cause issues if not set up properly. Our team of experts has experience in database tuning.

Learn how we can help your team by assisting them on setting up indexes and get the best performance from your database.

Choose the right FetchType

Fetching too much data is the number one problem that causes performance issues when it comes to using JPA and Hibernate. This is because JPA makes it very easy to fetch more data than you really need, especially from @ManyToOne and @OneToOne associations which are fetched eagerly by default.

When using Hibernate, there's no way to set a different fetch strategy from EAGER in case you're using a JPA entity graph.

Caching

When it comes to improving performance, caching can be a great tool. By caching large tables or columns within your database, you will see an improvement in query times because the data is stored locally.

This means that when your application requests data from the cache, it doesn't have to wait for the database server to respond.

This Hibernate performance best practice is a good way to improve the performance of your application. It's also one of the easiest and most effective ways to make your application faster.

Caches store frequently accessed data and retrieve it in a much shorter time than retrieving it from a database or API.

At its simplest, caching can be done with in-memory objects stored in the java.lang.ref package. More robust caches can use dedicated memory to store data on disk (for example, memcached).

Hibernate supports two different types of cache: the 1st level and 2nd level cache.

The first level cache is a session-level one and it relies on session-level objects. This type of cache is used to minimize Db interactions by caching the state of an object, which then updates only at the end instead of every time that something changes in a transaction.

The second category consists of sessions factory caches, available across all transactions running within an application for as long as they're necessary if there are none left from other transactions or sessions yet not closed out due to timeout.

By default, this type isn't enabled in Hibernate but it does exist if we want to use it because we can do so anytime we want through configuration parameters. Factory caching is just one of many Hibernate options that aren't enabled by default.

There are so many that it can be hard to know which to enable for best results. We've got plenty of experience optimizing both relational and NoSQL databases.

Avoid modeling a ManyToMany association as a list

Defining a OneToMany association in Hibernate is relatively simple. Issues start to arise when using a java.util.List to model a ManyToMany association.

Instead, it should be modeled as an entity with one row per parent and child instead of one row for every instance of the associated objects.

For example, if you have Book instances with an Author class that has been mapped using a java.util.List collection type, any change in either side will result in all elements from both sides being updated or deleted respectively before inserting new ones.

This is highly inefficient behavior on Hibernate's part and not recommended unless there are compelling reasons that justify taking such risks.

Logging SQL statements

Logging of SQL statements in a production environment leads to poor performance. It is very inefficient if your application uses System.out for Hibernate logs with the show_sql parameter set to true inside their configuration file (eg: persistenceXML).

This optimization strategy requires changing this value from true to false. Avoiding log statements is one of those little things that make a big impact.

Unfortunately, there are many of these little things that can have a negative impact on performance. Identifying all of them is near impossible without the right expertise. Reach out to let us show you how we can find and tweak all of these items to ensure your system performance meets your objectives.

Perform bulk updates and deletes

Running update or delete statements on one entity after the other feels natural in Java, but it is also very inefficient. Each time we update or delete an entity, Hibernate creates a SQL query for that single operation.

The Hibernate performance best practice to address this is to do jdbc batching by doing these operations in bulk by creating statements that affect multiple records at once-for example via JPQL statements and CriteriaUpdate and CriteriaDelete operations.

Configure dirty checking

Dirty checking is a concept to avoid time-consuming database write actions. It does this by ensuring that all necessary updating and changes are done without affecting the other fields.

Only the changed fields of the database are updated, while the remaining unchanged ones remain untouched. This Hibernate performance best practice saves developers' time and effort in updating databases when states of objects get modified inside a transaction.

Hibernate automatically detects an object's state, updates new values for it with DB values & synchronizes them every time any change is made.

Consider implications of using XML file for mappings or annotations

When Hibernate first came out, it was necessary to define all of your mappings in the orm.xml file. You had a bunch of Java classes that represented your domain model and a long XML file with mapping information for queries.

One of the main challenges with this approach is you had to keep two files synchronized - coding and mapping info are not consolidated!

That changed when annotations were introduced into Java 5; JPA added annotations as well which allowed entity mappings, query definitions, etc., to be coded right there within the entities themselves.

Avoid unnecessary Queries – Use Query-Specific fetching

This hibernate performance best practice is to use JOIN FETCH. The FETCH keyword of the JOIN FETCH statement is JPA-specific and tells persistence providers not only to join 2 database tables but initialize associations on returned entities. It can be used in a JOIN or LEFT JOINS statements.

Use FetchType.LAZY for all of your associations to ensure that you only fetch the ones that are used in business code.

However, if one changes the FetchType then Hibernate uses a separate query to initialize each of these associations which causes another performance problem called the n+1 select issue.

The first step is to implement lazy loading is to make sure they are updated with new columns on outgoing association mappings and lazy=true flag set on association mapping properties.

The second step is to change the default property name value from LAZY_PERSISTENCE_CONCURRENTLY to LazyUpdateWithoutConcurrentInvalidation First update outgoing column mappings with new columns on SQL queries by setting updatedAt column as PRIMARY KEY.

Second fix this issue by replacing LAXTY_PERSISTENCE_CONCURRENTLY default value by either LazyUpdateWithoutConcurrentInvalidation or AllowNullInsertionOnFollowAssociation options.

Related insights on best performance practices of Hibernate 5 and Spring Boot 2.

Conclusion

If you want to improve your Hibernate performance, having an expert by your side can help you achieve the best outcome.

Adservio continues helping companies to implement Hibernate performance best practices. We have extensive experience using advanced approaches to modern development.

If you're interested in learning more about how our team of professionals can help your organization thrive, reach out to Adservio to learn more.

Industry insights you won’t delete. Delivered to your inbox weekly.

Other posts

Do you have any questions for us?