Java Reference
In-Depth Information
The session bean class
The CatalogSessionEJBBean class is annotated with the annotation @Stateless .
The mappedName attribute specifies the global JNDI for the session bean. We will use
the mapped name in the test client to lookup the session bean and invoke method(s)
on it. The @Remote annotation indicates that the session bean is a remote interface.
@Stateless(name = "CatalogSessionEJB", mappedName = "EJB3-JSF-
public class CatalogSessionEJBBean implements CatalogSessionEJB { }
In the session bean an EntityManager is injected using the @PersistenceContext
annotation. The unitName is specified, but not required, as the EntityManager
variable name is the same as the persistence unit name:
@PersistenceContext(unitName = "em")
private EntityManager em;
Add a method persistEntity() and a method findEntity() to the session bean
and the remote interface. A method may be added to a session bean by selecting the
session bean node in the Application Navigator , and in the Structure view, right-click
on the session bean node and select EJB (N)|New Method . Similarly, a session bean
field may be added by selecting EJB(N)|New Field . The persistEntity() method
defines parameters for attributes of the Catalog entity class and persists an entity
instance to the database. The findEntity() method finds an entity instance. In the
persistEntity() method, create an Catalog entity instance:
Catalog catalog =new Catalog(author, edition, new Integer(catalogId),
journal,publisher, title);
Invoke the persist(Object) method of EntityManager to persist the
entity bean instance:
The find() method defines a catalogId parameter and finds an entity instance. In
the find() method, create an instance of the Query object using the createQuery()
method to run a Java persistence query language statement. Bind the catalogId to
a named parameter id using the setParameter() method of the Query object and
run the Java persistence query statement using the getResultList() method, which
returns a List :
List catalogEntry =em.createQuery(
"SELECT c from Catalog c
Search WWH ::

Custom Search