September 11, 2012

Navigating object relationship in MBO code

This entry is part of the Maximo Java Development series.

When you are in an Mbo class or you already have a reference to an Mbo object is it possible to retrieve the related records using Maximo object relationships as defined in the Database Configuration application.
For example the ASSET object has a relationship called ACTIVEASSETMETER to the ASSETMETER object. This relationship allows to find all active asset meters for the current asset. The resulting set will contain zero or more objects.

If you are in a class that extend the this object already provides a reference to the current Mbo. Use the Mbo.getMboSet(String mboSetName) method to retrieve a related MboSet.
The following snippet prints to the system logs the list of the active meters for the current asset. You can extend the asset Mbo and try to put this code in the init() method to test it.

MboSetRemote meterSet = this.getMboSet("ACTIVEASSETMETER");
MboRemote meter;
for(int j=0; ((meter = meterSet.getMbo(j)) != null); j++)
  String name = meter.getString("METERNAME");
  String desc = meter.getString("METER.DESCRIPTION");
  System.out.println(name + " - " + desc);

In the example I have also used the ASSETMETER.METER relationship in the getString method to directly retrieve the description of the each meter.

Note that Mbos obtained via relationship are included in the same transaction as the parent MBO set so you don't have to explicitly call the save() method.