The lifecycle of an entity bean

Entities are managed by the instances of javax.entity.EntityManager in the Java application. Entities have a lifecycle of four states: new, managed, detached, and removed.

The new entity state

An entity bean is in the new state when it is instantiated with the Java new operator, or serialized from the object stream. In the new state, entity bean is not associated with any EntityManager instance whatsoever. An alternative way to specify this is to say that the entity bean has no persistence context, and therefore, no persistence identity.

The managed entity state

An entity bean is in the managed state when it is associated with an EntityManager instance and that instance is still valid in terms of the JVM. A managed entity bean has a definite persistence context and identity.

The detached entity state

An entity bean is in the detached state when it is no longer associated with an EntityManager. A detached entity can be found if the entity bean is serialized into an object stream so that it is transferred across the network from one JVM to another. A detached entity can be found if its associated EntityManager goes out of scope.

The detached entity state

Fig: Diagram of entity Lifecycle states

A detached entity bean can be reattached with EntityManager during a merge operation, as we will see later in the discussion around management.

The removed entity state

An entity bean is in the removed state when it has been flagged for removal from the database. The entity bean is deleted from the database, and the mapped row is deleted from the table row when the enclosing transaction successfully commits.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.80.45