Persistence Context Introduction#
What Is The Persistence Context#
Persistence context: Thepersistence contextdefines the set of active instances that the application is handling. Thepersistence contextcan be created manually or through injection.- The
Persistence Contextis responsible for JPA entity management: When an application loads an Entity from the database, theentityis in fact stored in thePersistence Context, so theentitybecomes managed by thePersistence Context. Any further change made over that sameentitywill be monitored by thePersistence Context. - The
Persistence Contextwill also flush changedentitiesto the database when appropriate. When a transaction commits, the associatedPersistence Contextwill also flush any eventual pending changes to the Database. - We can understand like the
persistence contextis the first-level cache where all theentitiesare fetched from the database or saved to the database.
- The
Persistence Context Types#
Persistence Contexthave two types: