use of org.jpox.samples.linkedlist.ParentChildLink in project tests by datanucleus.
the class RelationshipTest method testParentChildLinkRelation.
/**
* Test case for 1-N inverse unidirectional to itself.
*/
public void testParentChildLinkRelation() throws Exception {
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
Object primaryObjId = null;
// Check the persistence of owner and elements
try {
tx.begin();
ParentChildLink secondaryObj = new ParentChildLink("secondaryObj", null);
ParentChildLink primaryObj = new ParentChildLink("primaryObj", secondaryObj);
ParentChildLink childA = new ParentChildLink("childA", null);
ParentChildLink childB = new ParentChildLink("childB", null);
pm.makePersistent(primaryObj);
pm.makePersistent(secondaryObj);
pm.makePersistent(childA);
pm.makePersistent(childB);
primaryObj.addChild(childA);
primaryObj.addChild(childB);
tx.commit();
primaryObjId = pm.getObjectId(primaryObj);
} catch (Exception e) {
LOG.error(e);
fail("Exception thrown while creating 1-N unidirectional relationships : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Check the retrieval of the owner and elements
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
tx = pm.currentTransaction();
try {
tx.begin();
ParentChildLink toSelf = (ParentChildLink) pm.getObjectById(primaryObjId, true);
assertNotNull("Unable to retrieve container object for unidirectional 1-N FK relationship", toSelf);
assertEquals("Number of elements in unidirectional 1-N FK relationship is wrong : was " + toSelf.getChildren().size() + " but should have been 2", toSelf.getChildren().size(), 2);
tx.commit();
} catch (Exception e) {
LOG.error(e);
fail("Exception thrown while querying 1-N unidirectional relationships : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
Extent ex = pm.getExtent(ParentChildLink.class);
Iterator iter = ex.iterator();
while (iter.hasNext()) {
ParentChildLink link = (ParentChildLink) iter.next();
link.getChildren().clear();
link.clearNextObject();
}
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
clean(ParentChildLink.class);
}
}
Aggregations