JPA - Entity (Operations|Lifecycle)

Card Puncher Data Processing

About

All entity operations are described in the chapter 3: Entity Operations of the specification

The following operations are possible:

  • PERSIST,
  • MERGE,
  • REMOVE,
  • REFRESH,
  • DETACH
  • FLUSH

This operations must be performed in a active persistence context.

Operation

Persist

A new entity instance becomes both:

  • and persistent (adds it to the context):

by invoking :

  • the persist method on it
  • or by cascading the persist operation.

Example

MyEntity e = new MyEntity();
// transaction starts
em.getTransaction().begin();
em.persist(e); 
e.setSomeField(someValue); 
em.getTransaction().commit();
// transaction ends: the row for someField is updated in the database

Merge

The merge operation allows for the propagation of state from detached entities onto persistent entities managed by the entity manager.

If X is a:

  • new entity instance, a new managed entity instance X' is created and the state of X is copied into the new managed entity instance X'.
  • detached entity, the state of X is copied onto a pre-existing managed entity instance X' of the same identity or a new managed copy X' of X is created.
  • a managed entity, it is ignored by the merge operation, however, the merge operation is cascaded to entities referenced by relationships from X if these relationships have been annotated with the cascade element value cascade=MERGE or cascade=ALL annotation.

The instance you pass in will not be managed (any changes you make will not be part of the transaction - unless you call merge again). Example:

MyEntity notAttachedEntity = new MyEntity();
// transaction starts
em.getTransaction().begin();
MyEntity attachedEntity = em.merge(notAttachedEntity); 
notAttachedEntity.setSomeField(someValue); 
em.getTransaction().commit();
// transaction ends: the row for someField is NOT updated in the database because you used e in place of the copy e2.

Flush

The flush method can be used by the application to force synchronization.

Refresh

Synchronization to the database does not involve a refresh of any managed entities unless the refresh operation is explicitly invoked on those entities or cascaded to them as a result of the specification of the cascade=REFRESH or cascade=ALL annotation element value.

Detach

JPA - Detached Entity

Remove

JPA - Removed Instance

Documentation / Reference





Discover More
Card Puncher Data Processing
JPA - Cascade Type

The cascade parameter on a relationship defines the entity operation that must be cascaded to the target of the association. See javax/persistence/CascadeTypecascadeType For all entities Y referenced...
Card Puncher Data Processing
JPA - Database Synchronization

The Database synchronization involves writing to the database any updates to persistent entities and their relationships. The state of persistent entities is (synchronized to|entered into) the database:...
Card Puncher Data Processing
JPA - Detached Entity

A detached entity results from: transaction commit if a transaction-scoped container-managed entity manager is used from transaction rollback from detaching the entity from the persistence context;...
Card Puncher Data Processing
JPA - Managed Entity

future updates to the entity will be tracked. persistence context The contains() method can be used to determine whether an entity instance is managed in the current persistence context. The...
Card Puncher Data Processing
JPA - Persistence Context

A persistence context is a set of entity instances in which for any persistent entity identity there is a unique entity instance. Within the persistence context, the entity instances and their operations...
Card Puncher Data Processing
JPA - Removed Instance

A managed entity instance becomes removed by: invoking the remove method on it or by cascading the remove operation. If X is a: new entity, it is ignored by the remove operation. However, the...
Card Puncher Data Processing
JPA - Transaction

The managed entities of: a transaction-scoped persistence context become detached when the transaction commits. an extended persistence context remain managed. For both transaction-scoped and...



Share this page:
Follow us:
Task Runner