JPA - Entity (Operations|Lifecycle)

1 - About

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

The following operations are possible:

  • MERGE,

This operations must be performed in a active persistence context.

3 - Operation

3.1 - 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.


MyEntity e = new MyEntity();
// transaction starts
// transaction ends: the row for someField is updated in the database

3.2 - 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
MyEntity attachedEntity = em.merge(notAttachedEntity); 
// transaction ends: the row for someField is NOT updated in the database because you used e in place of the copy e2.

3.3 - Flush

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

3.4 - 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.

3.5 - Detach

3.6 - Remove

4 - Documentation / Reference

Data Science
Data Analysis
Data Science
Linear Algebra Mathematics

Powered by ComboStrap