Java Reference
In-Depth Information
The session bean class
The session bean class is annotated with the @Stateless annotation and implements
the component interfaces:
@Stateless(name = "SessionEJBFaçade", mappedName = "EJB3-SessionEJB")
public class SessionEJBFaçadeBean implements SessionEJBFaçade,SessionE
JBFaçadeLocal { }
Inject an EntityManager into the session bean using dependency injection with the @
PersistenceContext annotation. An entity manager is used for persisting an entity
bean to a database:
@PersistenceContext(unitName = "em")
private EntityManager em;
A session bean deployed to a JAR file and packaged with a WAR file in an EAR file
has the default JNDI name of mappedName#remote_interface_name for the remote
interface. For convenient access to the JNDI name from a client class, assign a public
static final variable to the remote JNDI name:
public static final String RemoteJNDIName = "EJB3-SessionEJB#model.
SessionEJBFaçade";
The session bean is used to wrap the entity bean. In the Ajax application, we shall
be validating an input form to create a catalog entry. In the input form, we shall
specify a catalog id and the catalog id will be transferred to a servlet client via Ajax.
The servlet will invoke a session bean method to validate the catalog id. A catalog
id is valid if it is not already defined in the database and not valid if it is defined in
the database. Add a method validate(String catalogId) that returns a Catalog
object to the session bean. In the validate() method, create a Query object using a
Java persistence query language statement, which includes a named parameter
for catalogId :
Query query = em.createQuery("SELECT c from Catalog c where
c.catalogId=:catalogId");
Set the value of the named parameter using the setParameter() method:
query.setParameter("catalogId", catalogId);
Run the query statement using the getResultList() method:
List catalogEntry =query.getResultList();
 
Search WWH ::




Custom Search