Java Reference
In-Depth Information
Method signature
Description
public <T> T find(Class<T> entityClass, Object
primaryKey);
Finds an entity instance by its primary key. This method is
overloaded and comes in different forms.
Synchronizes the state of entities in the EntityManager's
persistence context with the database.
public void flush();
Changes the flush mode of the EntityManager's persistence
context. The flush mode may be AUTO or COMMIT. The
default flush mode is AUTO, meaning that the EntityMan-
ager tries to automatically sync the entities with the data-
base.
public void setFlushMode(FlushModeType flushMode);
public FlushModeType getFlushMode();
Retrieves the current flush mode.
Refreshes (resets) the entities from the database. This meth-
od is overloaded and comes in different forms.
public void refresh(Object entity);
Creates a dynamic query using a JPQL statement. This
method is overloaded and comes in different forms.
public Query createQuery(String jpqlString);
Creates a query instance based on a named query on the en-
tity instance. This method is overloaded and comes in dif-
ferent forms.
public Query createNamedQuery(String name);
Creates a dynamic query using a native SQL statement.
This method is overloaded and comes in different forms.
public Query createNativeQuery(String sqlString);
Creates a StoredProcedureQuery for executing a stored pro-
cedure. This method is overloaded and comes in different
forms.
public StoredProcedureQuery createStoredProced-
ureQuery(String procedureName);
public void close();
Closes an application-managed EntityManager.
Detached all managed entities from the persistence context.
All changes made to entities not committed will be lost.
public void clear();
public boolean isOpen();
Checks whether an EntityManager is open.
Retrieves a transaction object that can be used to manually
start or end a transaction.
public EntityTransaction getTransaction();
public void joinTransaction();
Asks an EntityManager to join an existing JTA transaction.
Don't worry too much if the methods aren't immediately obvious. Except for the methods
related to the query API ( createQuery , createNamedQuery , and createNat-
iveQuery ), we'll discuss them in detail in the coming sections. The few EntityMan-
ager interface methods that we didn't cover are rarely used, so we won't spend time dis-
cussing them. Once you've read and understood the material in this chapter, though, we
encourage you to explore the methods on your own. The EJB 3 Java Persistence API 2.1
final specification is available at http://jcp.org/en/jsr/detail?id=338 .
Even though JPA isn't container-centric like session beans or MDBs, entities still have a
lifecycle. This is because they're “managed” by JPA in the sense that the persistence pro-
vider keeps track of them under the hood and even automatically synchronizes the entity
Search WWH ::




Custom Search