×
☰ See All Chapters

JPA EntityManager

As the name tells, EntityManager manages the entities. EntityManager manages the complete transactions of entities. EntityManager is used to perform CRUD (Create, Read, Update, Delete) operations on entities. EntityManager provides sophisticated methods to perform CRUD operations. Now let us list out and learn these methods. Below table details the methods provided by EntityManager.

JPA EntityManager methods

The methods of the EntityManager can be divided into the following functional categories:

Method

Description

Transaction Association

public EntityTransaction getTransaction();

Return the resource-level EntityTransaction object. The EntityTransaction instance may be used serially to begin and commit multiple transactions.

Entity Lifecycle Management

public void persist(Object entity);

First entity class object is assigned with an identifier (internally assigned by JPA or programmer given value). This entity class object is then persisted. Returns void.

public void remove(Object entity);

Remove the entity instance. Trying to remove an instance which is not there in database will be considered as transient object and JPA will remove this transient object.

public void refresh(Object entity);

Refresh the state of the instance from the database, overwriting changes made to the entity, if any.

public Object merge(Object entity);

Merge the state of the given entity into the current persistence context. Returns an updated persistent instance.

When we try to update an object which is not yet been assigned identity earlier, which means trying to merge fresh new entity object will persist a new record to database.

Entity Identity Management

public <T> T find(Class<T> cls, Object oid);

Return the persistent instance of the given entity class with the given identifier, or null if there is no such persistent instance.

public <T> T getReference(Class<T> cls, Object oid);

Get an instance, whose state may be lazily fetched. If the requested instance does not exist in the database, the EntityNotFoundException is thrown when the instance state is first accessed. (The persistence provider runtime is permitted to throw the EntityNotFoundException when getReference is called.) The application should not expect that the instance state will be available upon detachment, unless it was accessed by the application while the entity manager was open.

public boolean contains(Object entity);

Check if the instance is a managed entity instance belonging to the current persistence context.

Cache Management

public void flush();

Synchronize the persistence context to the underlying database.

public FlushModeType getFlushMode();

 

Get the flush mode that applies to all objects contained in the persistence context.

public void setFlushMode(FlushModeType flushMode);

Set the flush mode that applies to all objects contained in the persistence context.

public void clear();

Clear the persistence context, causing all managed entities to become detached. Changes made to entities that have not been flushed to the database will not be persisted.

Query Factory

public Query createQuery(String query);

Create an instance of Query for executing a Java Persistence query language statement.

public Query createNamedQuery(String name);

Create an instance of Query for executing a named query (in the Java Persistence query language or in native SQL).

Entity Locking

public void lock(Object entity, LockModeType mode);

Lock an entity instance that is contained in the persistence context with the specified lock mode type.

public LockModeType getLockMode(Object entity);

Get the current lock mode for the entity instance.

Retrieving Properties Information

public Map<String,Object> getProperties();

 

Get the properties and hints and associated values that are in effect for the entity manager. Changing the contents of the map does not change the configuration in effect.

Closing

public boolean isOpen();

 

Determine whether the entity manager is open.

public void close();

Close an application-managed entity manager. After the close method has been invoked, all methods on the EntityManager instance and any Query, TypedQuery, and StoredProcedureQuery objects obtained from it will throw the IllegalStateException except for getProperties, getTransaction, and isOpen (which will return false). If this method is called when the entity manager is joined to an active transaction, the persistence context remains managed until the transaction completes.

 

JPA Exceptions

jpa-entitymanager-0
 

The diagram above depicts the JPA exception architecture. All exceptions are unchecked. JPA uses standard exceptions where appropriate, most notably IllegalArgumentExceptions and IllegalStateExceptions.

 


All Chapters
Author