First-Level Cache (Persistence Context)
Learn First-Level Cache (Persistence Context) (Phase 6 · Hibernate Caching (Essential), Hibernate) with worked examples, diagrams and interview questions. Free on JavaSchoolLab.
What the first-level cache is • The first-level (L1) cache is the persistence context of a Session/EntityManager: always on, per session , not shared, cannot be disabled. It stores every managed entity by (entity type, id) . • Effects: repeated find(Course.class, 1L) in one transaction run one SELECT ; the same Java instance is returned (identity guarantee); queries that return entities first check the cache by id for the returned rows (but a JPQL query itself always hits the database). • Lifetime = the unit of work. In Spring it is the @Transactional method; each non-transactional repository...