JPA: Persistence Context And Dirty Check Mechanism

Few entities were being persisted to database in certain cases, without explicitly calling save() or without having @Transactional annotation. Digging a little deeper, I realised this behaviour is due to persistence context and dirty check mechanism. Persistence Context Observation 1: Two entities, user and account, are retrieved from database and fields of both entities are modified. When save() was called only on account entity, both the entities were being persisted to database....

June 21, 2021 · 2 min · Me

JPA EntityGraphs: A Solution to N+1 Query Problem

The N+1 query problem is said to occur when an ORM, like hibernate, executes 1 query to retrieve the parent entity and N queries to retrieve the child entities. As the number of entities in the database increases, the queries being executed separately can easily affect the performance of the application. This article will demonstrate how N+1 queries occur and their solution through an example in spring boot. The N+1 Query Problem Consider a Content Management System that stores a list of articles per publication....

March 11, 2021 · 3 min · Me