Java Reference
In-Depth Information
As we can see, AbstractFacade is not much more than a facade to EntityManager .
Wrapping its calls inside a session bean gives us all of its advantages, such as
transaction management and distributed code. The generated create() method is
used to create new entities, the edit() method updates an existing entity, and the
remove() method deletes existing entities. The find() method finds an entity with
the given primary key, and the findAll() method returns a list of all entities in the
database. The findRange() method allows us to retrieve a subset of the entities in
the database; it takes an array of integers as its sole parameter. The first element in
this array should have the index of the first result to retrieve, and the second element
should have the index of the last element to retrieve. The count() method returns
the number of entities in the database; it is similar to a select count(*) from
TABLE_NAME statement in standard SQL.
As we mentioned previously, all of the generated session beans extend
AbstractFacade . Let's look at one of these generated EJBs:
package com.ensode.ejbdao.sessionbeans;
import com.ensode.ejbdao.entities.Customer;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
@Stateless
public class CustomerFacade extends AbstractFacade<Customer> {
@PersistenceContext(unitName = "EjbDaoPU")
private EntityManager em;
@Override
protected EntityManager getEntityManager() {
return em;
}
public CustomerFacade() {
super(Customer.class);
}
}
As we can see, the generated session beans are very simple. They simply include an
instance variable of type EntityManager and take advantage of resource injection to
initialize it. They also include a getEntityManager() method meant to be called by
the parent class so that it has access to this session bean's EntityManager instance.
Additionally, the session bean's constructor invokes the parent class' constructor,
which via generics initializes the entityClass instance variable on the parent class.
 
Search WWH ::




Custom Search