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, the
remove() method deletes an existing entity. 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 int 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 in standard SQL.
Like we previously mentioned, 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;
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.
We are of course free to add additional methods to the generated session beans. For
example, sometimes it is necessary to add a method to find all entities that meet
specific criteria, such as finding all customers with the same last name.
 
Search WWH ::




Custom Search