Feeds

Migrating EJB 2.1 Entity and Session Beans to EJB 3.0

The nuts and bolts

Choosing a cloud hosting partner with confidence

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.

Top 5 reasons to deploy VMware with Tegile

More from The Register

next story
Download alert: Nearly ALL top 100 Android, iOS paid apps hacked
Attack of the Clones? Yeah, but much, much scarier – report
You stupid BRICK! PCs running Avast AV can't handle Windows fixes
Fix issued, fingers pointed, forums in flames
Microsoft: Your Linux Docker containers are now OURS to command
New tool lets admins wrangle Linux apps from Windows
Facebook, working on Facebook at Work, works on Facebook. At Work
You don't want your cat or drunk pics at the office
Soz, web devs: Google snatches its Wallet off the table
Killing off web service in 3 months... but app-happy bonkers are fine
First in line to order a Nexus 6? AT&T has a BRICK for you
Black Screen of Death plagues early Google-mobe batch
Microsoft adds video offering to Office 365. Oh NOES, you'll need Adobe Flash
Lovely presentations... but not on your Flash-hating mobe
prev story

Whitepapers

Why and how to choose the right cloud vendor
The benefits of cloud-based storage in your processes. Eliminate onsite, disk-based backup and archiving in favor of cloud-based data protection.
Getting started with customer-focused identity management
Learn why identity is a fundamental requirement to digital growth, and how without it there is no way to identify and engage customers in a meaningful way.
10 threats to successful enterprise endpoint backup
10 threats to a successful backup including issues with BYOD, slow backups and ineffective security.
High Performance for All
While HPC is not new, it has traditionally been seen as a specialist area – is it now geared up to meet more mainstream requirements?
The hidden costs of self-signed SSL certificates
Exploring the true TCO for self-signed SSL certificates, including a side-by-side comparison of a self-signed architecture versus working with a third-party SSL vendor.