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 Last week, one of my blog readers asked me of a way to reuse the @Id mapping so that it won’t have to be declared on each an every entity. Because this is a good opportunity to introduce @MappedSuperclass, I decided to answer the question with a blog post.

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.