The best way to map a Composite Key with JPA and Hibernate

Introduction One of my readers asked me to help him map a Composite Key using JPA and Hibernate. Because this is a recurrent question, I decided to write a blog post in which I describe this mapping in more detail.

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.

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

How to map JSON objects using generic Hibernate Types

Introduction In this article, we are going to see how you can map a JSON column to a JPA entity attribute using the Hypersistence Utils open-source project. While you can create your own custom Hibernate Types, for mapping JSON column types on Oracle, SQL Server, PostgreSQL, or MySQL, you don’t need to implement your own Hibernate Type since the Hypersistence Utils project already offers this functionality.

How to implement a custom String-based sequence identifier generator with Hibernate

Introduction One of my blog readers bumped into the assigned generator with a sequence or an identity column post and wondered if it was possible to generate String-based identifiers instead. I accepted the challenge and answered his question on StackOverflow. However, this post is going to explain this topic in greater detail, so there we go.

How to implement equals and hashCode using the JPA entity identifier (Primary Key)

Introduction As previously explained, using the JPA entity business key for equals and hashCode is always the best choice. However, not all entities feature a unique business key, so we need to use another database column that is also unique as the primary key. But using the entity identifier for equality is very challenging, and this post is going to show you how you can use it without issues.

The Open Session In View Anti-Pattern

Introduction Open Session in View is an Anti-Pattern, and this post is going to demonstrate why it is so. First of all, let’s start with the Wikipedia definition of an Anti-Pattern: An anti-pattern (or antipattern) is a common response to a recurring problem that is usually ineffective and risks being highly counterproductive.

How to call PostgreSQL functions (stored procedures) with JPA and Hibernate

Introduction This article is part of a series of posts related to calling various relational database systems stored procedures and database functions from Hibernate. The reason for writing this down is because there are many peculiarities related to the underlying JDBC driver support and not every JPA or Hibernate feature is supported on every relational database.

How to call SQL Server stored procedures and functions with JPA and Hibernate

Introduction This article is part of a series of posts related to calling various relational database systems stored procedures and database functions from Hibernate. The reason for writing this down is because there are many peculiarities related to the underlying JDBC driver support and not every JPA or Hibernate feature is supported on every relational database.