BOFactory: create(..) or createByElement(..) ?

The com.ibm.websphere.bo.BOFactory interface defines two methods:

  • DataObject create(namespace, name)
  • DataObject createByElement(namespace, name)

Both methods take the same two parameters and both return DataObjects. Most of the time, create() is used and returns the DataObject as expected. Sometimes, though, you’ll find that it returns null when the namespace,name pair is correct.

The reason for this peculiarity is based on the structure of the XSD that backs the Business Object. If your XSD looks like:

<xs:complextype name="Person">
  <xs:sequence>
    <xs:element name="firstname" type="xs:string">
    <xs:element name="lastname" type="xs:string">
  </xs:element>
</xs:element></xs:sequence></xs:complextype>

Here, the type “Person” is defined with xs:complexType, so we use the create(..) method. If you created your business objects in WID, they are in this format.

If however, our definition uses an ‘xs:element’ tag such as:

<xs:element name="Person">
  <xs:complextype>
    <xs:sequence>
      <xs:element name="firstname" type="xs:string">
      <xs:element name="lastname" type="xs:string">
    </xs:element>
  </xs:element>
</xs:sequence></xs:complextype></xs:element>

Then we have to use createByElement(..) to get our DataObject as the complexType attribute is ‘anonymous’ (lacking a name attribute) and the name is only provided by the ‘element’ element.

You may be asking yourself why the BOFactory doesn’t just look for one and then the other by default? It would be a very good question. The problem with that solution arises when you have an Element type declaration and a complexType declaration in the same namespace with the same name but unique attributes. You’d never be able to get an instance of the DataObject for the ‘element defined’ structure.

There’s one interesting thing to note when it comes to using the ‘Create Specific BO’ Visual Snippet. This snippet is actually implemented by calling create(..), checking if the value is null, and then calling createByElement(..). I guess they didn’t have the same concerns that the BOFactory team did :)

Author: dan

Comments

  1. I am getting DataObject as ‘null’ when I create DataObject in the following manner.

    ServiceManager serviceManager = new ServiceManager();

    BOFactory boFactory = (BOFactory)serviceManager. locateService(“com/ibm/websphere/bo/BOFactory”);

    DataObject employee = boFactory.create(“http://ModuleWPS/bo”, “Employee”);

    I am getting ‘null’.
    Here is my xsd for the ‘Employee’

    I am not understanding what could be the possible solution for this.
    pls help me.

    Thanks
    Raja

  2. Sorry I could not able to post the xsd for the “Employee”.
    Here “Employee” is the complex type name.

    Thanks,
    Raja

  3. Is it true that the following approach can only used for the case when SDO service consumer is running on the same JVM as SDO service provider? If I have a use case that the SDO service consumer is running on a different JVM, or different WebSphere server, this approach won’t work at all.

Leave a Reply

Your email address will not be published. Required fields are marked *