Introduction In this article, I’m going to show you how to emulate a LEFT JOIN FETCH between two root entities without fetching the intermediate child entity. I got the inspiration for this article by this comment thread on my blog. Domain Model Let’s assume we have the following entities: The Post is the root entity of our hierarchy, but just…
Vlad Mihalcea
https://vladmihalcea.com/ · 110 posts · history since 2022 · active
27 Feb
2 Dec 2025
Introduction In this article, I’m going to show you the best way to replace the deprecated @GenericGenerator when upgrading your application to Hibernate 7. Hibernate @GenericGenerator annotation The @GenericGenerator annotation has been available since Hibernate 3.5, as an alternative to the <generator> XML element from HBM mappings. As illustrated by the Hibernate 6.5 @GenericGenerator Javadoc, this annotation was deprecated in…
24 Nov 2025
Introduction My friend, Denis Magda, wrote a wonderful book called Just Use Postgres!, and I’m glad that I got the chance to read it. Audience This book is useful for any software developer who’s either using PostgreSQL or plans on using it because it covers a lot of features that are very useful for modern applications. No matter your level…
11 Nov 2025
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…
15 Oct 2025
Introduction My friend, Laurentiu Spilca, has been working on the second edition of his Troubleshooting Java book, and when I was asked to review it, I was more than happy to do it since the book features a lot of topics that I’m also very interested in, such as performance tuning and query optimization. Audience This book is useful for…
30 Sept 2025
Twelve years later! Twelve years ago today, I decided to create this blog on WordPress.com to share whatever I was finding interesting while working as a software architect. Prior to starting this blog, I didn’t have any writing experience, which you can clearly see in the first article I’ve ever published. However, practice makes perfect, and in time, I managed…
12 May 2025
Introduction In this article, I’m going to show you how to optimize the queries executed by a Spring application running on MySQL using Releem. For a short introduction about Releem, check out this previous article, in which I explained how to set up Releem to collect metrics from your MySQL database. Spring Petclinic application The application under test is going…
11 Apr 2025
Introduction In this article, I’m going to review the Foundations of AI and Machine Learning for Java Developers video course from my fellow Java Champion, Frank Greco. If you are new to AI and ML and want to get a great introduction to these topics, then you should definitely join watch the video lessons created by Frank Greco. And, thanks…
18 Mar 2025
Introduction In this article, I’m going to explain how to map the Oracle TIMESTAMP WITH TIME ZONE with JPA and Hibernate. While the DATE and TIMESTAMP column types are suitable for mapping the LocalDateTime, the TIMESTAMP WITH TIME ZONE column type allows us to map both the ZonedDateTime and OffsetDateTime. Oracle TIMESTAMP WITH TIME ZONE Traditionally, Oracle provided the DATE…
10 Feb 2025
Introduction If your application uses MySQL and you’re interested in getting the best out of it via performance tuning and monitoring, then Releem is a very nice tool that can assist you in your endeavor. I discovered Releem from Roman Agabekov’s posts on social media. Roman is the founder of this tool, and since his posts about MySQL performance tuning…
14 Jan 2025
Introduction In this article, I’m going to explain you should use compact table columns when designing your database schema. By using compact table columns, you can cache more table records and index entries and, therefore, speed up your SQL queries. Database caching As I explained in this article, relational database systems cache the pages that are loaded from the disk…
12 Dec 2024
Introduction Welcome to a new issue of the High-Performance Java Persistence Newsletter in which we share articles, videos, workshops, and StackOverflow answers that are very relevant to any developer who interacts with a database system using Java. Articles The pick of this week is this series of articles from Franck Pachot about Amazon Aurora Limitless, a PostgreSQL-compatible database that provides…
26 Nov 2024
Introduction In this article, we are going to see why there is no benefit in using the Set collection type when mapping a bidirectional JPA OneToMany association. While the @OneToMany annotation can be used to map both unidirectional and bidirectional associations, as I explained this article, you should avoid the unidirectional mapping since it can lead to very inefficient SQL…
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…
7 Nov 2024
Introduction In this article, we are going to see the best way to determine the optimal connection pool size using the FlexyPool auto-incrementing pool strategy. If you are unfamiliar with the reason why database applications need a connection pool, then check out this article first. Now, according to the Universal Scalability Law, the maximum throughput of a database system is…
1 Nov 2024
Introduction Welcome to a new issue of the High-Performance Java Persistence Newsletter in which we share articles, videos, workshops, and StackOverflow answers that are very relevant to any developer who interacts with a database system using Java. Articles The pick of this week is this article about data alignment in PostgreSQL tables and indexes. By properly designing your tables and…
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…
7 Oct 2024
Introduction Welcome to a new issue of the High-Performance Java Persistence Newsletter in which we share articles, videos, workshops, and StackOverflow answers that are very relevant to any developer who interacts with a database system using Java. Articles The pick of this week is this article about B+Tree indexes, which features a live tool that we can use to see…
30 Sept 2024
Eleven years later! Eleven years ago today, I decided to create my blog on WordPress.com, and this is the first article I published. Every year, on my blog anniversary, I write a report about the achievements that were made possible by having this very blog. If you are curious about my previous reports, check out the following list: 2023 2022…
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…
10 Sept 2024
Introduction In this article, we are going to see how we can map embeddable inheritance when using JPA and Hibernate. The feature described in this article is available since version 6.6 of Hibernate ORM, so if you haven’t yet upgraded, then you have one more reason to consider it. Domain Model Let’s consider we have the following Subscriber entity that…
2 Sept 2024
Introduction Welcome to a new issue of the High-Performance Java Persistence Newsletter in which we share articles, videos, workshops, and StackOverflow answers that are very relevant to any developer who interacts with a database system using Java. Articles The pick of this week is this article about the difference between the ON CONFLICT clause and the MERGE statement when using…
26 Aug 2024
Introduction In this article, we are going to see how we can create a custom Spring Boot Actuator endpoint in order to export our own metrics and events. Spring Boot Actuator is a Spring Boot module that provides support for tracing and monitoring. And, while by default, we get various health checks and metrics, we are not limited to the…
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…
6 Aug 2024
Introduction Welcome to a new issue of the High-Performance Java Persistence Newsletter in which we share articles, videos, workshops, and StackOverflow answers that are very relevant to any developer who interacts with a database system using Java. Articles The pick of this week is this article about various online schema migration options that are available in MySQL. Another great read…
30 Jul 2024
Introduction In this article, we are going to see how we can use the Hibernate SoftDelete annotation to activate soft deleting for JPA entities. While, as I explained in this article, you can manually implement the soft delete mechanism using the @SQLDelete, @Loader and @Where annotations, it’s definitely much easier to just use the native Hibernate mechanism introduced in Hibernate…
24 Jul 2024
Introduction In this article, we are going to see how we can use symbolic links to move the DB data folder. The reason why I needed to move the data folder from the C to the D Windows partition was because the C partition was running out of disk space. DB data folder A relational database system requires to store…
10 Jul 2024
Introduction My friend, Dmitry Aleksandrov, has been very generous in giving me a copy of his Helidon book, which he co-authored with Dmitry Kornilov and Daniel Kec. Since I was interested in learning about Helidon, I decided to read this book and see what approach it takes to develop Java applications, as opposed to Spring Boot. The book is called…
2 Jul 2024
Introduction Welcome to a new issue of the High-Performance Java Persistence Newsletter in which we share articles, videos, workshops, and StackOverflow answers that are very relevant to any developer who interacts with a database system using Java. Articles The pick of this week is this article about the drawbacks of using natural keys. Speaking of mapping tables, if you need…
24 Jun 2024
Introduction In this article, we are going to see how we can map Java Enum to custom values when using JPA and Hibernate. While Hibernate provides several options to save Enum values, having the option to customize this mechanism is even better, as it will allow you to better deal with legacy applications or use cases that require you to…
18 Jun 2024
Introduction In this article, we are going to see how the Hibernate JAVA_TIME_USE_DIRECT_JDBC setting works and what JDBC Driver supports this feature. This setting can be set programmatically, as illustrated by the following Spring Java-based configuration: Or you can set it declaratively in the Spring Boot application.properties configuration file like this: Domain Model To test how the Hibernate JAVA_TIME_USE_DIRECT_JDBC setting…
13 Jun 2024
Introduction In this article, we are going to see how the Hibernate WITH RECURSIVE query works and how we can use it to fetch hierarchical data structures. Domain Model Let’s consider we have the following post and post_comment tables: The post_comment child table has a one-to-many table relationship with the parent post table via the post_id Foreign Key column in…
4 Jun 2024
Introduction Welcome to a new issue of the High-Performance Java Persistence Newsletter in which we share articles, videos, workshops, and StackOverflow answers that are very relevant to any developer who interacts with a database system using Java. Articles The pick of this week is this article from Gergely Orosz about designing and scaling Bluesky. You will learn about favoring flexibility,…
29 May 2024
Introduction In this article, we are going to see how the Hibernate ON CONFLICT DO clause works, and how we can use it to execute an SQL UPSERT statement in a portable way. Upsert As I explained in this article, if you have this use case where you need to either INSERT or UPDATE a table record based on whether…
22 May 2024
Introduction In this article, we are going to see how we can use the PostgreSQL COPY command to export a large result set to an external file. ETL (Extract, Transform, Load) When implementing an ETL (Extract, Transform, Load) process, you might have to extract a large result set from a relational database in order to transform it according to some…
15 May 2024
Introduction In this article, we are going to analyze the PostgreSQL plan_cache_mode setting and see when it’s useful to override a given generic plan using the the force_custom_plan strategy. Domain Model Let’s assume we have the following post table: The post_status type is an Enum that was created like this: We have 100,000 post records in our database that have…
8 May 2024
Introduction In this article, we are going to see how you can override the FetchType.EAGER strategy using the fetchgraph query hint. While this feature has been available in the JPA specification since version 2.1, Hibernate has only supported this feature since version 5.5. JPA FetchType.EAGER and FetchType.LAZY strategies The fetching strategy can be specified either at the entity mapping level…
2 May 2024
Introduction Welcome to a new issue of the High-Performance Java Persistence Newsletter in which we share articles, videos, workshops, and StackOverflow answers that are very relevant to any developer who interacts with a database system using Java. Articles The pick of this week is this article which describes how bandwidth, latency, IOPS, and jitter works in a relational database system.…
24 Apr 2024
Introduction In this article, we are going to see what is the best way we can use OneToOne optional attribute so that we can avoid N+1 query issues. One-To-One table relationship As I explained in this article, in a one-to-one table relationship, the parent and the child tables share the Primary Key column values because the child table Primary Key…
18 Apr 2024
Introduction In this article, we are going to investigate how to calculate percentiles with the SQL PERCENTILE_CONT function. Domain Model Let’s consider we have the following quotes table that holds the historical price values of various stocks and indexes: The quotes table is populated with data that looks like this: SQL PERCENTILE_CONT The SQL Standard provides support for a great…
10 Apr 2024
Introduction In this article, we are going to investigate the difference between the PostgreSQL FOR UPDATE and FOR NO KEY UPDATE when locking a parent record and inserting a child row. Domain Model To see the difference between the PostgreSQL FOR UPDATE and FOR NO KEY UPDATE locking clauses, consider the following one-to-many table relationship where the post table is…
3 Apr 2024
Introduction Welcome to a new issue of the High-Performance Java Persistence Newsletter in which we share articles, videos, workshops, and StackOverflow answers that are very relevant to any developer who interacts with a database system using Java. Articles The pick of this week is this article about Jakarta Persistence 3.2, which brings a lot of improvements to the JPA specifications,…
26 Mar 2024
Introduction In this article, we are going to analyze how PostgreSQL Heap-Only-Tuple or HOT Update optimization works, and why you should avoid indexing columns that change very frequently. PostgreSQL Tables and Indexes Unlike SQL Server or MySQL, which store table records in a Clustered Index, in Oracle and PostgreSQL, records are stored in Heap Tables that have unique row identifiers.…
11 Mar 2024
Introduction In this article, we are going to analyze the PostgreSQL Index Types so that we can understand when to choose one index type over the other. When using a relational database system, indexing is a very important topic because it can help you speed up your SQL queries by reducing the number of pages that have to be scanned…
4 Mar 2024
Introduction Welcome to a new issue of the High-Performance Java Persistence Newsletter in which we share articles, videos, workshops, and StackOverflow answers that are very relevant to any developer who interacts with a database system using Java. Articles The pick of this week is this article about using the BUFFERS option in the PostgreSQL EXPLAIN ANALYZE so that we can…
27 Feb 2024
Introduction In this article, we are going to see how we can map the OffsetDateTime ZoneOffset with the Hibernate TimeZoneColumn annotation. As I explained in this article, by default, Hibernate doesn’t store the time-zone offset of an OffsetDateTime entity attribute in a separate column. Instead, the OffsetDateTime is stored as a java.time.Instant that is relative to the UTC time zone.…
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…
14 Feb 2024
Introduction In this article, we are going to explore various PostgreSQL performance tuning settings that you might want to configure since the default values are not suitable for a QA or production environment. As explained in this PostgreSQL wiki page, the default PostgreSQL configuration settings were chosen to make it easier to install the database on a wide range of…
7 Feb 2024
Introduction In this article, we are going to see how we can use the Hibernate StatelessSession in order to enable JDBC Batching for INSERT, UPDATE, and DELETE statements. While the StatelessSession has been available for more than 20 years, until Hibernate 6, its usefulness was rather limited since it lacked support for batching, as well as other cool features, such…
1 Feb 2024
Introduction Welcome to a new issue of the High-Performance Java Persistence Newsletter in which we share articles, videos, workshops, and StackOverflow answers that are very relevant to any developer who interacts with a database system using Java. Articles The pick of this week is this article written by Lukas Eder about various query optimizations the database can do to speed…
24 Jan 2024
Introduction In this article, we are going to see how to fetch recursive associations when using JPA and Hibernate. Recursive table relationships are built using self-referencing Foreign Key columns so that a record in a table can reference another record in the very same table, therefore allowing us to represent hierarchical structures using the relational model. Domain Model Let’s consider…
18 Jan 2024
Introduction In this article, we are going to see how to use the Java CountDownLatch to write test cases that take concurrency into consideration. The Java CountDownLatch has been available since version 1.5, and it’s part of the java.util.concurrent package that contains many other threading-related utilities. The Java CountDownLatch class The Java CountDownLatch class provides the following methods: The most…
11 Jan 2024
Introduction In this article, we are going to see what is the best way to test the data access layer when using a relational database system. First, we will talk about the pros and cons of unit testing. While unit testing is fine for validating independent logic units, when it comes to testing the data access layer, integration testing is…
3 Jan 2024
Introduction Welcome to a new issue of the High-Performance Java Persistence Newsletter in which we share articles, videos, workshops, and StackOverflow answers that are very relevant to any developer who interacts with a database system using Java. Articles The pick of this edition is this series of articles about transaction isolation levels in PostgreSQL. Speaking of database transactions, this article…
19 Dec 2023
Introduction In this article, we are going to see how the Hibernate StatelessSession Upsert method works. As I explained in this article, the UPSERT operation allows you to INSERT a record if there is no such record matching the filtering criteria or to UPDATE it in case the record exists. While many relational database systems offer the standard MERGE command…
15 Dec 2023
Introduction In this article, we are going to see what is the best way to map multiple entities on the same table. There are several advantages to mapping multiple entities on the same database table: To avoid loading large columns (e.g., JSON) To avoid N+1 query issues for bidirectional @OneToOne associations Before we start investigating the best way to map…
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…
1 Dec 2023
Introduction Welcome to a new issue of the High-Performance Java Persistence Newsletter in which we share articles, videos, workshops, and StackOverflow answers that are very relevant to any developer who interacts with a database system using Java. Articles The pick of this edition is this article about PostgreSQL double buffering. Hibernate 6.3 added support for table partitioning, and in this…
22 Nov 2023
Introduction In this article, we are going to see how Index Selectivity works in relational database systems and why the database Optimizer might choose to avoid using an index if the number of matching records is large. Index selectivity is inversely proportional to the number of index entries matched by a given value. So, a unique index has the highest…
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…
1 Nov 2023
Introduction Welcome to a new issue of the High-Performance Java Persistence Newsletter in which we share articles, videos, workshops, and StackOverflow answers that are very relevant to any developer who interacts with a database system using Java. Articles The pick of this edition is this article about MySQL 8.2 automatic read and write routing. Until MySQL 8.2 is released, if…
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…
3 Oct 2023
Introduction Welcome to a new issue of the High-Performance Java Persistence Newsletter in which we share articles, videos, workshops, and StackOverflow answers that are very relevant to any developer who interacts with a database system using Java. Articles The pick of this edition is this article that marks the 10-year anniversary of this blog. For over a decade, I managed…
29 Sept 2023
Ten years later! Ten years ago today, I decided to start blogging and published this article about injecting a List of Spring beans using the @Autowired annotation. On every blog anniversary, I write a report about all my achievements that would not have been possible without having this blog. If you are interested in my previous reports, you can check…
20 Sept 2023
Introduction In this article, we are going to see how we can find the source of an SQL query generated by Hibernate. Knowing where a given SQL query originates from is very useful when trying to investigate performance issues caused by either long-running queries or queries that are executed excessively (e.g., N+1 query issues). Hypersistence Utils This feature that we…
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…
5 Sept 2023
Introduction Welcome to a new issue of the High-Performance Java Persistence Newsletter in which we share articles, videos, workshops, and StackOverflow answers that are very relevant to any developer who interacts with a database system using Java. Articles The pick of this edition is this article, which explains how you can use the Hibernate Statistics in order to assert the…
31 Aug 2023
Introduction In this article, we’re going to see how the PostgreSQL JDBC Driver implements Statement Caching and what settings we need to configure in order to optimize the performance of our data access layer. Prepared Statements The JDBC API allows you to create a PreparedStatement by calling prepareStatement(java.lang.String) method on a given Connection reference. For this reason, it’s very common…
22 Aug 2023
Introduction In this article, I’m going to show you the best way to hide the JPA entity identifier so that the users of your application won’t be able to guess and access data that belongs to other users. This has been a recurring question that I’ve been getting when running training or workshops, so I decided it’s a good idea…
16 Aug 2023
Introduction In this article, we are going to see what are the best way to use JPA bidirectional sync methods for one-to-many, one-to-one, and many-to-many associations. For an introduction to why you need sync methods for your bidirectional JPA associations, check out this article first. Domain Model For this article, we are going to use the following entities that are…
7 Aug 2023
Introduction In this article, we are going to see how the SQL Server useBulkCopyForBatchInsert configuration property works when using JDBC, JPA, or Hibernate. SQL Server PreparedStatement batching When using JPA and Hibernate, the generated SQL statements are going to be executed using the JDBC PreparedStatement because prepared statements increase the likelihood of statement caching, and you to avoid SQL injection…
2 Aug 2023
Introduction Welcome to a new issue of the High-Performance Java Persistence Newsletter in which we share articles, videos, workshops, and StackOverflow answers that are very relevant to any developer who interacts with a database system using Java. Articles The pick of this edition is this article, which explains how the standard PostgreSQL MVCC system works and how the OrioleDB engine…
26 Jul 2023
Introduction In this article, we are going to see how we can do JDBC profiling using the VisualVM tool to find slow-running SQL queries or discover N+1 query issues. VisualVM VisualVM is an OSS tool that allows us to inspect the JVM and the Java applications running inside the Java Virtual Machine. You can download VisualVM from this link and…
21 Jul 2023
Introduction In this article, we are going to see how we can implement a soft delete mechanism when the JPA entity features a version property. If you are not using a version property for optimistic locking, then you can implement the soft delete strategy, as explained in this article. Domain Model Let’s assume we have the following Post, PostDetails, PostComment,…
11 Jul 2023
Introduction In this article, we are going to see how to customize the HibernateTypesContributor from Hypersistence Utils so that we can disable all or filter out certain Types. What does HibernateTypesContributor does in Hypersistence Utils The HibernateTypesContributor implements the TypeContributor Hibernate interface and registers several Types automatically. By doing this, the audited entities will be able to locate the Hypersistence…
4 Jul 2023
Introduction Welcome to a new issue of the High-Performance Java Persistence Newsletter in which we share articles, videos, workshops, and StackOverflow answers that are very relevant to any developer who interacts with a database system using Java. Articles The pick of this edition is this article, which provides a very thorough explanation of how queries are executed and how you…
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…
21 Jun 2023
Introduction In this article, we are going to see how we can fetch multiple to-many relationships with jOOQ MULTISET so that we avoid bumping into an unintentional Cartesian Product. The approach taken by jOOQ is truly revolutionary since it offers a solution that allows you to fetch multiple to-many relationships using a single type-safe query. Domain Model Let’s assume that…
14 Jun 2023
Introduction In this article, we are going to see what is the best way to use one-to-one table relationships. I decided to write this article after reading this Tweet: One-to-one table relationships As I explained in this article, a relational database system defines three table relationship types: one-to-many one-to-one many-to-many The one-to-one table relationships rely on the fact that both…
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…
31 May 2023
Introduction Welcome to a new issue of the High-Performance Java Persistence Newsletter in which we share articles, videos, workshops, and StackOverflow answers that are very relevant to any developer who interacts with a database system using Java. Articles The pick of this edition is this article which explains why performance engineering is not just about solving a few bottlenecks that…
25 May 2023
Introduction In this article, we are going to see how we can achieve cache synchronization with the help of jOOQ and PostgreSQL functions. By using Change Data Capture, we can track how table records change over time and synchronize the application-level cache entries that were built from the table records in question. Domain Model Let’s assume we are building a…
17 May 2023
Introduction In this article, we are going to see what is the standard SQL operation order. Once you understand the order in which SQL operations are executed, then it will be clear why the Oracle legacy pagination query required a Derived Table in order to make sure that the ROWNUM pseudocolumn is computed after executing the ORDER BY clause. SQL…
9 May 2023
Introduction In this article, we are going to see how the default entity sequence changes when migrating from Hibernate 5 to Hibernate 6. Domain Model Let’s assume we have a Post parent entity that has a one-to-many bidirectional association with the PostComment child entity. The Post parent entity looks like this: And the PostComment looks as follows: Notice that both…
3 May 2023
Introduction Welcome to a new issue of the High-Performance Java Persistence Newsletter in which we share articles, videos, workshops, and StackOverflow answers that are very relevant to any developer who interacts with a database system using Java. Articles The pick of this edition is this article, which explains the bad parts of PostgreSQL. While PostgreSQL is a great open-source database…
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…
19 Apr 2023
Introduction In this article, we are going to see what is the best way to call SQL Server stored procedures with jOOQ. I decided to write this article because stored procedures and database functions are extremely useful for data-intensive applications, and sometimes, they are the only solution to process data efficiently. While SQL remains the de-facto way to query data,…
13 Apr 2023
Introduction In this article, we are going to see how the PostgreSQL Auto Explain feature works and why you should use it to gather the actual execution plan for SQL statements that execute on a production system. SQL Execution Plan As I explained in this article, when you send a SQL statement to PostgreSQL, the statement is executed as illustrated…
4 Apr 2023
Introduction Welcome to a new issue of the High-Performance Java Persistence Newsletter in which we share articles, videos, workshops, and StackOverflow answers that are very relevant to any developer who interacts with a database system using Java. Articles The pick of this edition is this article, which explains the performance optimizations done by the Quora engineering team to scale their…
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…
10 Mar 2023
Introduction In this article, we are going to see what is the best way to use the amazing SQL NOWAIT feature that allows us to avoid blocking when acquiring a row-level lock. Since all the top major database support this functionality, Hibernate offers a NOWAIT option that allows you to render the proper database-specific syntax associated with this feature without…
1 Mar 2023
Introduction Welcome to a new issue of the High-Performance Java Persistence Newsletter in which we share articles, videos, workshops, and StackOverflow answers that are very relevant to any developer who interacts with a database system using Java. Articles The pick of this edition is this article that explains why an SQL Server query executed by an application can have a…
23 Feb 2023
Introduction In this article, we are going to explore the YugabyteDB architecture and see how it manages to provide automatic sharding and failover without compromising data integrity. YugabyteDB is a distributed SQL database, so its architecture is different than the ones employed by traditional relational database systems. Traditional relational database architecture Most relational database systems use a Single-Primary replication architecture,…
16 Feb 2023
Introduction In this article, I will show you the best way to generate a TSID entity identifier with JPA and Hibernate. Prior to continuing, if you are not familiar with the advantages of using compact Time-Sorted Identifiers, like TSID, check out this article first. Hypersistence Utils As promised, the Hypersistence Utils OSS library keeps on evolving, adding more and more…
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…
1 Feb 2023
Introduction Welcome to a new issue of the High-Performance Java Persistence Newsletter in which we share articles, videos, workshops, and StackOverflow answers that are very relevant to any developer who interacts with a database system using Java. Articles The pick of this edition is this article that not only explains how B+Tree indexes work in PostgreSQL, but it provides an…
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…
11 Jan 2023
Introduction In this article, we are going to see the overhead of acquiring a new connection when using YugabyteDB and why connection pooling is mandatory for performance. Acquiring a database connection using JDBC To interact with a database system, first, we need to acquire a database connection. And, when using Java, we need to obtain a Connection object from the…
5 Jan 2023
Introduction Welcome to a new issue of the High-Performance Java Persistence Newsletter in which we share articles, videos, workshops, and StackOverflow answers that are very relevant to any developer who interacts with a database system using Java. Articles The pick of this edition is this article about 10 common PostgreSQL mistakes and how you can avoid them. If you want…
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…