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.
by Deepak Vohra
May 15, 2006
Page 1 of 4
efore showing you how to migrate your EJBs from EJB 2.1 to EJB 3.0, it's worth exploring what you gain by making the move. Basically, EJB 3.0 reduces the number of classes, interfaces, and deployment descriptors needed to create EJBs. The EJB 3.0 specification simplifies EJB development by letting you replace abstract bean classes with Plain Old Java Objects (POJOs) and replace Component and Home interfaces with Plain Old Java Interfaces (POJIs). The latter are optionalyou don't have to include them at all.
The deployment descriptor ejb-jar.xml, which specifies the EJB name, the bean class name, the interfaces, the finder methods, the container-managed relationships (CMR), and any vendor-specific deployment descriptors are no longer required; they're replaced by metadata annotations in the bean classes. That's why you need JDK 5.0 to develop EJB 3.0 EJBsbecause they use annotations, which weren't available until JDK 5.0.
EJB 3.0 replaces the create() and finder methods of EJB 2.1 with the javax.persistence.EntityManager API. Client applications for EJB 2.1 obtain a reference to entity and session bean objects using the JNDI name, while EJB 3.0 clients obtain them using the @Resource, @Inject, and @EJB annotations.
In EJB 2.1, you developed entity and session beans using the javax.ejb package classes and interfaces, where a session bean implements the SessionBean interface and an entity bean implements the EntityBean interface. In contrast, EJB 3.0 session and entity bean classes are POJOs, and do not implement the SessionBean and EntityBean interfaces.
An EJB 2.1 session bean class specifies one or more ejbCreate methods, callback methods, the setSessionContext method, and business methods. Similarly, an EJB 2.1 entity EJB specifies the ejbCreate(), ejbPostCreate(), callback, Container-managed persistence (CMP), getter and setter CMR, and getter, setter, and business methods. An EJB 3.0 session bean class specifies only the business methods. Similarly, an EJB 3.0 entity bean specifies business methods, getter/setter methods for the different bean properties, and getter/setter methods for bean relationships.
The EJB 2.1 home interface extends the javax.ejb.EJBHome interface, and a local home interface extends the javax.ejb.EJBLocalHome interface. The EJB 2.1 remote interface extends the javax.ejb.EJBObject interface, and a local interface extends the javax.ejb.EJBLocalObject interface. EJB 3.0 doesn't specify the component and home interfacesthey're replaced with POJI business interfaces. If a session bean class doesn't specify a business interface, the EJB server generates a POJI business interface from the session bean class.
With these changed features in mind, the rest of this article concentrates on showing you the details you need to know to migrate an example session bean and an example entity bean from EJB 2.1 to EJB 3.0.