liveBook · Manning

Snapshot

As explained in Java Persistence with Spring Data and Hibernate epub

A state snapshot is a stored copy of an entity's state used to detect changes made to the managed instance.

A persistence snapshot is a stored copy of an entity’s state captured when Hibernate loads it into the persistence context. During flushing, Hibernate compares the managed entity with that copy; a difference triggers an SQL UPDATE. section 10.2.3

Detecting a changed entity

Item item = em.find(Item.class, ITEM_ID); if (item != null) item.setName("New Name"); Hibernate compares the modified Item with its load-time snapshot during flush and writes the changed state with an UPDATE. section 10.2.3

Snapshots support automatic dirty checking, but they consume memory for every persistent instance and make flushing more expensive because Hibernate compares managed instances with their snapshots. Large persistence contexts can therefore cause memory exhaustion or slow synchronization. section 10.2.3 section 10.2.8

Control snapshot overhead by limiting which entities remain managed:

  • Use EntityManager#detach(entity) to remove one instance from the persistence context. section 10.2.8
  • Use EntityManager#clear() to detach all managed instances and release the persistence context’s retained state. section 10.2.8
  • Use read-only settings when modifications are unnecessary; Hibernate then disables snapshots and dirty checking, so flush does not update the database. section 10.2.8

How Java Persistence with Spring Data and Hibernate epub covers it

How deep it goes
Explains snapshot creation, comparison, memory cost, flush overhead, and read-only suppression, but not snapshot representation internals.
Examples
Uses a modified Item name, generated UPDATE behavior, detach and clear operations, and read-only Hibernate configuration.
What it assumes
Assumes familiarity with managed entities, persistence contexts, EntityManager operations, transactions, and flushing.
What you can do after
Diagnose memory and flush costs, then reduce them with detach, clear, read-only settings, or narrower queries.
How it connects
Connects snapshots with dirty checking, persistence contexts, flushing, transactions, and read-only entity management.

Also explained in

Compare how 11 books explain snapshot →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Java Persistence with Spring Data and Hibernate epub. The section links point to the parts of the book it draws on.