The best way to fix the Hibernate MultipleBagFetchException

Introduction If you’ve been using Hibernate for some time, there is a good chance you bumped into a MultipleBagFetchException issue: org.hibernate.loader.MultipleBagFetchException: cannot simultaneously fetch multiple bags In this article, we are going to see the reason Hibernate throws the MultipleBagFetchException as well as the best way to solve this issue.

Spring Boot performance tuning

Introduction While developing a Spring Boot application is rather easy, tuning the performance of a Spring Boot application is a more challenging task, as, not only it requires you to understand how the Spring framework works behind the scenes, but you have to know what is the best way to use the underlying data access framework, like Hibernate for instance. In a previous article, I showed you how easily to optimize the performance of the Petclinic demo application. However, by default, the Petclinic Spring Boot application uses the in-memory HSQLDB database, which… Read More

The best way to fetch multiple entities by id using JPA and Hibernate

Introduction In this article, we are going to see how we can load multiple entities by id at once when using JPA and Hibernate. Loading multiple entities by their identifier is a very common requirement when using JPA and Hibernate. Hence, we are going to see how we can optimize the underlying SQL query execution.

How to map SQL Server JSON columns using JPA and Hibernate

Introduction In this article, we are going to see how easy it is to map an SQL Server JSON column when using the Hibernate Types project. Not only that the hibernate-types project supports JSON column types for PostgreSQL and MySQL and Oracle, but, as you will see in this article, the JsonStringType works just fine when using Microsoft SQL Server.

How to map Oracle JSON columns using JPA and Hibernate

Introduction In this article, we are going to see how to map the Oracle JSON column storage types when using JPA and Hibernate. My hibernate-types project has long been supporting JSON for PostgreSQL and MySQL. However, while developing my new and awesome SQL Master Class training, I realized that the current implementation does not handle Oracle JSON column types properly. Luckily, the fix was easy and starting with the 2.7.0 version you can now persist and fetch JSON attributes on Oracle using JPA and Hibernate.

How to write a compact DTO projection query with JPA

Introduction In this article, we are going to see how we can write the best DTO projection JPQL query by omitting the package name when using JPA, Hibernate, and Spring. As I already explained, DTO projections are the most efficient way of fetching data when using JPA and Hibernate.

Mapping PostgreSQL Interval to Java Duration with Hibernate

Introduction In this article, we are going to see how to map the PostgreSQL interval column type to a Java Duration object using Hibernate and the hibernate-types project. Another very useful feature introduced by the hibernate-types project is that all types extending the ImmutableType can now be treated as standard org.hibernate.type.Type, therefore enabling a much better Hibernate Core API integration.

How to map multiple JPA entities to one database table with Hibernate

Introduction In this article, we are going to see how we can map multiple JPA entities to one table with Hibernate. Using multiple entities can speed up both read and write operations. This question has been a recurring theme on StackOverflow, so I decided to explain the advantages of mapping multiple JPA entities to the same database table.

How to write JPA Criteria API queries using Codota

Introduction Writing JPA Criteria API queries is not very easy. The Codota IDE plugin can guide you on how to write such queries, which greatly simplifies the process. In this article, we are going to see how Codota works and how it can help you discover a given API.

How to override the default Hibernate Session FlushMode

Introduction In this article, we are going to see how we can override the default FlushMode used by Hibernate. If you bootstrap Hibernate natively, and not as a JPA provider, de default FlushMode.AUTO is used which, as explained in this article, does not trigger a Persistence Context flush prior to executing a native SQL query.