Show TOC

Entity Manager OperationsLocate this document in the navigation structure

Use

Basic Operations

Type

Method and operation

Basic Operations

void

persist(Object entity)

Make an entity instance managed and persistent.

<T> T

find(Class<T> entityClass, Object primaryKey)

Find by primary key.

<T> T

merge(T entity)

Merge the state of the entity with the current persistence context.

void

remove(Object entity)

Remove the entity instance.

Basic Query Operations

Query

createNamedQuery(String name)

Create an instance of Query for executing a named query (in JPQL or Native SQL).

Query

createQuery(String qlString)

Create an instance of Query for executing a JPQL statement.

Advanced Query Operations

Query

createNativeQuery(String sqlString, Class resultClass)

Create an instance of Query for executing a Native SQL query.

Query

createNativeQuery(String sqlString, String resultSetMapping)

Create an instance of Query for executing a Native SQL query.

Query

createNativeQuery(String sqlString)

Create an instance of Query for executing a Native SQL statement, for example, for update or delete.

FlushModeType

getFlushMode()

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

void

setFlushMode(FlushModeType flushMode)

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

Synchronization with the Database

void

flush()

Synchronize the persistence context with the underlying database.

void

refresh(Object entity)

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

Contents of the Persistence Context

boolean

contains(Object entity)

Check if the entity instance belongs to the current persistence context.

void

clear()

Clear the persistence context, causing all managed entities to become detached.

Life Cycle of the Entity Manager

void

close()

Close an application-managed Entity Manager.

boolean

isOpen()

Determine whether the Entity Manager is open.

Concurrency Control

void

lock(Object entity, LockModeType lockMode)

Set the lock mode for an entity object within the persistence context.

Transactions

EntityTransaction

getTransaction()

Returns the resource-level transaction object.

void

joinTransaction()

Indicate to the Entity Manager that a JTA transaction is active.

Other

<T> T

getReference(Class<T> entityClass, Object primaryKey)

Get an instance which allows lazy fetching of its state.

Object

getDelegate()

Return the underlying provider object for the Entity Manager, if available.