How to expose Hibernate Statistics via JMX

Introduction As already explained, Hibernate provides a very flexible statistics mechanism. The Hibernate metrics can be accessed either programmatically via the org.hibernate.stat.Statistics interface or via JMX (Java Management Extensions). In this article, you are going to see how you can expose the Hibernate statistics metrics using JMX.

A beginner’s guide to Hibernate Statistics

Introduction Hibernate provides a very powerful Statistics mechanism that, unfortunately, is lesser known. In this article, we are going to see how the Hibernate Statistics mechanism works, and how you can activate. While the Hibernate Statistics mechanism is not enabled by default, you will see that many applications can benefit from using it, especially when it comes to determining cache effectiveness.

How to change the @OneToOne shared primary key column name with JPA and Hibernate

Introduction In this article, we are going to see how you can change the @OneToOne shared primary key column name when using JPA and Hibernate. This has been a recurrent theme when answering questions about Hibernate or during my High-Performance Java Persistence training. As previously explained, the one-to-one database table relationship requires the Primary Key to be shared among the parent and the child tables. Unfortunately, just adding the JPA @OneToOne annotation in the child entity does not render a true one-to-one table relationship since a separate Foreign Key column will be… Read More

Hypersistence Optimizer initial release

Introduction At the end of 2018, I got this idea of writing a tool which can automatically detect JPA and Hibernate issues by scanning your data access layer and provide you optimization tips. At the beginning of February, Thodoris Chaikalis surprised me with this Facebook comment which reinforced the idea that having such a tool would be really awesome for Java developers working with JPA and Hibernate. At the end of February, I got some time off, and I started working on it, and the reaction on social media exceeded my expectations:… Read More

Why you should avoid EXTRA Lazy Collections with Hibernate

Introduction In this article, you are going to learn why using EXTRA Lazy Collections with Hibernate is a bad idea since it can lead to N+1 query issues and cause performance problems. The reason I wanted to write this article is that I keep seeing it mentioned in StackOverflow or the Hibernate forum.

How does the entity version property work when using JPA and Hibernate

Introduction In this article, I’m going to show you how the JPA @Version entity property works when using Hibernate. The most significant benefit of adding a version property to a JPA entity is that we can prevent the lost update anomaly, therefore ensuring that data integrity is not compromised.

How do JPA persist, merge and Hibernate save, update, saveOrUpdate methods work

Introduction In this article, I’m going to show you how the JPA persist and merge work and how do they compare with the Hibernate save, update, and saveOrUpdate methods. Although you should favor the JPA-based entity state transition methods, you are going to see that the Hibernate-specific update is actually a good alternative to merge when you want to reduce the number of SQL queries being executed during a batch processing task.

How to log the database transaction id using MDC (Mapped Diagnostic Context)

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 db-util… 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 String JPA property to a JSON column using Hibernate

Introduction In this article, I want to show you how you can map a String JPA property to a JSON database column using the hibernate-types open-source project. Although it’s probably more common to use a JsonNode or POJO (Plain Old Java Object) on the Java side, the hibernate-types framework is very flexible and allows you to use a String JPA property type to represent a JSON structure.