The awesome BaseJpaRepository from Hypersistence Utils

Introduction In this article, I’m going to explain how you can use the BaseJpaRepository from the Hypersistence Utils OSS project as an alternative to the default Spring Data JpaRepository. The reason why I’m not using the default JpaRepository on any of my Spring projects is that it provides some terrible defaults that can be very easily misused, like: The findAll Anti-Pattern The JPA save Anti-Pattern So, there are two ways you can fix the problems induced by the default JpaRepository: You can use the HibernateRepository to deprecate the questionable methods that you… Read More

From Hibernate Types to Hypersistence Utils

Introduction In this article, I explain why the Hibernate Types became Hypersistence Utils, and how you can migrate your old dependency to Hypersistence Utils 3.0.

Spring MDC Transaction Logging

Introduction In this article, we are going to see how to configure a Spring application to use MDC (Mapped Diagnostic Context) for transaction logging. This technique is going to help us inject the Persistence Context information as well as the associated database transaction identifier in all the log entries emitted from within a @Transactional service method.

Spring Data Hibernate Entity Listeners

Introduction In this article, we are going to see how we can configure Spring Data to register several Hibernate Entity Listeners that can intercept entity state modifications. As I explained in this article, JPA also offers an event listening mechanism that you can configure via the @EntityListeners, @PostPersist or @PostUpdate, or PostRemove annotations. However, the JPA solution is way too limited as it doesn’t allow you to interfere with the EntityManager directly. Therefore, I prefer using the Hibernate Entity Listeners mechanism, which you can easily integrate with Spring Data, as this article… Read More

The best way to do the Spring 6 migration

Introduction In this article, we are going to see how you can migrate an existing application to Spring 6 and how to get the most out of this upgrade. The tips in this article are based on the work I’ve done to add support for Spring 6 in Hypersistence Optimizer and the High-Performance Java Persistence project.

The Spring Data JPA findById Anti-Pattern

Introduction In this article, we are going to see how the Spring Data JPA findById method can become an Anti-Pattern when using it to reference parent entity associations.

Spring Data JPA entity locking

Introduction In this article, we are going to see what options Spring Data JPA offers for entity locking. We will see how we can apply a shared or exclusive row-level lock upon fetching one or multiple entities.

How to write a custom Spring Data Base Repository

Introduction In this article, we are going to see how we can write a custom Spring Data base Repository that you could use instead of the default ones, like the overly common JpaRepository. Now, why would you even want to do that? Well, most of the default repositories extend the CrudRepository, which provides some very questionable defaults, like findAll or deleteAll that shouldn’t be really inherited by every single data access Repository instance.

The Spring Data findAll Anti-Pattern

Introduction In this article, we are going to see why inheriting the default Spring Data findAll method in every data access Repository is a terrible Anti-Pattern.

The best way to fetch a Spring Data JPA DTO Projection

Introduction In this article, I’m going to show you what is the best way to fetch a Spring Data JPA DTO Projection. Apart from the basic use cases, we’re going to see how to fetch even hierarchical DTO structures using Spring Data JPA.