Handle CMP Entity Bean Primitive Return Types When Migrating from WebLogic to Red Hat JBoss Enterprise Application Platform

Updated -

Summary

Some Oracle WebLogic applications with high performance requirements may use primitive types rather than Object types where the database allows NULL values. For example, if an Integer type database column that allows NULL values has not been initialized or contains no data, it will return NULL. WebLogic can be configured to return a primitive type for the NULL column value and will return 0 in this situation.

Because primitive types have lower memory requirements and better performance, you may want to preserve this behavior in JBoss Enterprise Application Platform 6. You can achieve the same results by adding a try/catch block around the database call and return a 0 in the case of a NULL, however, this approach will impact performance. This article describes a better approach.

Procedure to Use Primitive Return Types in JBoss EAP

NOTE: Because primitive types are not Objects and can not be NULL, updates using the following procedure will replace the NULL in the database column with 0. This is the same behavior that occurs in WebLogic.

  1. Modify the application code.

    Assume the application has defined the following interface for the CMP entity bean:

    public interface primitiveInterface {
        public int getNumber();
        public void setNumber(int number);
    }
    

    WebLogic returns a 0 in the case of a NULL value in the database, so you must modify the interface as follows to get the similar result in JBoss EAP.

    public interface primitiveInterface {
        public Integer getNumberAsObject();
        public int getNumber() {
            if (getNumberAsObject() != null) {
                return getNumberAsObject().intValue();
            } else {
                return 0;
            }
        }
        public void setNumberAsObject(Integer number);
        public void setNumber(int number) {
            // The following code assumes primitive autoboxing is available. 
            // Use the appropriate code if it is not.
            setNumberAsObject(number); 
        }
    }
    
  2. Modify the ejb-jar.xml descriptor file.

    The following is an example of the entity bean description in the ejb-jar.xml file before the migration.

    <entity>
        <ejb-name>PrimitiveInterface</ejb-name>
        <home>com.redhat.demo.PrimitiveInterfaceHome</home>
        <remote>com.redhat.demo.PrimitiveInterface</remote>
        <ejb-class>com.redhat.demo.PrimitiveInterface</ejb-class>
        <persistence-type>Container</persistence-type>
        <reentrant>false</reentrant>
        <cmp-version>2.x</cmp-version>
        <abstract-schema-name>PrimitiveInterface</abstract-schema-name>
        <cmp-field>
            <field-name>number</field-name>
        </cmp-field>
    </entity>
    

    You must change the <field-name> element from number to numberAsObject. This tells JBoss EAP to associate the getNumberAsObject() method with the database instead of the getNumber() method.

    <entity>
        <ejb-name>PrimitiveInterface</ejb-name>
        <home>com.redhat.demo.PrimitiveInterfaceHome</home>
        <remote>com.redhat.demo.PrimitiveInterface</remote>
        <ejb-class>com.redhat.demo.PrimitiveInterface</ejb-class>
        <persistence-type>Container</persistence-type>
        <reentrant>false</reentrant>
        <cmp-version>2.x</cmp-version>
        <abstract-schema-name>PrimitiveInterface</abstract-schema-name>
        <cmp-field>
            <field-name>numberAsObject</field-name>
        </cmp-field>
    </entity>
    
  3. Modify the jbosscmp-jdbc.xml descriptor file.

    The following is an example of the entity bean description in the jbosscmp-jdbc.xml file before migration.

    <jbosscmp-jdbc>
      <enterprise-beans>
        <entity>
          <ejb-name>PrimitiveInterface</ejb-name>
          <table-name>primitive</table-name>
    
          <cmp-field>
            <field-name>number</field-name>
            <column-name>number</column-name>
          </cmp-field>
        <entity>
      <enterprise-beans>
    <jbosscmp-jdbc>
    

    Change the <field-name> element from number to numberAsObject.

    <jbosscmp-jdbc>
      <enterprise-beans>
        <entity>
          <ejb-name>PrimitiveInterface</ejb-name>
          <table-name>primitive</table-name>
    
          <cmp-field>
            <field-name>numberAsObject</field-name>
            <column-name>number</column-name>
          </cmp-field>
        <entity>
      <enterprise-beans>
    <jbosscmp-jdbc>