How to generate JPA entity identifier values using a database sequence

Introduction In this article, I’m going to show you how you can generate entity identifier values using a database sequence when using JPA and Hibernate. Using a database sequence is the most efficient Hibernate identifier generation strategy, as it allows you to take advantage of the automatic JDBC batching mechanism.

How to migrate the hilo Hibernate identifier optimizer to the pooled strategy

Introduction In this article, I’m going to show you how to migrate from the legacy hilo sequence-based identifier optimizer to the pooled Hibernate strategy. I decided to write this article after having a discussion with Gerd Aschemann on Twitter about addressing the HHH-13783 Hibernate issue.

The best way to fetch multiple entities by id using JPA and Hibernate

Introduction In this article, we are going to see how we can load multiple entities by id at once when using JPA and Hibernate. Loading multiple entities by their identifier is a very common requirement when using JPA and Hibernate. Hence, we are going to see how we can optimize the underlying SQL query execution.

How to inherit properties from a base class entity using @MappedSuperclass with JPA and Hibernate

Introduction In this article, we are going to see how @MappedSuperclass can help us reuse the @Id mapping of a JPA and Hibernate entity so that it won’t have to be declared on each and every entity.

How to map a composite identifier using an automatically @GeneratedValue with JPA and Hibernate

Introduction One of my readers asked me to answer the following StackOverflow question. While I already covered the best way to map composite identifiers with JPA and Hibernate, this use case is different because one column is automatically generated.

Why you should never use the TABLE identifier generator with JPA and Hibernate

Introduction From a data access perspective, JPA supports two major types of identifiers: assigned generated The assigned identifiers must be manually set on every given entity prior to being persisted. For this reason, assigned identifiers are suitable for natural keys. For synthetic Primary Keys, we need to use a generated entity identifier, which is supported by JPA through the use of the @GeneratedValue annotation. There are four types of generated identifier strategies which are defined by the GenerationType enumeration: AUTO IDENTITY SEQUENCE TABLE The AUTO identifier generator strategy chooses one of the… Read More

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 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.