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.

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.

The best way to map an entity version property with JPA and Hibernate

Introduction In this article, we are going to see what is the best way to map the entity version property with JPA and Hibernate. Most often, we overlook the basic entity type mappings, focusing more on associations or querying options. However, basic types can also have a significant impact on application performance, especially if the type in question is used in many entity mappings.

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 merge entity collections with JPA and Hibernate

Introduction In this article, you are going to learn why overwriting entity collections is an anti-pattern and how you can merge collections both effectively and efficiently with JPA and Hibernate. The reason I decided to write this article is that I’ve been this question asked over and over and over again.

The best way to clone or duplicate an entity with JPA and Hibernate

Introduction Have you ever wondered how to clone an entity with JPA or Hibernate? Recently, I stumbled upon this Hibernate forum question and it reminded me that this was a common requirement when working with JPA and Hibernate. In this article, we are going to see the best way to clone a JPA entity with JPA and Hibernate.

How to map a JPA entity to a View or SQL query using Hibernate

Introduction In this article, you are going to learn how to map a JPA entity to the ResultSet of a SQL query using the @Subselect Hibernate-specific annotation.

How to JOIN unrelated entities with JPA and Hibernate

Introduction Answering questions on the Hibernate forum is a neverending source of inspiration. The following question is about joining unrelated entities using entity queries, and this post is going to explain how you can do it when using JPA and Hibernate.

How to map calculated properties with JPA and Hibernate @Formula annotation

Introduction As I explained in this StackOverflow question, mapping calculated properties is very easy with JPA and Hibernate. In this post, I’m going to demonstrate how you can derive some entity property based on one or multiple persistent entity attributes.