As shown in my previous post, Hibernate offers several APIs to query data from the database. If you want to define your query dynamically at runtime, you can use JPA’s Criteria API. In the past, Hibernate also offered its own proprietary Criteria API. It has been deprecated in Hibernate 5, and you should avoid it

Read More

When working with Hibernate, you can choose between multiple APIs to query data from your database. Some of them are very flexible and provide almost unlimited options to filter and transform your query results. Others are specifically designed to implement specific tasks as easily and quickly as possible. In this article, I will show you

Read More

The size of a result set has a huge impact on the performance of a query, the burden it puts on your Java application, and the user experience your UI provides. It’s a general best practice to split huge result sets into multiple parts presented on separate pages. This process is called pagination. The most

Read More

In a comment here on the blog, Mehmet asked how he could call a function before inserting or updating an entity in the database. There are several ways you can call a function as part of an SQL INSERT or UPDATE statement. But these are not your only options. To change an attribute’s value before

Read More

Almost all applications have at least a few uses cases that require complex database operations. You can’t define them using a few annotations in a Spring Data JPA repository. You need to create a composite repository. Whenever derived or custom queries are not powerful or flexible enough, you can add your own methods to your

Read More

Most databases put a lot of effort into analyzing SQL statements and finding the optimal execution plan. This often makes the difference between a slow and a blazing fast statement. But this analysis can also be rather complex. That’s why a lot of databases try to cache and reuse the created execution plans. Hibernate and

Read More

Arrays are a common data type in Java and part of the SQL-99 standard. So, it should be easy and quite common to use them with Hibernate, right? No, they aren’t. Most developers try to avoid using arrays in their persistence layers. There are several reasons for that: Not all DBMS support arrays. Because of

Read More

When you model a hierarchical data structure, you often have to use self-referencing associations. Both ends of these associations are of the same type. In other words, the entity object on which the association is defined and the one the association references are of the same type. In your database, you model that using a

Read More