JPA Association Fetching Validator

Introduction In this article, I’m going to show you how we can build a JPA Association Fetching Validator that asserts whether JPA and Hibernate associations are fetched using joins or secondary queries. While Hibernate does not provide built-in support for checking the entity association fetching behavior programmatically, the API is very flexible and allows us to customize it so that we can achieve this non-trivial requirement.

JPA Entity Graph

Introduction In this article, I’m going to explain how you can fetch an entity association using a JPA Entity Graph and how you can build it either declaratively or programmatically.

JPA Default Fetch Plan

Introduction In this article, I’m going to explain what the JPA Default Fetch Plan is and how it differs from the Query Fetch Plan when using FetchType EAGER associations.

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 lazy load entity attributes using JPA and Hibernate

Introduction When fetching an entity, all attributes are going to be loaded as well. This is because every entity attribute is implicitly marked with the @Basic annotation whose default fetch policy is FetchType.EAGER. However, the attribute fetch strategy can be set to FetchType.LAZY, in which case the entity attribute is loaded with a secondary select statement upon being accessed for the first time. This configuration alone is not sufficient because Hibernate requires bytecode instrumentation to intercept the attribute access request and issue the secondary select statement on demand.

JPA and Hibernate FetchType EAGER is a code smell

Introduction Hibernate fetching strategies can really make a difference between an application that barely crawls and a highly responsive one. In this post, I’ll explain why you should prefer query-based fetching instead of global fetch plans. Fetching 101 Hibernate defines four association retrieving strategies: Fetching Strategy Description Join The association is OUTER JOINED in the original SELECT statement Select An additional SELECT statement is used to retrieve the associated entity(entities) Subselect An additional SELECT statement is used to retrieve the whole associated collection. This mode is meant for to-many associations Batch An… Read More