String postfix = "_" + System.currentTimeMillis();
String firstnameFather = "Father" + postfix;
String firstnameChild_1 = "Child_One" + postfix;
String firstnameChild_2 = "Child_Two" + postfix;
Person father = createPerson(firstnameFather, "testStoreThreePersons_3", null, null);
Person child_1 = createPerson(firstnameChild_1, "testStoreThreePersons_3", null, null);
Person child_2 = createPerson(firstnameChild_2, "testStoreThreePersons_3", null, null);
Person[] children = new Person[]{child_1, child_2};
father.setChildren(children);
child_1.setFather(father);
child_2.setFather(father);
/*
lock childs first, then lock father
TODO: Does not pass - why? A defined lock
order necessary?
if this doesn't make sense remove the test
*/
Transaction tx = odmg.newTransaction();
tx.begin();
tx.lock(child_1, Transaction.WRITE);
tx.lock(child_2, Transaction.WRITE);
tx.lock(father, Transaction.WRITE);
tx.commit();
// without running tx, should not take effect
father.setChildren(null);
father.setFirstname(null);
tx.begin();
// make sure all objects are retrieved freshly in subsequent transactions
((TransactionImpl) tx).getBroker().clearCache();
OQLQuery qry = odmg.newOQLQuery();
qry.create("select a from " + PersonImpl.class.getName() + " where firstname=$1");
qry.bind(firstnameFather);
Collection result = (Collection) qry.execute();
assertEquals("Exactly one element in result set", 1, result.size());
Person returnedFather = (Person) result.iterator().next();
// should retrieve new instance, cause we clear the cache
assertTrue("not same", returnedFather != father);
Person[] returnedChildren = returnedFather.getChildren();
assertNotNull(returnedChildren);
assertEquals(2, returnedChildren.length);
Person child = returnedChildren[0];
Person lookupFather = child.getFather();
assertNotNull(lookupFather);
assertEquals(returnedFather.getFirstname(), lookupFather.getFirstname());
// unfortunately, PersonImpl does not have a suitable equals method.
assertEquals(
"children's names are equal",
Arrays.asList(getFirstNames(returnedChildren)),
Arrays.asList(getFirstNames(children)));