Sun Java logo     Previous      Contents      Index      Next     

Sun logo
Sun Java System Application Server Platform Edition 8 Migrating and Redeploying Server Applications Guide 

Chapter 2
Migrating from EJB 1.1 to EJB 2.0

Although the EJB 1.1 specification will continue to be supported in Sun Java System Application Server Platform Edition 8, the use of the EJB 2.0 architecture is recommended to leverage its enhanced capabilities.

To migrate EJB 1.1 to EJB 2.0 a number of modifications will be required, including within the source code of components.

Essentially, the required modifications relate to the differences between EJB 1.1 and EJB 2.0, all of which are described in the following topics.

EJB Query Language

The EJB 1.1 specification left the manner and language for forming and expressing queries for finder methods to each individual application server. While many application server vendors let developers form queries using SQL, others use their own proprietary language specific to their particular application server product. This mixture of query implementations causes inconsistencies between application servers.

The EJB 2.0 specification introduces a query language called EJB Query Language, or EJB QL to correct many of these inconsistencies and shortcomings. EJB QL is based on SQL92. It defines query methods, in the form of both finder and select methods, specifically for entity beans with container-managed persistence. EJB QL's principal advantage over SQL is its portability across EJB containers and its ability to navigate entity bean relationships.

Local Interfaces

In the EJB 1.1 architecture, session and entity beans have one type of interface, a remote interface, through which they can be accessed by clients and other application components. The remote interface is designed such that a bean instance has remote capabilities; the bean inherits from RMI and can interact with distributed clients across the network.

With EJB 2.0, session beans and entity beans can expose their methods to clients through two types of interfaces: a remote interface and a local interface. The 2.0 remote interface is identical to the remote interface used in the 1.1 architecture, whereby, the bean inherits from RMI, exposes its methods across the network tier, and has the same capability to interact with distributed clients.

However, the local interfaces for session and entity beans provide support for lightweight access from EJBs that are local clients; that is, clients co-located in the same EJB container. The EJB 2.0 specification further requires that EJBs that use local interfaces be within the same application. That is, the deployment descriptors for an application's EJBs using local interfaces must be contained within one ejb-jar file.

The local interface is a standard Java interface. It does not inherit from RMI. An enterprise bean uses the local interface to expose its methods to other beans that reside within the same container. By using a local interface, a bean may be more tightly coupled with its clients and may be directly accessed without the overhead of a remote method call.

In addition, local interfaces permit values to be passed between beans with pass by reference semantics. Because you are now passing a reference to an object, rather than the object itself, this reduces the overhead incurred when passing objects with large amounts of data, resulting in a performance gain.

Setting up a session or entity bean to use a local interface rather than a remote interface is simple. The local interface through which the bean's methods are exposed to clients extends EJBLocalObject rather than EJBObject. Similarly, the bean's home interface extends EJBLocalHome rather than EJBHome. The implementation class extends the same EntityBean or SessionBean interface.

In EJB 2.0, a bean that is destined to be remote extends EJBObject in its remote interface and EJBHome in its home interface, just as it did in EJB 1.1.

EJB 2.0 Container-Managed Persistence (CMP)

The EJB 2.0 specification expanded CMP to allow multiple entity beans to have relationships among themselves. This is referred to as Container-Managed Relationships (CMR). The container manages the relationships and the referential integrity of the relationships.

The EJB 1.1 specification presented a more limited CMP model. The EJB 1.1 architecture limited CMP to data access that is independent of the database or resource manager type. It allowed you to expose only an entity bean's instance state through its remote interface; there is no means to expose bean relationships. The EJB 1.1 version of CMP depends on mapping the instance variables of an entity bean class to the data items representing their state in the database or resource manager. The CMP instance fields are specified in the deployment descriptor, and when the bean is deployed, the deployer uses tools to generate code that implements the mapping of the instance fields to the data items.

You must also change the way you code the bean's implementation class. According to the EJB 2.0 specification, the implementation class for an entity bean that uses CMP is now defined as an abstract class.

Defining Persistent Fields

The EJB 2.0 specification lets you designate an entity bean's instance variables as CMP fields or CMR fields. You define these fields in the deployment descriptor. CMP fields are marked with the element cmp-field, while container-managed relationship fields are marked with the element cmr-field.

In the implementation class, note that you do not declare the CMP and CMR fields as public variables. Instead, you define get and set methods in the entity bean to retrieve and set the values of these CMP and CMR fields. In this sense, beans using the 2.0 CMP follow the JavaBeans model: instead of accessing instance variables directly, clients use the entity bean's get and set methods to retrieve and set these instance variables. Keep in mind that the get and set methods only pertain to variables that have been designated as CMP or CMR fields.

Defining Entity Bean Relationships

As noted previously, the EJB 1.1 architecture does not support CMRs between entity beans. The EJB 2.0 architecture does support both one-to-one and one-to-many CMRs. Relationships are expressed using CMR fields, and these fields are marked as such in the deployment descriptor. You set up the CMR fields in the deployment descriptor using the appropriate deployment tool for your application server.

