Introduction In this article, I’m going to show you the best way to clean up test data when using Spring and Hibernate. While it’s very common to use the @DataJpaTest annotation when implementing integration tests, in this blog post, you will see why you are better off avoiding using this annotation. Why I don’t use @DataJpaTest As explained by the…
#spring
28 posts
11 Nov 2025
14 Nov 2024
Introduction In this article, we are going to analyze how the JTA transaction type works. Since this is the default transaction type when using Jakarta EE or Java EE applications, it’s very important to understand how JTA transactions work, especially since Spring Boot or Spring applications use RESOURCE_LOCAL transactions instead. JTA Transaction Type When using the JTA transaction type, you…
25 Oct 2024
Introduction In this article, we are going to see how we can use the LazyConnectionDataSourceProxy with Spring Data JPA to acquire the database connection as late as possible and, therefore, reduce transaction response time. For an introduction to how Spring transactions manage database connections, check out this article as well. Service Layer Connection Management Let’s consider we have the following…
14 Oct 2024
Introduction In this article, we are going to analyze how the RESOURCE_LOCAL JPA transaction type works. Since this is the default transaction type when using Spring Boot or Spring Data JPA, it’s very important to understand how transactions are managed when using the RESOURCE_LOCAL mode. JPA Transaction Types When the JPA 1.0 specification was released, there were two transaction types…
26 Sept 2024
Introduction In this article, we are going to see how we can generate Keyset Pagination queries with the Spring Data WindowIterator utility. This is an alternative to the Blaze Persistence solution I documented in this article. Domain Model Considering we have the following PostComment entity that has both a createdOn and a monotonically increasing id: As I explained in this…
19 Sept 2024
Introduction In this article, we are going to investigate the Spring Data Envers project and see how to get the best out of it. Hibernate Envers is a Hibernate ORM extension that allows us to track entity changes with almost no changes required on the application part. Just like Envers plugs into Hibernate ORM in order to build an audit…
19 Aug 2024
Introduction In this article, we are going to see how we can integrate Jakarta Data with Spring and Hibernate. Jakarta Data is a new Jakarta EE specification that provides a common API for building data Repositories and data access objects. If you are familiar with Spring Data JPA, you will see that Jakarta Data is very much inspired by this…
24 Feb 2024
Introduction In this article, we are going to see how we can use Java Records with Spring Data JPA Repositories. As I already explained, Java Records cannot be used as JPA entities since the Records are immutable, and JPA requires the entity class to have a default constructor and be modifiable, as that’s how the entity properties are populated when…
7 Dec 2023
Introduction In this article, we are going to see how Spring load-time weaving works so that you can apply the Hibernate bytecode enhancement mechanism at runtime. Traditionally, the bytecode enhancement mechanism is applied when the project is built using a Maven or Gradle plugin. For more details about the build-time approach, check out this article. Domain Model Let’s consider we…
15 Nov 2023
Introduction In this article, we are going to see how we can cascade the DELETE operation for unidirectional associations with Spring Data JPA and Hibernate events. Using Hibernate events to achieve this goal is an alternative to the bulk DELETE statement strategy, as it allows us to cascade the delete operation from a parent entity to its children when we…
9 Nov 2023
Introduction In this article, we are going to see how we can implement a table partitioning solution when using Spring and Hibernate. The goal of table partitioning is to split a large table into multiple smaller partition tables so that the associated table and index records can fit into the in-memory Buffer Pool, therefore allowing a more efficient seek or…
26 Oct 2023
Introduction In this article, we are going to see how to cascade DELETE the unidirectional associations with Spring Data JPA when we cannot rely on the CascadeType mechanism that propagates state transitions from parent to child entities. Domain Model Let’s consider we have the following entities in our system: The Post entity is the root of this entity hierarchy, and…
18 Oct 2023
Introduction In this article, we are going to see how we can batch INSERT statements when using MySQL and Hibernate. While Hibernate has long supported automated JDBC batch inserts, this feature doesn’t work when using the IDENTITY identifier generator strategy. Unfortunately, MySQL doesn’t support SEQUENCE objects, so using IDENTITY is the only reasonable option. Therefore, I’m going to show you…
11 Oct 2023
Introduction In this article, we are going to see what is the best way to use Spring Data JPA Stream query methods. When having to fetch a larger result set, the advantage of using a Java Stream is that the query result set could be fetched progressively instead of getting all the data at once. JPA Stream methods As I…
13 Sept 2023
Introduction In this article, we are going to see how we can fetch multiple JPA entity collections without generating an implicit Cartesian Product with the MULTISET strategy offered by the Blaze Persistence open-source project. The MULTISET fetch strategy is inspired by the MULTISET operator offered by jOOQ. If you are not familiar with the MULTISET operator, then check out this…
27 Jun 2023
Introduction In this article, we are going to see how the Spring Data JPA Query By Example (QBE) feature works, when you should use it, and what limitations it has. While Spring Data JPA already provides a wide range of options to query data: query methods or the @Query annotation Spring Data JPA Specification custom Repository query methods The Spring…
7 Jun 2023
Introduction In this article, we are going to see what is the best way to validate the DDL schema and the JPA entity mappings when using Spring and Hibernate. I decided to write this article after reading this Tweet: The hbm2ddl validate strategy As I explained in this article, Hibernate provides a SchemaManagementTool that we can use to manage or…
29 Apr 2023
Introduction In this article, we are going to see how Spring handles transaction and database connection management. I decided to write this article because this topic has been requested recurringly by the students taking my High-Performance Java Persistence training. Spring TransactionInterceptor In order to provide declarative transaction management, Spring uses AOP (Aspect-Oriented Programming), and methods annotated with the @Transactional annotations…
28 Mar 2023
Introduction In this article, we are going to see what is the best way to use the Spring Data JPA Specification when combining multiple predicates with the result set ordering logic. While you can also use query methods or the @Query annotation to define your Spring Data queries, the Spring Data JPA Specification allows you to compose dynamically various filtering…
22 Mar 2023
Introduction In this article, we are going to see how we can use the JOIN FETCH clause when fetching a child collection eagerly while also limiting the number of parent records using pagination in a Spring Data JPA application. I decided to write this article because the most common solution used in many projects turns out to be extremely inefficient.…
14 Mar 2023
Introduction In this article, we are going to see how Spring Data query methods are built, when you should use them, and especially when you should avoid them. I decided to write this article after answering this StackOverflow question, which depicts an 87-character-long Spring Data query method. TL;DR, Don’t write query methods that cannot even fit on the screen. Spring…
8 Feb 2023
Introduction In this article, we are going to see how we can achieve fault tolerance in your Spring Data application with the help of YugabyteDB. As previously explained, YugabyteDB is an open-source distributed SQL database that combines the benefits of traditional relational databases with the advantages of globally-distributed auto-sharded database systems. Fault tolerance First, let’s start with the definition of…
24 Jan 2023
Introduction In this article, we are going to see that scaling the data access layer of a Spring application can be done very easily with a YugabyteDB cluster. As I explained in this article, YugabyteDB is an open-source distributed SQL database that offers all the benefits of a typical relational database (e.g., SQL, strong consistency, ACID transactions) with the advantages…
17 Jan 2023
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…
27 Dec 2022
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. Hibernate Types The Hibernate Types project was first created in September 2017 with the goal of offering various Hibernate Types that were not supported natively, like support for JSON or ARRAY column types. Over the…
21 Dec 2022
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. MDC (Mapped Diagnostic Context)…
15 Dec 2022
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…
16 Dec 2021
Starting from Spring Boot 2.5, there’s a handy new callback that you can implement, called DefaultConfigurationCustomizer, where the word DefaultConfiguration corresponds to jOOQ’s DefaultConfiguration. You can simply create a class like this in your project: The above callback receives the DefaultConfiguration at its initialisation stage, during which you can still safely mutate it to change … Continue reading How to…