Well here is how I did it last night, I could be wrong and someone will fix this for me, but here is what I did anyway:
I always setup a base business object to work with plus the other classes. Everything extends BaseBO.

BaseBO

BaseBO has get/set methods which create the following business objects
HomeBO
BrokerBO
BrokerToHomeBO

each of those business objects have their own set/get methods for the various values you'll want to bind. These values are mapped in the repository.xml to column names.

Also in BaseBO I have get/set methods which create Collections of the following:

Collection homeBOColl
Collection brokerBOColl
Collection brokerToHomesBOColl

Now, say I want to get everything about a home and return it to which ever client requested it, I setup a Data Transfer Object, in this case we'll call it HomeDetailDTO. Home detail DTO has the set/get methods to set/get any of the business objects you need to bring data back to your client. in otherwords you have things like:

public HomeBO getHomeBO()
{
return homeBO;
}


public Collection getHomeBOColl()
{
return homeBOColl;
}

etc...

The reason I have this data transfer object is because it makes it a lot easier to grab all the data once and move one object around pre populated with everything I need in it (including the Collections which will result from the select)

Now to select everything about a Home, I do the following:

public HomeDetailDTO selectHomeDetail( Integer homeID ) throws DataAccessException
{
List results = null;
HomeBO home = null;
HomeDetailDTO homeDet = null;
Transaction tx = null;
try
{
tx = odmg.newTransaction();
tx.begin();
OQLQuery query = odmg.newOQLQuery();
//This select will cascade down to the other relationships you have setup because of auto-retrieve="true"
String sql = "select homeDetail from " + HomeBO.class.getName();
sql += " where homeID = $1";
query.create( sql );
query.bind( homeID );
results = ( List ) query.execute();
tx.commit();
//Bind the results to the HomeBO object
home = ( HomeBO ) results.get( 0 );
//Initialize the home detail dto
homeDet = new HomeDetailDTO();
//Now set the values for the BrokerHomesColl Collection. This is where the meat of this example is see below
homeDet.setBrokerHomesColl( home.getBrokerHomesColl() );
... do other sets here to bind all the data to the HomeDetailDTO ...
}
... do catch etc
}

Now since you've bound all the values to one object and returned the object, you can pull the values out of that object. In my case the brokerHomesColl actually contains a collection of BrokerBO objects with full info on the brokers which are related to the home. This is because I have a 1:n relationship from the brokers table to the brokers_to_homes table. If you run in a debugger (I use IntelliJ IDEA to write my code) you can step through in the debugger and look at the contents of the brokerHomesColl and see that there are 1 or more BrokerBO objects in it. To get the data out and prove that, you do this:

HomeDetailDTO hdto = homeImpl.selectHomeDetail( homeID ); <-- this is the method call to the method described above
Iterator it = hdto.getBrokerHomesColl().iterator();
while( it.hasNext() )
{
BrokersToHomesBO bth = ( BrokersToHomesBO ) it.next();
BrokerBO bro = bth.getBrokerBO();
System.out.println( "The Bro Name is: " + bro.getName() ); // You will see the value of bro.getName() for that specific home
}

If there are no objects to iterate through, then that means there are no BrokerBO objects in the relationship.

So in my case I grab all the values out of BrokerBO for each object in the HomeDetailDTO, add it to a List object and now I have what I need to work with in my 'upper layers' where I do my presentation.

Does this make ANY sense at all? If not I'm surprised because this exact same stuff is in Tutorial 3, and it makes very little sense there until you carefully study the UML design, compare them to the XML mapping, AND look at the ODMG tutorials to see how to select, AND look at the Advanced O/R under 1:n to notice that they say to make sure auto-retreive is true if you're going to use ODMG.

The problem is not the documentation on the site, the problem is WHERE the documentation is on the site and how it's organized, but I can understand why it's all over the place since writing the code is more important than keeping clean docs and concrete world examples. I found most of my clues as to where to look on the site by doing lots of various searches in the archives, none of which had a full answer to the problem.

For those writing the docs, it would be MUCH MORE useful to have an ODMG example right there with the UML design and the xml repository equivalent.


R

--
Robert S. Sfeir
Senior Java Engineer
National Institutes of Health
Center for Information Technology
Department of Enterprise Custom Applications
[EMAIL PROTECTED]


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to