Java Reference
In-Depth Information
makes an entity bean persistent,
remove()
deletes an entity bean, and
find()
retrieves an entity bean by its primary key.
The
Query
interface is equivalent to the Hibernate or
JDO
Query
interfaces. It
defines methods for executing queries, bulk deletes, and bulk updates. An appli-
cation creates a
Query
by calling factory methods defined by the
EntityManager
such as
createQuery()
, which creates a
Query
from a query string, and
create-
NamedQuery()
, which creates one from a named query. The
Query
interface defines
several methods, including
setParameter()
, which sets a query parameter;
get-
ResultList()
, which executes a query and returns a list of results; and
execute-
Update()
, which executes an update or delete statement. For example, here is a
code fragment that shows how an application can execute an
EJB 3
named query:
EntityManager entityManager = …;
Query query = entityManager
.createNamedQuery("Restaurant.findAvailableRestaurants");
…
query.setParameter("dayOfWeek", new Integer(dayOfWeek));
query.setParameter("hour", new Integer(hour));
query.setParameter("minute", new Integer(minute));
query.setParameter("zipCode", new Integer(zipCode));
List result = query.getResultList();
The application calls
EntityManager.createNamedQuery()
to create the query, sets
some parameters, and executes the query by calling
Query.getResultList()
.
The
EntityManagerFactory
interface is equivalent to a Hibernate
Session-
Factory
or a
JDO
PersistenceManagerFactory
. It has a
createEntityManager()
method that creates an
EntityManager
.
J2EE
applications will typically use depen-
dency injection to access an
EntityManager
, but client-side code, most notably test
cases, needs to explicitly create an
EntityManager
using the
EntityManager-
Factory
. Client-side code creates an
EntityManagerFactory
by calling
Persis-
tence.createEntityManagerFactory()
, which is a static method.
The
EntityTransaction
interface is equivalent to the
Transaction
interfaces
provided by Hibernate and
JDO
. The application gets the
EntityTransaction
from the
EntityManager
and calls its methods to control transactions.
J2EE
appli-
cations will most likely use container-managed transactions, but client-side tests
will use
EntityTransaction
.
As you can see, the
EJB 3
persistence interface is completely different from the
EJB 2
equivalent. It's very similar to
JDO
and Hibernate
API
s. Later in this chapter,
you will see examples of how to use it. Let's now look at
EJB 3
detached objects.
Search WWH ::
Custom Search