Migrating EJB Entity Bean Clients
You create an EJB 2.1 entity bean home or local home object with the
create() method in the entity bean home or local home interface. A client for an EJB 2.1 entity bean obtains a local or remote object for the entity bean with JNDI lookup. Here's an example code snippet that creates an EJB 2.1 entity bean local home object.
InitialContext ctx=new InitialContext();
Object objref=ctx.lookup("BookCatalogLocalHome");
BookCatalogLocalHome catalogLocalHome =
(BookCatalogLocalHome)objref;
In the code snippet above,
BookCatalogLocalHome is the JNDI name of the BookCatalogBean entity bean.
After obtaining the reference, EJB 2.1 clients create a local object created with the
create() method.
BookCatalogLocal catalogLocal = (BookCatalogLocal)
catalogLocalHome.create(title);
In EJB 2.1, you obtain a local or remote object from a local home or home object with the finder methods. For example, you can obtain a local object with the
findByPrimaryKey method as shown below.
BookCatalogLocal catalogLocal = (BookCatalogLocal)
catalogLocalHome.findByPrimaryKey(title);
In EJB 2.1, you remove an entity bean instance with the
remove() method:
catalogLocal.remove();
EJB 3.0 implements persistence, lookup, and removal through the javax.persistence.EntityManager class. Table 2 lists some of the commonly used methods in the EntityManager class that replace the EJB 2.1 methods.
Table 2. EntityManager Class Methods: The table shows commonly used EntityManager methods and their descriptions.
| EntityManager Method |
Description |
| persist(Object entity) |
Makes an entity bean instance persistent. |
| createNamedQuery(String name) |
Creates an instance of Query object to execute a named query. |
| find(Class entityClass, Object primaryKey) |
Finds a entity bean instance. |
| createQuery(String ejbQl) |
Creates a Query object to run an EJBQL query. |
| remove(Object entity) |
Removes an entity bean instance. |
In a client class for an EJB 3.0 entity bean, you inject the EntityManager object using the
@Resource annotation.
@Resource
private EntityManager em;
You persist an entity bean instance by calling the
EntityManager.persist() method, for example:
BookCatalogBean catalogBean = new
BookCatalogBean (title);
em.persist(catalogBean);
Similarly, you obtain an entity bean instance by calling the
EntityManager.find() method.
BookCatalogBean catalogBean = (BookCatalogBean)
em.find("BookCatalogBean", title);
You can define an EJB 3.0 client class finder method (not the same as an EJB 2.1 finder method) corresponding to the named query
findByTitle. In the finder method, obtain a Query object with the
createNamedQuery(String) method.
Query query=em.createNamedQuery("findByTitle");
Set the Query object parameters with the
setParameter(int paramPosition, String paramValue) method or the
setParameter(String parameterName, String value) method. The parameter position is zero-based.
query.setParameter(0, title);
Obtain a Collection of the BookCatalogBean objects with the
Query.getResultList() method. If a query returns a single result, use the
getSingleResult() method instead.
java.util.Collection catalogBeanCollection =
(BookCatalogBean)query.getResultList();
Finally, you remove an entity bean instance with the
EntityManager.remove(Object entity) method.
BookCatalogBean catalogBean;
em.remove(catalogBean);
Listing 4 shows the complete stateless session bean client class for the EJB 3.0 entity bean.
In this article, you've seen an example of the code changes needed to migrate an example session bean and an example entity bean from EJB 2.1 to EJB 3.0. Migration from EJB 2.0 is similar.
As of this writing, several application servers already support the EJB 3.0 specification, including the
JBoss application server, the
Oracle application server, and the
Caucho application server. Unfortunately, the implementations may vary among the different application serversthey may not implement all the EJB 3.0 features, so be sure to check the documentation for your application server.