Similar to CMP fields, the bean does not declare the CMR fields as instance variables. Instead, the bean provides get and set methods for these fields.

Message-Driven Beans

Message-driven beans are another new feature introduced by the EJB 2.0 architecture. Message-driven beans are transaction-aware components that process asynchronous messages delivered through the Java Message Service (JMS). The JMS API is an integral part of the J2EE 1.3 and J2EE 1.4 platform.

Asynchronous messaging allows applications to communicate by exchanging messages so that senders are independent of receivers. The sender sends its message and does not have to wait for the receiver to receive or process that message. This differs from synchronous communication, which requires the component that is invoking a method on another component to wait or block until the processing completes and control returns to the caller component.

Migrating EJB Client Applications

This section includes the following topics:

Declaring EJBs in the JNDI Context

In Sun Java System Application Server Platform Edition 8, EJBs are systematically mapped to the JNDI sub-context "ejb/". If we attribute the JNDI name "Account" to an EJB, then Sun Java System Application Server Platform Edition 8 will automatically create the reference "ejb/Account" in the global JNDI context. The clients of this EJB will therefore have to look up "ejb/Account" to retrieve the corresponding home interface.

Let us examine the code for a servlet method deployed in Sun ONE Application Server 6.x.

The servlet presented here calls on a stateful session bean, BankTeller, mapped to the root of the JNDI context. The method whose code we are considering is responsible for retrieving the home interface of the EJB, so as to enable a BankTeller object to be instantiated and a remote interface for this object to be retrieved, in order to make business method calls to this component.

* Look up the BankTellerHome interface using JNDI.
private BankTellerHome lookupBankTellerHome(Context ctx)
   throws NamingException
Object home = (BankTellerHome) ctx.lookup("ejb/BankTeller");
return (BankTellerHome) PortableRemoteObject.narrow(home, BankTellerHome.class);
catch (NamingException ne)
log("lookupBankTellerHome: unable to lookup BankTellerHome" +

     "with JNDI name 'BankTeller': " + ne.getMessage() );
throw ne;

As the code already uses ejb/BankTeller as an argument for the lookup, there is no need for modifying the code to be deployed on Sun Java System Application Server Platform Edition 8.

Recap on Using EJB JNDI References

This section summarizes the considerations when using EJB JNDI references. Where noted, the consideration details are specific to a particular source application server platform.

Placing EJB References in the JNDI Context

It is only necessary to modify the name of the EJB references in the JNDI context mentioned above (moving these references from the JNDI context root to the sub-context "ejb/") when the EJBs are mapped to the root of the JNDI context in the existing WebLogic application.

If these EJBs are already mapped to the JNDI sub-context ejb/ in the existing application, no modification is required.

However, when configuring the JNDI names of EJBs in the deployment descriptor within the Forté for Java IDE, it is important to avoid including the prefix ejb/ in the JNDI name of an EJB. Remember that these EJB references are automatically placed in the JNDI ejb/ sub-context with Sun Java System Application Server Platform Edition 8. So, if an EJB is given to the JNDI name "BankTeller" in its deployment descriptor, the reference to this EJB will be "translated" by Sun Java System Application Server Platform Edition 8 into ejb/BankTeller, and this is the JNDI name that client components of this EJB must use when carrying out a lookup.

Global JNDI context versus local JNDI context

Using the global JNDI context to obtain EJB references is a perfectly valid, feasible approach with Sun Java System Application Server Platform Edition 8. Nonetheless, it is preferable to stay as close as possible to the J2EE specification, and retrieve EJB references through the local JNDI context of EJB client applications. When using the local JNDI context, you must first declare EJB resource references in the deployment descriptor of the client part (web.xml for a Web application, ejb-jar.xml for an EJB component).

Migrating CMP Entity EJBs

This section describes the steps to migrate your application components from the EJB 1.1 architecture to the EJB 2.0 architecture.

In order to migrate a CMP 1.1 bean to CMP 2.0, we first need to verify if a particular bean can be migrated. The steps to perform this verification are as follows.

  1. From the ejb-jar.xml file, go to the <cmp-fields> names and check if the optional tag <prim-key-field> is present in the ejb-jar.xml file and has an indicated value. If it does, go to next step.
  2. Look for the <prim-key-class> field name in the ejb-jar.xml, get the class name and get the public instance variables declared in the class. Now see if the signature (name and case) of these variables matches with the <cmp-field> names above. Segregate the ones that are found. In these segregated fields, check if some of them start with an upper case letter. If any of them do, then migration cannot be performed.

  3. Look into the bean class source code and obtain the java types of all the <cmp-field> variables.
  4. Change all the <cmp-field> names to lowercase and construct accessors from them. For example if the original field name is Name and its java type is String, the accessor method signature will be:
  5. Public void setName(String name)
    Public String getName()

  6. Compare these accessor method signatures with the method signatures in the bean class. If there is an exact match found, migration is not possible.
  7. Get the custom finder methods signatures and their corresponding SQLs. Check if there is a ‘Join’ or ‘Outer join’ or an ‘OrderBy’ in the SQL, if yes, we cannot migrate, as EJB QL does not support ‘joins’, ‘Outer join’ and ‘OrderBy’.
  8. Any CMP 1.1 finder, which used java.util.Enumeration, should now use java.util.Collection. Change your code to reflect this. CMP2.0 finders cannot return java.util.Enumeration.

