Hi guys,
there is one thing that is confusing me at the moment
when it comes to making an object persistent.
Let's say I have a Person object and a PersonContainer
object. The PersonContainer object contains a list of
Person object, so it's a 1 to many relationship. The
Person object has a field called PersonContainerId,
which is obviously the id of the container to which it
belongs.
What I wanted is to be able save a given Person object
and hoped that OJB would resolve the Container to
which it belongs and save everything happily. However,
I kept on getting various exceptions, so I did a
workaround which looks like this:
public void enterNewPerson(Person person)
{
// 1. build oql query to select container by
id:
String oqlQuery = "select personContainer
from PersonContainer" +
" where mId = " +
person.getPersonContainerId();
Transaction tx = null;
try
{
// 2. start transaction
tx = odmg.newTransaction();
tx.begin();
// 3. lookup the person cont. specified by
query
OQLQuery query = odmg.newOQLQuery();
query.create(oqlQuery);
List result = (List)query.execute();
PersonContainer toBeEdited =
(PersonContainer)result.get(0);
// 4. lock the modelContainer for write
access
tx.lock(toBeEdited, Transaction.WRITE);
Vector persons = toBeEdited.getPersons();
persons.add(person);
// 5. Edit the person container entry by
adding the new model
toBeEdited.setPersons(persons);
// 6. commit transaction
tx.commit();
}
catch (Throwable t)
{
// rollback in case of errors
tx.abort();
t.printStackTrace();
}
}
**************************************************
As you can see, what I've ended up doing is manually
searching for the appropriate PersonContainer,then
adding the person to the container and finally
commiting the modified container (which in turn adds
the person as well).
I have a feeling this is not the right way to do
things, OJB should be taking care of this on it's own
instead of me searching for the right container.
Can anybody give me a nice example of how this should
be done?
____________________________________________________
Do you Yahoo!?
The New Yahoo! Movies: Check out the Latest Trailers, Premiere Photos and full
Actor Database.
http://au.movies.yahoo.com
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]