EJB 2.0 vs EJB 3.0

difference between ejb2 and ejb3 and How to convert EJB 2.0 to EJB 3.0
JohenCorner Profile Pic
JohenCorner,France,Professional
Published Date:02-08-2017
Your Website URL(Optional)
Comment
Converting an EJB 2.0 Entity to an EJB 3.0 Entity The Enterprise JavaBeans (EJB) 3.0 specification has facilitated the development of EJBs by providing an annotations-based API in which the remote/local and home/ local home interfaces are not required. The deployment descriptors that form the basis of an EJB 2.0 entity bean are also not required for deploying an EJB 3.0 entity bean. In Chapter 1, we discussed the new features in EJB 3.0. This chapter covers the procedure to convert an EJB 2.0 Entity to an EJB 3.0 Entity. The EJB 3.0 entity bean classes are simplified in comparison to the EJB 2.0 specification classes. The EJB 3.0 entity class is a Plain Old Java Object (POJO) instead of a class implementing theEntityBean interface. The component interfaces in EJB 2.0, which were required to extend theEJBLocalObject/EJBObject and home interfaces, which were required to extend theEJBLocalHome/EJBHome interfaces, are replaced with thejavax.persistence.EntityManager API to create, find, and update entity bean instances. Setting the environment We shall be creating an EJB 3.0 entity by transforming the EJB 2.0 deployment descriptor (ejb-jar.xml) using XSL stylesheets. This chapter uses the built-in XSL transformation tool in Oracle JDeveloper 11g to provide the XSLT transformation for converting the EJB 2.0 entity deployment descriptor to the EJB 3.0 entity class. Download and install Oracle JDeveloper 11g fromhttp://www.oracle.com/ technology/software/products/middleware/index.html. aConverting an EJB 2.0 Entity to an EJB 3.0 Entity Adding an Application First, create a new application in JDeveloper. To do so, click on New Application, as shown next: In the New Application wizard, specify an Application Name (for example EJB2toEJB3). Select Application Template as Generic Application and click on Next, as shown in the following screenshot: 24 aChapter 2 Next, specify a Project Name (EJB2toEJB3 for example). Select XML as a project technology and click on Finish. An application and a project get added to the Application Navigator, as shown next: 25 aConverting an EJB 2.0 Entity to an EJB 3.0 Entity Creating an XML deployment descriptor Next, we create an XML document for the EJB 2.0 entity deployment descriptor, which is to be converted to anEJB 3.0 entity. We shall also create EJB 3.0 Session bean façade classes from the deployment descriptor. Select File New and in the New Gallery window select Categories:General XML. From the Items: window, select XML Document and click on OK. In the Create XML File window, specify a File Name (ejb-jar-modified.xml). We shall be using a slightly modified ejb-jar.xml, as the deployment descriptor does not contain all the required information to convert an EJB 2.0 entity to an EJB 3.0 entity. Click on OK. 26 aChapter 2 Creating stylesheets We also need to create the XSL stylesheets to transform the EJB 2.0 entity deployment descriptor to EJB 3.0 entity class and façade classes. In the New Gallery window, select Categories:General XML and Items:XSL Style Sheet and click on OK. In the Create XSL File window specify an XSL File Name (entity-bean.xsl) and click on OK. The XSL version is set to XSL 2.0 by default. 27 aConverting an EJB 2.0 Entity to an EJB 3.0 Entity The XSL stylesheetentity-bean.xsl gets added to theEJB2toEJB3 project. The following illustration shows the stylesheet copied from a later listing in this chapter. Similarly, add XSL stylesheetsfaçade.xsl andfaçade-bean.xsl to generate EJB 3.0 façade classes with. 28 aChapter 2 To test that the EJB 3.0 entity classes generated using XSL transformation comply with the EJB 3.0 API, add libraries EJB 3.0, Java EE 1.5, and Java EE 1.5 API to the project in the Project Properties window. First, select Tools Project Properties. Subsequently, add the required libraries with the Add Library button. These libraries are pre- cong fi ured in JDeveloper and only need to be selected and added. Click on OK. Converting the entity class The EJB 3.0 entity class is a non-abstract POJO class with implementations for the getter/setter methods, in comparison to the EJB 2.0 entity bean class, which is abstract with abstract getter/setter methods. EJB 3.0 does not require component and home interfaces. The entity bean class may implement a business interface, though is not required to. 29 aConverting an EJB 2.0 Entity to an EJB 3.0 Entity In the EJB 3.0 specification, the EntityManager class is used to create, find, and update an entity bean instance. In EJB 3.0, deployment descriptors are not required and are replaced with metadata annotations. When deployment descriptors are supplied, their values override annotations. An entity bean is specified with the Entity annotation. The table name, column name, and primary key column properties are specified with the metadata annotations listed in the following table: Annotation Description Annotation Elements Table Specifies the table used for Name (if the name element is not entity bean persistence. specified, the EJB class name is used as the table name). Column Specifies a column Name, nullable, length, updatable, corresponding to an entity and unique. bean property. Id Specifies a primary key column property. Transient Specifies a property that is not persistent. EJB Query Language (QL) queries in the EJB 2.0 specification are specified with the query/ element in theejb-jar.xml deployment descriptor. EJB QL queries in EJB 3.0 are specified with the metadata annotations NamedQuery andNamedQueries, which are listed in the following table: Annotation Description Annotation Elements NamedQueries Specifies a group of EJB QL queries NamedQuery name="query name" Specifies an EJB QL query query="SQL query" The entity bean container-managed relationship (CMR) relationships in EJB 2.0 are specified with the ejb-relation/ elements in theejb-jar.xml deployment descriptor, and the entity bean CMR relationships in EJB 3.0 are specified in the bean class. The metadata annotations used to specify the entity bean CMR relationships are listed in the following table: Annotation Description OneToMany One-to-many entity bean CMR relationship. OneToOne One-to-one entity bean CMR relationship. ManyToOne Many-to-one entity bean CMR relationship. ManyToMany Many-to-many entity bean CMR relationship. 30 aChapter 2 The EJB 2.0 entity The EJB 2.0 entity class being migrated to EJB 3.0 in this chapter is as shown next: import javax.ejb.; abstract public class CatalogBean implements EntityBean private EntityContext ctx; public CatalogBean() ; public void setEntityContext(EntityContext ctx) this.ctx = ctx; public void unsetEntityContext() this.ctx = null; abstract public String getCatalogId(); abstract public void setCatalogId(String catalogId); abstract public String getJournal(); abstract public void setJournal(java.lang.String journal); abstract public String getPublisher(); abstract public void setPublisher(String publisher); public void ejbActivate() public void ejbPassivate() public void ejbLoad() public void ejbStore() public void ejbRemove() throws RemoveException public String ejbCreate(String catalogId, String journal, String publisher) throws CreateException setCatalogId(catalogId); setJournal(journal); setPublisher(publisher); return null; public void ejbPostCreate(String catalogId, String journal, String publisher) 31 aConverting an EJB 2.0 Entity to an EJB 3.0 Entity The chapter is about converting an EJB 2 entity to EJB 3.0. We are not migrating, which would have involved taking sections of the EJB 2 entity and creating the corresponding EJB 3 entity. We are converting using an XSLT transformation and we need to modify slightly the starting deployment descriptorejb-jar.xml. The EJB 2.0 entityejb-jar.xml deployment descriptor does not include enough information to generate an EJB 3.0 entity from. Modify theejb-jar.xml deployment descriptor for the example entity to include elements for the table name, field type, and EJB QL query collection type for multi-entity return values. Add thetable-name/, field-type/, andcollection-type/ elements toejb-jar.xml. The modified ejb-jar.xml deployment descriptor for the example entity bean is as follows: ?xml version="1.0"? DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN" "http://java.sun.com/dtd/ejb-jar_2_0.dtd" ejb-jar enterprise-beans entity table-nameCatalog/table-name ejb-nameCatalog/ejb-name local-homeCatalogHome/local-home localCatalog/local ejb-classCatalogBean/ejb-class persistence-typeContainer/persistence-type prim-key-classCatalogPK/prim-key-class reentrantFalse/reentrant cmp-version2.x/cmp-version abstract-schema-nameCatalogBean/abstract-schema-name cmp-field field-namecatalogId/field-name field-typeString/field-type /cmp-field cmp-field field-namejournal/field-name field-typeString/field-type /cmp-field cmp-field field-namepublisher/field-name field-typeString/field-type /cmp-field primkey-fieldcatalogId/primkey-field query query-method 32 aChapter 2 method-namefindByCatalogId/method-name method-params method-paramjava.lang.String/method-param /method-params /query-method ejb-ql CDATASELECT OBJECT(a) FROM CatalogBean AS a WHERE a.catalogId = ?1 /ejb-ql /query query query-method method-namefindByJournal/method-name method-params method-paramjava.lang.String/method-param /method-params /query-method ejb-ql CDATASELECT OBJECT(a) FROM CatalogBean AS a WHERE a.journal= ?1 /ejb-ql collection-typejava.util.Collection/collection-type /query /entity /enterprise-beans /ejb-jar Copy the modified deployment descriptor to the ejb-jar-modified.xml file in JDeveloper. The XSLT stylesheet Next, convert the modified ejb-jar.xml to an EJB 3.0 entity bean with an XSLT stylesheet. The EJB 3.0 entity bean class is generated using the built-in XSLT transformation tool in JDeveloper. The XSLT stylesheet entity-bean.xsl used to generate the EJB 3.0 entity is listed next: ?xml version="1.0" encoding="UTF-8"? xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xsl:output encoding="ISO-8859-1" omit-xml-declaration="yes" method="text" / xsl:template match="/" 33 aConverting an EJB 2.0 Entity to an EJB 3.0 Entity xsl:apply-templates select="ejb-jar/enterprise-beans/entity" / xsl:apply-templates select="ejb-jar/relationships/ejb-relation" / /xsl:template xsl:variable name="lcletters" abcdefghijklmnopqrstuvwxyz /xsl:variable xsl:variable name="ucletters" ABCDEFGHIJKLMNOPQRSTUVWXYZ /xsl:variable xsl:template match="cmp-field" xsl:param name="varDecl" / xsl:param name="beanMethods" / xsl:param name="pkFields" / xsl:param name="constructorFields" / xsl:param name="constructor" / xsl:param name="constructorPK" / xsl:param name="primKeyField" / xsl:param name="fieldName" select="field-name" / Create a constructor with primary key field as parameter: xsl:if test="constructor='constructor'" xsl:if test="primKeyField=fieldName" this. xsl:value-of select="translate((substring(field-name, 1,1)),ucletters,lcletters)" / xsl:value-of select="substring(field-name,2)" / = xsl:value-of select="translate((substring(field-name, 1,1)), ucletters,lcletters)" / xsl:value-of select="substring(field-name,2)" / ; /xsl:if /xsl:if xsl:if test="varDecl='varDecl'" private xsl:value-of select="field-type" / xsl:text /xsl:text xsl:value-of select="field-name" / ; xsl:text disable-output-escaping="yes" 34 aChapter 2 /xsl:text /xsl:if xsl:if test="constructorFields='constructorFields'" xsl:if test="primKeyField=fieldName" xsl:value-of select="field-type" / xsl:text /xsl:text xsl:value-of select="translate((substring(field-name, 1,1)), ucletters,lcletters)" / xsl:value-of select="substring(field-name,2)" / /xsl:if /xsl:if Create getter and setter methods for the primary key field: xsl:if test="beanMethods='beanMethods'" xsl:if test="primKeyField=fieldName" xsl:text disable-output-escaping="yes"Id/xsl:text xsl:text disable-output-escaping="yes" /xsl:text xsl:text disable-output-escaping="yes"Column(name="/xsl:text xsl:value-of select="translate((substring(field-name, 1)), lcletters,ucletters)" / xsl:text disable-output-escaping="yes"", unique=true)/xsl:text /xsl:if public xsl:value-of select="field-type" / get xsl:value-of select="translate((substring(field-name, 1,1)), lcletters,ucletters)" / xsl:value-of select="substring(field-name,2)" / () return xsl:value-of select="translate((substring(field-name, 1,1)), ucletters,lcletters)" / xsl:value-of select="substring(field-name,2)" / ; xsl:text /xsl:text public void set xsl:value-of select="translate((substring(field-name, 1,1)), lcletters,ucletters)" / xsl:value-of select="substring(field-name,2)" / 35 aConverting an EJB 2.0 Entity to an EJB 3.0 Entity ( xsl:value-of select="field-type" / xsl:text /xsl:text xsl:value-of select="translate((substring(field-name, 1,1)), ucletters,lcletters)" / xsl:value-of select="substring(field-name,2)" / )this. xsl:value-of select="translate((substring(field-name, 1,1)), ucletters,lcletters)" / xsl:value-of select="substring(field-name,2)" / = xsl:value-of select="translate((substring(field-name, 1,1)), ucletters,lcletters)" / xsl:value-of select="substring(field-name,2)" / ; /xsl:if /xsl:template Create getter and setter methods for entity relationships: xsl:template match="ejb-relation" xsl:variable name="src1" select="ejb-relationship-roleposition()=1/ relationship-role-source/ejb- name" / xsl:variable name="src2" select="ejb-relationship-roleposition()=2/ relationship-role-source/ejb- name" / xsl:variable name="multiplicity1" select="ejb-relationship-roleposition()=1/multiplicity" / xsl:variable name="multiplicity2" select="ejb-relationship-roleposition()=2/multiplicity" / xsl:variable name="cmr1" select="ejb-relationship-roleposition()=1/cmr-field/cmr-field- name" / xsl:variable name="cmrType1" select="ejb-relationship-roleposition()=1/cmr-field/cmr-field- type" / xsl:variable name="cmr2" select="ejb-relationship-roleposition()=2/cmr-field/cmr-field- name" / 36 aChapter 2 xsl:if test="multiplicity2='Many'" private xsl:value-of select="cmrType1" / xsl:text disable-output-escaping="yes"</xsl:text xsl:value-of select="src2" / xsl:text disable-output-escaping="yes"> /xsl:text xsl:value-of select="cmr1"/ ; /xsl:if xsl:if test="multiplicity2='One'" private xsl:value-of select="src2"/ xsl:text /xsl:text xsl:value-of select="cmr1"/ ; /xsl:if xsl:if test="multiplicity1='One' and multiplicity2='Many'" xsl:text disable-output-escaping="yes"OneToMany/xsl:text xsl:text /xsl:text public xsl:value-of select="cmrType1"/ xsl:text disable-output-escaping="yes"</xsl:text xsl:value-of select="src2"/ xsl:text disable-output-escaping="yes"> /xsl:text get xsl:value-of select="translate((substring(cmr1, 1, 1)), lcletters,ucletters)"/ xsl:value-of select="substring(cmr1,2)"/ ()return xsl:value-of select="cmr1"/ ; public void set xsl:value-of select="translate((substring(cmr1, 1, 1)), lcletters,ucletters)"/ xsl:value-of select="substring(cmr1,2)"/ ( xsl:value-of select="cmrType1"/ xsl:text disable-output-escaping="yes"</xsl:text xsl:value-of select="src2"/ xsl:text disable-output-escaping="yes"> /xsl:text xsl:text /xsl:text xsl:value-of select="cmr1"/ ) this. xsl:value-of select="cmr1"/ = 37 aConverting an EJB 2.0 Entity to an EJB 3.0 Entity xsl:value-of select="cmr1"/ ; /xsl:if xsl:if test="multiplicity1='One' and multiplicity2='One'" xsl:text disable-output-escaping="yes"OneToOne/xsl:text xsl:text /xsl:text public xsl:text disable-output-escaping="yes"</xsl:text xsl:value-of select="src2"/ xsl:text disable-output-escaping="yes"> /xsl:text get xsl:value-of select="translate((substring(cmr1, 1, 1)), lcletters,ucletters)"/ xsl:value-of select="substring(cmr1,2)"/ ()return xsl:value-of select="cmr1"/ ; public void set xsl:value-of select="translate((substring(cmr1, 1, 1)), lcletters,ucletters)"/ xsl:value-of select="substring(cmr1,2)"/ ( xsl:value-of select="src2"/ xsl:text /xsl:text xsl:value-of select="cmr1"/ ) this. xsl:value-of select="cmr1"/ = xsl:value-of select="cmr1"/ ; /xsl:if xsl:if test="multiplicity1='Many' and multiplicity2='One'" xsl:text disable-output-escaping="yes"ManyToOne/xsl:text xsl:text /xsl:text public xsl:value-of select="src2"/ get xsl:value-of select="translate((substring(cmr1, 1, 1)), lcletters,ucletters)"/ xsl:value-of select="substring(cmr1,2)"/ ()return xsl:value-of select="cmr1"/ ; public void set xsl:value-of select="translate((substring(cmr1, 1, 1)), lcletters,ucletters)"/ 38 aChapter 2 xsl:value-of select="substring(cmr1,2)"/ ( xsl:value-of select="src2"/ xsl:text /xsl:text xsl:value-of select="cmr1"/ ) this. xsl:value-of select="cmr1"/ = xsl:value-of select="cmr1"/ ; /xsl:if xsl:if test="multiplicity1='Many' and multiplicity2='Many'" xsl:text disable-output-escaping="yes"ManyToMany/xsl:text xsl:text /xsl:text public xsl:value-of select="cmrType1"/ xsl:text disable-output-escaping="yes"</xsl:text xsl:value-of select="src2"/ xsl:text disable-output-escaping="yes"> /xsl:text get xsl:value-of select="translate((substring(cmr1, 1, 1)), lcletters,ucletters)"/ xsl:value-of select="substring(cmr1,2)"/ ()return xsl:value-of select="cmr1"/ ; public void set xsl:value-of select="translate((substring(cmr1, 1, 1)), lcletters,ucletters)"/ xsl:value-of select="substring(cmr1,2)"/ ( xsl:value-of select="cmrType1"/ xsl:text disable-output-escaping="yes"</xsl:text xsl:value-of select="src2"/ xsl:text disable-output-escaping="yes"> /xsl:text xsl:text /xsl:text xsl:value-of select="cmr1"/ ) this. xsl:value-of select="cmr1"/ = xsl:value-of select="cmr1"/ ; /xsl:if /xsl:template 39 aConverting an EJB 2.0 Entity to an EJB 3.0 Entity Addimport statements: xsl:template match="entity" import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Column; import javax.persistence.NamedQueries; import javax.persistence.NamedQuery; import javax.persistence.OneToMany; import javax.persistence. OneToOne; import javax.persistence.ManyToOne; import javax.persistence.ManyToMany; import javax.persistence.Table; Add annotations for the entity and entity table: xsl:text disable-output-escaping="yes"Entity/xsl:text xsl:text disable-output-escaping="yes" /xsl:text xsl:text disable-output-escaping="yes"Table(name="/xsl:text xsl:value-of select="table-name"/ xsl:text disable-output-escaping="yes"")/xsl:text xsl:text disable-output-escaping="yes" /xsl:text Add annotation for named queries: xsl:if test="query" xsl:text disable-output-escaping="yes"NamedQueries(/xsl:text xsl:apply-templates select="query"/ xsl:text disable-output-escaping="yes")/xsl:text /xsl:if Add a class declaration and class constructor: public class xsl:value-of select="ejb-class"/ implements java.io.Serializable public xsl:value-of select="ejb-class"/ () public xsl:value-of select="ejb-class"/ ( xsl:apply-templates select="cmp-field" xsl:with-param name="constructorFields" select="'constructorFields'"/ xsl:with-param name="primKeyField" select="primkey-field"/ /xsl:apply-templates 40 aChapter 2 ) xsl:apply-templates select="cmp-field" xsl:with-param name="constructor" select="'constructor'"/ xsl:with-param name="primKeyField" select="primkey-field"/ /xsl:apply-templates xsl:apply-templates select="cmp-field" xsl:with-param name="varDecl" select="'varDecl'"/ /xsl:apply-templates xsl:text disable-output-escaping="yes" /xsl:text xsl:apply-templates select="cmp-field" xsl:with-param name="beanMethods" select="'beanMethods'"/ xsl:with-param name="primKeyField" select="primkey-field"/ /xsl:apply-templates /xsl:template Add named queries: xsl:template match="query" xsl:text disable-output-escaping="yes"NamedQuery(name="/ xsl:text xsl:value-of select="translate((substring(query-method/method-name, 1,1)),lcletters,ucletters)"/ xsl:value-of select="substring(query-method/method-name,2)"/ xsl:text disable-output-escaping="yes"",query="/xsl:text xsl:value-of select="ejb-ql"/ xsl:text disable-output-escaping="yes"")/xsl:text xsl:if test="position() = last()",/xsl:if xsl:text disable-output-escaping="yes" /xsl:text /xsl:template /xsl:stylesheet Copy the stylesheet to theentity-bean.xsl file in JDeveloper. 41 aConverting an EJB 2.0 Entity to an EJB 3.0 Entity Generating the EJB 3.0 entity To transform the modified deployment descriptor ( ejb-jar-modified.xml) to an EJB 3.0 entity bean class, right-click onentity-bean.xsl and select Run. In the XSLT Settings window select Input XML File asejb-jar-modified.xml and specify Output File asCatalogBean.java. Click on OK. 42 a