0% found this document useful (0 votes)
9 views12 pages

ClassBook-Lesson02-The Persistence Life Cycle

The document discusses the persistence life cycle of entities in JPA with Hibernate 3.0, focusing on the role of the EntityManager and the persistence context. It outlines the different states of an entity: New, Managed, Detached, and Removed, detailing how entities transition between these states during their lifecycle. The lesson emphasizes the importance of understanding these concepts for effective ORM implementation.

Uploaded by

Suresh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views12 pages

ClassBook-Lesson02-The Persistence Life Cycle

The document discusses the persistence life cycle of entities in JPA with Hibernate 3.0, focusing on the role of the EntityManager and the persistence context. It outlines the different states of an entity: New, Managed, Detached, and Removed, detailing how entities transition between these states during their lifecycle. The lesson emphasizes the importance of understanding these concepts for effective ORM implementation.

Uploaded by

Suresh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

JPA with Hibernate 3.

0 The Persistence Life Cycle

Instructor Notes:

Page 02-1
JPA with Hibernate 3.0 The Persistence Life Cycle

Instructor Notes:

Add instructor notes


here.

Page 02-2
JPA with Hibernate 3.0 The Persistence Life Cycle

Instructor Notes:

Add instructor notes


here.

Before we start working with ORM, it is very important to understand how


ORM works. The slide shows an example of abstraction, when we dial or
receive a call on mobile, lot of functionality goes in background. We as a user,
least bothered about internal component working due to abstraction.

Similarly, objects created in your application, when passed to ORM, get stored
in database table. How it happens? What work goes in background? How
your object persisted in database?

This lesson tries to answer abstract working of ORM and we will discuss in
details about the object’s persistence life cycle.

Page 02-3
JPA with Hibernate 3.0 The Persistence Life Cycle

Instructor Notes:

The slide doesn’t


cover entire JPA
runtime. Before we
discuss the
persistence life cycle,
we need to explain,
which instance is
responsible for entity
life cycle. Where
entities are cached?
etc.

Entity Manager:
The EntityManager is the primary interface used by application developers to
interact with the JPA runtime.

Persistence Context:
Persistence context defines a scope under which particular entity instances
are created, persisted, and removed.

Every EntityManager manages its own persistence context. In short,


persistence context is a memory area for EntityManager to work on entity
instance.

The slide diagram shows a sample JPA runtime. JPA uses EntityManager
instance to manage objects which required to be persisted. Such objects are
called Entities. Entities managed by EntityManager travels through different
life cycle phases. This lesson primarily focuses on entities persistence life
cycle.

Page 02-4
JPA with Hibernate 3.0 The Persistence Life Cycle

Instructor Notes:

The slide diagram shows different states of entity. When entity is in managed
state, the data of entity is persisted in database.

The remaining slides discusses about entity states in details.

Page 02-5
JPA with Hibernate 3.0 The Persistence Life Cycle

Instructor Notes:

New State :When an entity object is initially created its state is New. In this
state the object is not yet associated with an Entity Manager and has no
representation in the database.

Page 02-6
JPA with Hibernate 3.0 The Persistence Life Cycle

Instructor Notes:

Managed State : An entity object becomes Managed when it is persisted to


the database via an EntityManager’s persist method which must be invoked
within an active transaction. On transaction commit, the owning Entity
Manager stores the new entity object to the database.

Entity objects retrieved from the database by an EntityManager are also in the
Managed state.

If a managed entity object is modified within an active transaction the change


is detected by the owning EntityManager and the update is propagated to the
database on transaction commit.

Note: The methods shown in slide are of EntityManager interface.

Page 02-7
JPA with Hibernate 3.0 The Persistence Life Cycle

Instructor Notes:

Detached State : represents entity objects that have been disconnected from
the EntityManager. For instance, all the managed objects of an EntityManager
become detached when the EntityManager is closed.

Note: The method shown in slide are of EntityManager interface.

Page 02-8
JPA with Hibernate 3.0 The Persistence Life Cycle

Instructor Notes:

Note: The methods shown in slide are of EntityManager interface.

Page 02-9
JPA with Hibernate 3.0 The Persistence Life Cycle

Instructor Notes:

Add instructor notes


here.

Removed State : A managed entity object can also be retrieved from the
database and marked for deletion, by using the EntityManager’s remove
method within an active transaction. The entity object changes its state from
Managed to Removed, and is physically deleted from the database during
commit.

Note: The methods shown in slide are of EntityManager interface.

Page 02-10
JPA with Hibernate 3.0 The Persistence Life Cycle

Instructor Notes:

Add instructor notes


here.

Page 02-11
JPA with Hibernate 3.0 The Persistence Life Cycle

Instructor Notes:

Answers:
1. True
2. Managed

Page 02-12

You might also like