Table of Contents

JPA - Entity (Operations|Lifecycle)

About

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

The following operations are possible:

This operations must be performed in a active persistence context.

Operation

Persist

A new entity instance becomes both:

by invoking :

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:

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