Feeds

Migrating EJB 2.1 Entity and Session Beans to EJB 3.0

The nuts and bolts

Combat fraud and increase customer satisfaction

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.

Combat fraud and increase customer satisfaction

More from The Register

next story
OpenBSD founder wants to bin buggy OpenSSL library, launches fork
One Heartbleed vuln was too many for Theo de Raadt
Got Windows 8.1 Update yet? Get ready for YET ANOTHER ONE – rumor
Leaker claims big release due this fall as Microsoft herds us into the CLOUD
This time it's 'Personal': new Office 365 sub covers just two devices
Redmond also brings Office into Google's back yard
Ubuntu 14.04 LTS: Great changes, but sssh don't mention the...
Why HELLO Amazon! You weren't here last time
Patch iOS, OS X now: PDFs, JPEGs, URLs, web pages can pwn your kit
Plus: iThings and desktops at risk of NEW SSL attack flaw
Next Windows obsolescence panic is 450 days from … NOW!
The clock is ticking louder for Windows Server 2003 R2 users
Batten down the hatches, Ubuntu 14.04 LTS due in TWO DAYS
Admins dab straining server brows in advance of Trusty Tahr's long-term support landing
Red Hat to ship RHEL 7 release candidate with a taste of container tech
Grab 'near-final' version of next Enterprise Linux next week
Apple inaugurates free OS X beta program for world+dog
Prerelease software now open to anyone, not just developers – as long as you keep quiet
prev story

Whitepapers

Mobile application security study
Download this report to see the alarming realities regarding the sheer number of applications vulnerable to attack, as well as the most common and easily addressable vulnerability errors.
3 Big data security analytics techniques
Applying these Big Data security analytics techniques can help you make your business safer by detecting attacks early, before significant damage is done.
The benefits of software based PBX
Why you should break free from your proprietary PBX and how to leverage your existing server hardware.
Securing web applications made simple and scalable
In this whitepaper learn how automated security testing can provide a simple and scalable way to protect your web applications.
Combat fraud and increase customer satisfaction
Based on their experience using HP ArcSight Enterprise Security Manager for IT security operations, Finansbank moved to HP ArcSight ESM for fraud management.