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 🙂