Quick revision
The short answer
The persistence context, often called the first-level cache, is the set of managed entity instances that one EntityManager is tracking. Its guarantee is identity: within one context, each database row is represented by at most one Java object, so repeated finds for the same id return the same instance. find checks the context before querying, queries still go to the database but return the managed instances, and the context keeps snapshots for dirty checking and queues changes until flush; in Spring it is usually bound to the transaction.
What to remember
In this answer, we saw that the persistence context is the set of managed entities one EntityManager tracks, and that it guarantees one Java object per database row within the context. find checks the context before running SQL, while queries always run but return the already managed instances. The context keeps snapshots for dirty checking and queues changes until flush, which is also why it consumes memory. In Spring it is bound to the transaction and closed at its end, so identity holds within a transaction but not across transactions.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
The persistence context, often called the first-level cache, is the set of managed entity instances that one EntityManager is tracking. Its guarantee is identity: within one context, each database row is represented by at most one Java object, so repeated finds for the same id return the same instance. find checks the context before querying, queries still go to the database but return the managed instances, and the context keeps snapshots for dirty checking and queues changes until flush; in Spring it is usually bound to the transaction.
Why this question is asked
The persistence context is the part of JPA that does the most work and gets the least attention. It decides when SQL runs, why we never call update, why the same entity loaded twice is the same object, and why memory can grow in a long transaction. Interviewers ask about it because it is the foundation for understanding dirty checking, entity states and many performance problems. In this answer, we will see what the persistence context holds, prove its identity guarantee in code, look at how find and queries use it, understand snapshots and flush, and finish with its lifetime in Spring.
1What the Persistence Context Holds
Every EntityManager has a persistence context: internally, a map from entity type and id to the managed instance, plus some bookkeeping for each instance. When we load an entity, Hibernate puts it in this map. When we persist a new one, it goes in too. As long as the entity is in the map, it is managed.
It is called the first-level cache because it does avoid repeated database reads, but that is a side effect. Its main purpose is to be the single place where the current state of each loaded row lives during a unit of work, so that JPA can track changes and keep object identity consistent. It is not shared between EntityManagers or threads, and it does not survive the transaction in Spring.
2The Identity Guarantee
Within one persistence context, a database row maps to at most one Java object. However we reach that row, by find, by a query, or by navigating an association, we get the same instance.
The reason this matters is consistency. If a and c were different objects, we could change one and read the old value from the other, and at flush Hibernate would not know which state to write. With one object per row, every part of the code in the transaction sees the same, current state.
3How find and Queries Use the Context
find by id checks the persistence context first. If the entity is already there, no SQL runs. That is why the second find above was free.
A JPQL or Criteria query is different. Hibernate cannot know which rows a query will match without asking the database, so the query always runs. But when the rows come back, Hibernate looks up each id in the context. If an entity with that id is already managed, it returns the existing instance and does not overwrite it with the database values. So if we changed a in memory and then ran the query, c would show our in-memory change, not the database's value. Before running the query, Hibernate also flushes pending changes that could affect its result, in the default AUTO flush mode, so the database sees our changes too.
- Is it a find by id?YesLook in the context firstNoRun the SQL query
- Already managed?YesReturn that instance, no SQLNoRun SELECT, store new instance
- Query rows come backfor each id already managed, keep and return the existing instance
3 steps in order. Play them, or step through them one at a time.
find can skip SQL; queries always run but still return the managed instances.
4Snapshots, Dirty Checking and Flush
When Hibernate puts a loaded entity into the context, it also keeps a snapshot: a copy of the values as they were loaded. At flush, it compares each managed entity's current values with its snapshot. Any difference becomes an UPDATE. This is dirty checking, and it is why we never call an update method for a managed entity.
Changes are not written immediately. The context queues inserts, updates and deletes and writes them at flush, which happens before commit, before queries that need it, or when we call flush() ourselves. This write-behind lets Hibernate order statements correctly and batch them.
The snapshots are also the cost: each managed entity is kept twice in memory, and dirty checking compares every managed entity at every flush. That is why a context holding hundreds of thousands of entities gets slow and can run out of memory.
5The Context's Lifetime in Spring
In a Spring application, we usually do not create EntityManagers ourselves. When a @Transactional method starts, JpaTransactionManager creates an EntityManager, and with it a fresh persistence context, and binds it to the current thread. Every repository and every injected EntityManager in that transaction uses it. At commit, the context is flushed and closed, and all its entities become detached.
So the identity guarantee holds for one transaction, not across transactions. Two transactions loading the same row get two different objects. The one common exception is Open Session in View, enabled by default in Spring Boot web applications, which keeps the context open for the whole web request so that lazy loading still works in controllers.
6Follow-up questions
What is the difference between the first-level and second-level cache?
The first-level cache is the persistence context: per EntityManager, always on, holding managed objects, and gone when the transaction ends. The second-level cache is optional and shared across EntityManagers in the application; it stores entity data, not managed objects, and must be enabled per entity with a cache provider. A find that misses the persistence context can be served by the second-level cache before going to the database.
Can we remove an entity from the persistence context?
Yes. detach(entity) removes one entity, and clear() removes all of them, making them detached. Pending changes to detached entities that were not flushed are lost, so we flush before clearing. This is the standard technique for keeping memory flat in large batch jobs.
Why can a query return stale data for an entity we already loaded?
Because the context does not overwrite managed instances with query results. If another transaction changed the row after we loaded it, our query still returns our existing instance with the older values. If we need fresh values, em.refresh(entity) reloads it from the database.
7Conclusion
In this answer, we saw that the persistence context is the set of managed entities one EntityManager tracks, and that it guarantees one Java object per database row within the context. find checks the context before running SQL, while queries always run but return the already managed instances. The context keeps snapshots for dirty checking and queues changes until flush, which is also why it consumes memory. In Spring it is bound to the transaction and closed at its end, so identity holds within a transaction but not across transactions.
Open with
The persistence context is the set of entity instances that one EntityManager is managing, essentially a map from entity type and id to the instance. People call it the first-level cache, but its main purpose is to be the single current copy of each row during a unit of work.
Then, if they want more
Its guarantee is identity: inside one persistence context, a row is represented by at most one Java object. If I call find twice with the same id, I get the same instance and the second call runs no SQL, because find checks the context first.
A JPQL query always goes to the database, since Hibernate cannot know which rows match, but for each returned id that is already managed, it returns the existing instance and does not overwrite it, so my in-memory changes are what I see.
The context also keeps a snapshot of each loaded entity; at flush it compares current values with the snapshot and writes updates for the differences, which is dirty checking, and it queues changes until flush rather than writing immediately.
In Spring the context is created when a transaction starts, bound to the thread, and flushed and closed at commit, so the guarantee holds within one transaction, and two transactions get two different objects for the same row.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Data JPA reference, Hibernate ORM documentation, Spring Framework reference, Transaction management
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "What is the persistence context (first-level cache), and what guarantee does it provide...": https://scrivio-interview-library.netlify.app/databases/jpa-fundamentals/b2a726a4c1/
Spring Boot backend interviews, question 12 of 16 Back to the path
More in JPA and Hibernate fundamentals