RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Moving On: Migrating from EJB 2.1 to EJB 3.0 : Page 4

Find out how much you can simplify your EJB code by working through these examples, which show the process for migrating EJBs to version 3.0.

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 = 
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) 
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) 
In EJB 2.1, you remove an entity bean instance with the remove() method:

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.

   private EntityManager em;
You persist an entity bean instance by calling the EntityManager.persist() method, for example:

   BookCatalogBean catalogBean = new 
      BookCatalogBean (title);
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 = 
Finally, you remove an entity bean instance with the EntityManager.remove(Object entity) method.

   BookCatalogBean 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 servers—they may not implement all the EJB 3.0 features, so be sure to check the documentation for your application server.

Deepak Vohra is an O'Reilly technical reviewer, who reviewed the book WebLogic: The Definitive Guide. He's also a NuBean consultant and web developer, and is a Sun Certified Java 1.4 Programmer and Sun Certified Web Component Developer for J2EE.
Email AuthorEmail Author
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date