How to log the database transaction id using MDC
Introduction In this article, I’m going to show you how you can log the database transaction id associated with a given SQL statement using the MDC (Mapped Diagnostic Context) feature offered by many logging frameworks. If you are writing data access code, you have to use logging. As I explained in this article, using a JDBC proxy tool like datasource-proxy or p6spy is the best approach to logging SQL statements. With datasource-proxy, you can easily build a JUnit extension to automatically detect N+1 query issues. For more details, check out the Hypersistence… Read More
The best way to call a stored procedure with JPA and Hibernate
Introduction In this article, you are going to learn the best way to call a stored procedure when using JPA and Hibernate so that the underlying JDBC resources are released as soon as possible. I decided to write this article since the way Hibernate handles stored procedures can lead to ORA-01000: maximum open cursors exceeded issues on Oracle as explained in this Hibernate forum thread or StackOverflow question.
How to map a PostgreSQL HStore entity property with JPA and Hibernate
Introduction The Hypersistence Utils open-source project allows you to map a great variety of database types that are not supported natively by Hibernate ORM (e.g. JSON, ARRAY, YearMonth, Month, INET addresses). In this article, we are going to see how you can map a PostgreSQL HStore type, which allows you to store key/value pairs, to a Java Map entity property when using JPA and Hibernate.
How to map a PostgreSQL Enum ARRAY to a JPA entity property using Hibernate
Introduction The Hypersistence Utils OSS project allows you to map JSON, ARRAY, YearMonth, Month or database-specific columns (e.g., INET addresses). In this article, we are going to see how you can map a PostgreSQL Enum ARRAY type to a Java array entity property when using JPA and Hibernate.
How to bind custom Hibernate parameter types to JPA queries
Introduction In this article, we are going to see how we can bind a custom Hibernate parameter type to a JPA query. I decided to write this article after one of my hibernate-types framework users created this very interesting issue on GitHub.
The best way to initialize LAZY entity and collection proxies with JPA and Hibernate
Introduction In this article, we are going to see the best way to initialize LAZY proxies and collections when using JPA and Hibernate. I decided to write this article because there are way too many resources available on the Internet that mislead the reader into using awkward and inefficient practices.
How to execute SQL functions with multiple parameters in a JPQL query with Hibernate
Introduction In this article, we are going to see how you can execute SQL functions with multiple parameters in JPQL queries with Hibernate. I recently stumbled on this requirement while answering this StackOverflow question, which is a never-ending source of inspiration for articles on my blog.
The best way to use the JPQL DISTINCT keyword with JPA and Hibernate
Introduction In this article, we are going to how the JPQL DISTINCT keyword behaves depending on the underlying entity query type.
How to map camelCase properties to snake_case column names with Hibernate
Introduction In this article, you are going to learn how to map cameCase entity properties (e.g., phoneNumber) to snake_case column names (e.g., phone_number) using a Hibernate naming strategy. While you could achieve the same goal with the name attribute of JPA @Column annotation, it’s much more convenient to use a custom Hibernate strategy to apply this naming convention consistently.

