From the course: Java Persistence with JPA and Hibernate
Entities and the persistence context - JPA Tutorial
From the course: Java Persistence with JPA and Hibernate
Entities and the persistence context
- [Instructor] You are most probably aware of the concept of cache. In computing, a cache is a place where a copy of data is stored when the original data itself is stored in a different place. The purpose of cache is to be able to serve different requests for data, like updates, reads, and removals faster and in an efficient manner. In Jakarta persistence, the persistence context serves a similar purpose. In fact, in ORM framework, no operations like database inserts, updates, deletes, or retrievals are issued directly on the database. All these operations take place via another layer that sits between the application and the database. You can think that the persistence context is similar to this layer. In my opinion, it's not wrong even to say that this layer is the persistence context. If you follow chapter one of this course, you would know that ORM creates a layer of abstraction. This is that layer of abstraction and ORM creates it between the object-oriented application and the relational database. So it's technically correct if you think that the persistence context is the real layer of abstraction. So the persistence context is the magic behind ORM, but how does it work? Well, before we check that out, let's first be clear with these terms. Entity, entity instance, and lifecycle. A table in a relational database, which is also known as a database entity, can be represented as an entity in the application. Then a row in that table becomes an entity instance. An entity instance will have its own lifecycle from initial creation of birth to death. This lifecycle has four states transient. That is, when an entity instance is newly instantiated and hasn't been persisted yet. So it doesn't still represent any database table record. Managed, this is when an entity instance can be tracked or changes happening to it can be tracked by an ORM framework like hibernate. Detached, this is when an entity instance was previously managed or was being tracked, but is no longer so. Removed, this is when an entity instance is marked for removal. Well, if you carefully consider these four states, you'll realize that these four things could happen to each every record in your database table. Now, coming back to how the persistence context works, you can think of it as a temporary storage area created by an ORM framework like hibernate to make it easier for your application to interact with the database. Within the persistence context, you have a set of entity instances that are actually instances of domain model classes that reside within your OO application. These instances can be in any of the four states that you saw just now. That is, a newly created instance can be there. That doesn't still represent any database table record. In other words, in a transient state. Instances can be there that are associated with existing database table records. That means in managed state, these instances will always be associated with an IED, and the persistence context always keep struck of changes occurring to them. Instances that were managed earlier but no longer being managed or tracked are in a detached state. When in this state, the persistence context will not track the changes occurring to it. Then the instances associated with database table records are scheduled to be removed from the database, meaning in a removed state. So in essence, a persistence context is like a place where we put the entity instances that hibernate works with, while working in a given application scenario. The persistence context keeps track of any changes made to managed entity instances. If any changes happen during a transaction, the entity is marked dirty. When the transaction completes, the changes are flushed into the persistent storage, which in this case is the database.
Contents
-
-
-
-
-
Entities and the persistence context5m 9s
-
(Locked)
EntityManager interface4m 55s
-
(Locked)
Bootstrapping JPA and Hibernate: Creating the configuration5m 56s
-
(Locked)
Bootstrapping JPA and Hibernate: Creating the EntityManager3m 33s
-
(Locked)
Challenge: Art Class Management app, part 11m 30s
-
(Locked)
Solution: Art Class Management app, part 11m 43s
-
-
-
-
-
-
-