JPA and Hibernate provide 3 main options to map a Collection. If it’s a Collection of other entities, you can model it as a to-many association. This is the most common mapping. But you can also map it as an @ElementCollection or as a basic type. In this article, I will show you all 3

Read More

When writing tests that rely on a database, you are facing 2 challenges: You need to write meaningful tests that ensure that your application works correctly. You need to provide a test database for each test run. I can’t help you with the 1st challenge. You know your application a lot better than I do,

Read More

Spring Data JPA supports various ways to retrieve data from your database. Some are very easy to use, like derived queries. Others provide great flexibility and enable you to read and transform the data in various ways, e.g., custom queries. Native queries are the extreme of the 2nd category. They are your most flexible and

Read More

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