Feeds

Migrating EJB 2.1 Entity and Session Beans to EJB 3.0

The nuts and bolts

Build a business case: developing custom apps

Listing 2. CatalogBean.java EJB 3.0 Session Bean

@Stateless
@Local ({CatalogLocal.java})

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

In EJB 3.0, the component and home interfaces of EJB 2.1 are replaced with a business interface. The business interfaces for the session bean are POJIs, and do not extend the EJBLocalObject or the EJBObject. A local business interface is denoted with the annotation @Local. A remote business interface is denoted with the annotation @Remote. A remote business interface does not throw the RemoteException. The local business interface corresponding to the session bean class is listed in Listing 3.

Listing 3. CatalogLocal.java. Local Interface of Session Bean

@Local

public interface CatalogLocal{
    public String getJournal(String publisher);
}

A client for an EJB 2.1 session bean gets a reference to the session bean with JNDI. The JNDI name for the CatalogBean session bean is CatalogLocalHome. The local/remote object is obtained with the create() method. The client class for the EJB 2.1 session bean is listed in Listing 4.

Listing 4. EJB 2.1 Session Bean Client Class

import javax.naming.InitialContext;

public class CatalogClient {

    public static void main(String[] argv) {
        try {
            InitialContext ctx=new InitialContext();
            Object objref=ctx.lookup("CatalogLocalHome");
            CatalogLocalHome catalogLocalHome=(CatalogLocalHome)objref;
            CatalogLocal catalogLocal=(CatalogLocal)catalogLocalHome.create();
            String publisher="OReilly";
            String journal=catalogLocal.getJournal(publisher);
            System.out.println("Journal for Publisher: "+publisher +" "+journal);
        }
        catch (Exception e) {}
    }
}

In EJB 3.0 a reference to a resource is obtained with dependency injection with the @Inject annotation or the @Resource annotation. JNDI lookup and create() method invocation is not required in EJB 3.0. The client class for the EJB 3.0 session bean is listed in Listing 5.

Boost IT visibility and business value

More from The Register

next story
The Return of BSOD: Does ANYONE trust Microsoft patches?
Sysadmins, you're either fighting fires or seen as incompetents now
Microsoft refuses to nip 'Windows 9' unzip lip slip
Look at the shiny Windows 8.1, why can't you people talk about 8.1, sobs an exec somewhere
Munich considers dumping Linux for ... GULP ... Windows!
Give a penguinista a hug, the Outlook's not good for open source's poster child
Intel's Raspberry Pi rival Galileo can now run Windows
Behold the Internet of Things. Wintel Things
Linux Foundation says many Linux admins and engineers are certifiable
Floats exam program to help IT employers lock up talent
Microsoft cries UNINSTALL in the wake of Blue Screens of Death™
Cache crash causes contained choloric calamity
Eat up Martha! Microsoft slings handwriting recog into OneNote on Android
Freehand input on non-Windows kit for the first time
prev story

Whitepapers

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.
7 Elements of Radically Simple OS Migration
Avoid the typical headaches of OS migration during your next project by learning about 7 elements of radically simple OS migration.
BYOD's dark side: Data protection
An endpoint data protection solution that adds value to the user and the organization so it can protect itself from data loss as well as leverage corporate data.
Consolidation: The Foundation for IT Business Transformation
In this whitepaper learn how effective consolidation of IT and business resources can enable multiple, meaningful business benefits.
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?