"Migrating the Bean Class," explains how to perform the actual migration process.

Migrating the Bean Class

This section describes the steps required to migrate the bean class to Sun Java System Application Server Platform Edition 8.

  1. Prepend the bean class declaration with the keyword abstract. For example if the bean class declaration was:
  2. Public class CabinBean implements EntityBean // before modification

    abstract Public class CabinBean implements EntityBean // after modification

  3. Prefix the accessors with the keyword abstract.
  4. Insert all the accessors after modification into the source(.java) file of the bean class at class level.
  5. Comment out all the cmp fields in the source file of the bean class.
  6. Construct protected instance variable declarations from the cmp-field names in lowercase and insert them at the class level.
  7. Read up all the ejbCreate() method bodies (there could be more than one ejbCreate). Look for the pattern ‘<cmp-field>=some value or local variable’, and replace it with the expression ‘abstract mutator method name (same value or local variable)’. For example, if the ejbCreate body (before migration) is like this:
  8.   public MyPK ejbCreate(int id, String name)
          { = 10*id;
        Name = name;      //1
        return null;

  1. All the protected variables declared in the ejbPostCreate()methods in step 5 must be initialized. The protected variables will be equal in number with the ejbCreate() methods. This initialization will be done by inserting the initialization code in the following manner:
  2.   protected String name; //from step 5
      protected int id; //from step 5
        public void ejbPostCreate(int id, String name)
    name /*protected variable*/ = getName(); /*abstract accessor*/ //inserted in this step
    id /*protected variable*/ = getId(); /*abstract accessor*/ //inserted in this step

  3. Inside the ejbLoad method, you must set the protected variables to the beans’ database state. To do so, insert the following lines of code:
  4.     public void ejbLoad()
          name = getName(); //inserted in this step
          id = getId(); //inserted in this step
              ……….. //already present code

  5. Similarly, you will have to update the beans’ state inside ejbStore()so that its database state gets updated. But remember, you are not allowed to update the setters that correspond to the primary key outside the ejbCreate(), so do not include them inside this method. Insert the following lines of code:
  6.   public void ejbStore()
        setName(name); //inserted in this step
      //  setId(id); //Do not insert this if it is a
    part of the primary key
            ……………….. //already present code

  7. As a last change to the bean class source (.java) file, examine the whole code and replace all occurrences of any <cmp-field> variable name with the equivalent protected variable name (as declared in step 5).
  8. If you do not migrate the bean, at the minimum you need to insert the <cmp-version>1.x</cmp-version> tag inside the ejb-jar.xml file at the appropriate place, so that the unmigrated bean still works on Sun Java System Application Server Platform Edition 8.

Migration of ejb-jar.xml

To migrate the file ejb-jar.xml to Sun Java System Application Server Platform Edition 8, perform the following steps:

  1. In the ejb-jar.xml, convert all <cmp-fields> to lowercase.
  2. In the ejb-jar.xml file, insert the tag <abstract-schema-name> after the <reentrant> tag. The schema name will be the name of the bean as in the < ejb-name> tag, prefixed with “ias_”.
  3. Insert the following tags after the <primkey-field> tag:
  4. <security-identity><use-caller-identity/></security-identity>

  5. Use the SQL’s obtained above to construct the EJB QL from SQL.
  6. Insert the <query> tag and all its nested child tags with all the required information in the ejb-jar.xml, just after the <security-identity> tag.

Custom Finder Methods

The custom finder methods are the findBy... methods (other than the default findByPrimaryKey method), which can be defined in the home interface of an entity bean. Since the EJB 1.1 specification does not stipulate a standard for defining the logic of these finder methods, EJB server vendors are free to choose their implementations. As a result, the procedures used to define the methods vary considerably between the different implementations chosen by vendors.

Sun ONE Application Server 6.x uses standard SQL to specify the finder logic.

Information concerning the definition of this finder method is stored in the enterprise bean's persistence descriptor (Account-ias-cmp.xml) as follows:


Each findXXX finder method therefore has two corresponding entries in the deployment descriptor (SQL code for the query, and the associated parameters).

In Sun Java System Application Server Platform Edition 8 the custom finder method logic is also declarative, but is based on the EJB query language EJB QL.

The EJB-QL language cannot be used on its own. It has to be specified inside the file ejb-jar.xml, in the <ejb-ql> tag. This tag is inside the <query> tag, which defines a query (finder or select method) inside an EJB. The EJB container can transform each query into the implementation of the finder or select method. Here's an example of an <ejb-ql> tag:

WHERE = ?1]]

Previous      Contents      Index      Next     

Copyright 2003 Sun Microsystems, Inc. All rights reserved.