database.makePersistent(gat);
tx.commit();
// now cross ODMG with PB api
Gatherer fetchedGatherer = null;
PersistenceBroker pb = null;
try
{
pb = PersistenceBrokerFactory.defaultPersistenceBroker();
pb.clearCache();
Criteria crit = new Criteria();
crit.addLike("name", name);
Query q = QueryFactory.newQuery(Gatherer.class, crit);
fetchedGatherer = (Gatherer) pb.getObjectByQuery(q);
}
finally
{
if(pb != null) pb.close();
}
// check queried result
assertNotNull(fetchedGatherer);
assertEquals(gat.getGatId(), fetchedGatherer.getGatId());
assertNotNull(fetchedGatherer.getCollectiblesB());
assertEquals(1, fetchedGatherer.getCollectiblesB().size());
CollectibleB fetched_B = (CollectibleB) fetchedGatherer.getCollectiblesB().iterator().next();
assertNotNull(fetched_B);
// Now work with queried result
tx.begin();
tx.getBroker().clearCache();
//*************************************
tx.lock(fetchedGatherer, Transaction.WRITE);
CollectibleB newB = new CollectibleB(name);
newB.setGatherer(fetchedGatherer);
fetchedGatherer.getCollectiblesB().add(newB);
tx.lock(newB, Transaction.WRITE);
//*************************************
assertEquals(2, fetchedGatherer.getCollectiblesB().size());
tx.commit();
// now cross again ODMG with PB api
fetchedGatherer = null;
pb = null;
try
{
pb = PersistenceBrokerFactory.defaultPersistenceBroker();
pb.clearCache();
Criteria crit = new Criteria();
crit.addLike("name", name);
Query q = QueryFactory.newQuery(Gatherer.class, crit);
fetchedGatherer = (Gatherer) pb.getObjectByQuery(q);
}
finally
{
if(pb != null) pb.close();
}
// check queried result
assertNotNull(fetchedGatherer);
assertEquals(gat.getGatId(), fetchedGatherer.getGatId());