Java Reference
In-Depth Information
In EJB 3.0, persistence and lookup are provided by the EntityManger class. In a
session bean client class for the EJB 3.0 entity bean, dependency injection is used to
inject an EntityManager object using the @PersistenceContext annotation:
@PersistenceContext
private EntityManager em;
An entity bean instance is created by invoking new on the CatalogBean class and
persisted with the persist() method of the EntityManager class:
CatalogBean catalogBean=new CatalogBean(catalogId);
em.persist(catalogBean);
An entity bean instance is obtained with the find() method:
CatalogBean catalogBean=(CatalogBean)em.find("CatalogBean",
catalogId);
A Query object for a finder method is obtained with the createNamedQuery method:
Query query=em.createNamedQuery("findByJournal");
An entity bean instance is removed with the remove() method of the
EntityManager class:
CatalogBean catalogBean;
em.remove(catalogBean);
The client class for the EJB 3.0 entity bean is listed next:
import javax.ejb.Stateless;
import javax.ejb.Resource;
import javax.persistence.EntityManager;
import javax.persistence.Query;
@Stateless
public class CatalogClient implements CatalogLocal {
@Resource
private EntityManager em;
public void create(String catalogId) {
CatalogBean catalogBean = new CatalogBean(catalogId);
em.persist(catalogBean);
}
public CatalogBean findByPrimaryKey(String catalogId) {
return (CatalogBean) em.find("CatalogBean", catalogId);
}
public void remove(CatalogBean catalogBean) {
 
Search WWH ::




Custom Search