Feeds

Migrating EJB 2.1 Entity and Session Beans to EJB 3.0

The nuts and bolts

Securing Web Applications Made Simple and Scalable

In this tutorial, migration of EJB 2.1 specification EJBs to EJB 3.0 specification is discussed. EJB 3.0 Session and Entity beans require JDK 5.0 as metadata annotations are used in the specification.

The EJB 2.1 specification is implemented in the javax.ejb package classes and interfaces. A session bean is required to implement the SessionBean interface.

An EJB 3.0 session bean class is a POJO (Plain Old Java Object – Martin Fowler) and does not implement the SessionBean interface.

An EJB 2.1 session bean class includes one or more ejbCreate methods, the callback methods ejbActivate, ejbPassivate, ejbRemove, and setSessionContext, and the business methods defined in the local/remote interface.

An EJB 3.0 session bean class includes only the business methods.

A home interface in an EJB 2.1 session EJB extends the javax.ejb.EJBHome interface; a local home interface extends the javax.ejb.EJBLocalHome interface. A remote interface in an EJB 2.1 Session EJB extends the javax.ejb.EJBObject interface; a local interface extends the javax.ejb.EJBLocalObject interface. In EJB 3.0 the home/local home and remote/local interfaces are not required. The EJB interfaces are replaced with a POJI (Plain Old Java Interface) business interface. If a business interface is not included with the session bean class, a POJI business interface gets generated from the session bean class by the EJB server.

An EJB 2.1 session EJB includes a deployment descriptor that specifies the EJB name, the bean class name, and the interfaces. The deployment descriptor also specifies the bean type of Stateless/Stateful. In EJB 3.0 a deployment descriptor is not required for a session bean.

An EJB 2.1 Entity EJB bean class implements the EntityBean class and the callback methods in the interface. An EJB 2.1 entity bean also includes the ejbCreate and ejbPostCreate methods, which are not required in the EJB 3.0 specification. An entity bean includes the component and home interfaces that extend the EJBObject/EJBLocalObject and EJBHome/EJBLocalHome interfaces.

In comparison, an EJB 3.0 entity bean class is a POJO which does not implement the EntityBean class. The callback methods, the ejbCreate and ejbPostCreate methods are not required in the EJB 3.0 entity bean class. Also, the component and home interfaces and deployment descriptors are not required in EJB 3.0.

The values specified in the EJB 2.1 deployment descriptor are included in EJB 3.0 bean class with JDK 5.0 annotations.

Thus, the number of classes/interfaces/deployment descriptors is reduced in the EJB 3.0 specification. In this tutorial we shall migrate an example Session EJB and an example Entity EJB from EJB 2.1 to EJB 3.0 specification.

Introduction

The purpose of the Enterprise JavaBeans EJB 3.0 specification is to improve the EJB architecture by reducing its complexity from the developer's point of view.

EJB 3.0 is implemented by different application servers such as those from JBoss, Oracle and Caucho.

The implementation in the different application servers may vary. In this tutorial an example session bean and an example entity bean are migrated from EJB 2.1 to EJB 3.0 specification. Migration from EJB 2.0 is similar.

Migrating a Session Bean

In this section we shall migrate an example stateless session bean from EJB 2.1 specification to EJB 3.0 specification. The session bean class implements the SessionBean interface. The session bean class has a ejbCreatemethod, a business method, getJournal(), and the callback methods. The example session bean class CatalogBean.java is listed in Listing 1.

Listing 1. CatalogBean.java EJB 2.1 Session Bean

import javax.ejb.SessionBean;
import javax.ejb.SessionContext;

public class CatalogBean implements SessionBean{
   private SessionContext ctx;

   public String getJournal(String publisher){
      if (publisher.equals("Oracle Publisher"))
         return new String("Oracle Magazine");
      if (journal.equals("OReilly"))
         return new String("dev2dev");
   }

   public void ejbCreate(){}
   public void ejbRemove() {}
   public void ejbActivate() {}
   public void ejbPassivate() {}
   public void setSessionContext(SessionContext ctx) {this.ctx=ctx;}
}

In EJB 3.0, metadata annotations are used to specify the session bean type and local and remote business interfaces. A stateless session bean is specified with annotation @Stateless, a stateful session bean with annotation @Stateful. Component and home interfaces are not required for a session bean. A session bean is required to implement a business interface. The business interface, which is a POJI, may be a local or remote interface. A local interface is denoted with annotation @Local and a remote interface is denoted with annotation @Remote. A session bean may implement one or both (local and remote) of the interfaces. If none of the interfaces is specified, a local business interface gets generated. The remote and local business interface class may be specified in the @Local and @Remote annotations. For example, a local business interface may be specified as, @Local ({CatalogLocal.class})

The EJB 3.0 session bean corresponding to the EJB 2.1 stateless session bean is annotated with the metadata annotation @Stateless. The EJB 3.0 bean class does not implement the SessionBean interface. The EJB 3.0 session bean implements a business interface. The @Local annotation specifies the local business interface for the session bean. The EJB 3.0 session bean corresponding to the EJB 2.1 example session bean is listed in Listing 2.

Bridging the IT gap between rising business demands and ageing tools

More from The Register

next story
NO MORE ALL CAPS and other pleasures of Visual Studio 14
Unpicking a packed preview that breaks down ASP.NET
Secure microkernel that uses maths to be 'bug free' goes open source
Hacker-repelling, drone-protecting code will soon be yours to tweak as you see fit
KDE releases ice-cream coloured Plasma 5 just in time for summer
Melty but refreshing - popular rival to Mint's Cinnamon's still a work in progress
Cheer up, Nokia fans. It can start making mobes again in 18 months
The real winner of the Nokia sale is *drumroll* ... Nokia
Put down that Oracle database patch: It could cost $23,000 per CPU
On-by-default INMEMORY tech a boon for developers ... as long as they can afford it
Another day, another Firefox: Version 31 is upon us ALREADY
Web devs, Mozilla really wants you to like this one
Google shows off new Chrome OS look
Athena springs full-grown from Chromium project's head
prev story

Whitepapers

Designing a Defense for Mobile Applications
Learn about the various considerations for defending mobile applications - from the application architecture itself to the myriad testing technologies.
Implementing global e-invoicing with guaranteed legal certainty
Explaining the role local tax compliance plays in successful supply chain management and e-business and how leading global brands are addressing this.
Top 8 considerations to enable and simplify mobility
In this whitepaper learn how to successfully add mobile capabilities simply and cost effectively.
Seven Steps to Software Security
Seven practical steps you can begin to take today to secure your applications and prevent the damages a successful cyber-attack can cause.
Boost IT visibility and business value
How building a great service catalog relieves pressure points and demonstrates the value of IT service management.