The best way to map a @OneToOne relationship with JPA and Hibernate

Introduction In this article, we are going to learn the best way to map a OneToOne association with JPA and Hibernate. While there are many ways you can map a one-to-one relationship with Hibernate, I’m going to demonstrate which mapping is the most efficient one from a database perspective.

How do persist and merge work in JPA

Introduction In this article, I’m going to explain how the persist and merge entity operations work when using JPA and Hibernate. When using JPA, entity state transitions are translated automatically to SQL statements. This post is going to explain when to use persist and when to use merge.

The best way to detect database connection leaks

Introduction Database connections are not free, and that’s the reason for using a connection pooling solution in the first place. However, the connection pool alone does not solve every issue associated to managing database connections. The application developer must make sure that every Connection is closed when no longer needed. Behind the scenes, the connection pool gives a logical transaction which, when being closed, it returns back to the pool so that it can be further reused by other concurrent transactions. A connection leak happens when a connection is acquired without ever… Read More

How to lazy load entity properties with Hibernate

Introduction One of my readers bumped into the JSON mapping post and asked me if we can fetch the JSON properties lazily. This post demonstrates how easily this can be done when using Hibernate as a JPA provider. As I previously explained, EAGER fetching is a code smell and loading associations eagerly is very detriment to application performance. However, it’s not just associations that we must be careful about. Basic entity properties may also cause performance issues as well. In this post, I’m going to show you how you can fetch entity… Read More