use of org.jpox.samples.one_many.collection.PCFKSetElement in project tests by datanucleus.
the class RelationshipTest method test1toNunidirFK.
/**
* Test case for 1-N unidir using FK.
*/
public void test1toNunidirFK() throws Exception {
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
Object holderId = null;
// Check the persistence of owner and elements
try {
tx.begin();
// Create some data
SetHolder holder = new SetHolder("First");
PCFKSetElement elem1 = new PCFKSetElement("Element 1");
PCFKSetElement elem2 = new PCFKSetElement("Element 2");
holder.getFkSetPC().add(elem1);
holder.getFkSetPC().add(elem2);
pm.makePersistent(holder);
tx.commit();
holderId = pm.getObjectId(holder);
} catch (Exception e) {
LOG.error("Exception in test", e);
fail("Exception thrown while creating 1-N uni FK 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();
try {
tx.begin();
SetHolder holder = (SetHolder) pm.getObjectById(holderId);
assertNotNull("Unable to retrieve container object for 1-N uni FK relationship", holder);
Collection elements = holder.getFkSetPC();
assertNotNull("Elements in holder are null!", elements);
assertEquals("Number of elements in holder is incorrect", 2, elements.size());
Iterator roomsIter = elements.iterator();
while (roomsIter.hasNext()) {
roomsIter.next();
}
tx.commit();
} catch (Exception e) {
LOG.error("Exception in test", e);
fail("Exception thrown while querying 1-N uni FK relationships : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
}
pm.close();
} finally {
// Clean out our data
clean(SetHolder.class);
clean(PCFKSetElement.class);
}
}
Aggregations