use of org.datanucleus.samples.detach.ClassOwner in project tests by datanucleus.
the class AttachDetachTest method testSimpleDetach.
/**
* test if simple detachment works. to avoid problems like "No metadata has been registered for class..."
* TODO Change this to use Company model data
*/
public void testSimpleDetach() {
try {
ClassOwner owner1 = new ClassOwner("Owner1");
ClassElements elementA = new ClassElements("ElementA");
ClassElements elementB = new ClassElements("ElementB");
ClassElements elementC = new ClassElements("ElementC");
ClassElements elementD = new ClassElements("ElementD");
owner1.addElement(elementA);
owner1.addElement(elementB);
owner1.getMapToCheckPrefetch().put("C", elementC);
owner1.getListToCheckPrefetch().add(elementD);
PersistenceManager pm = newPM();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
pm.makePersistent(owner1);
tx.commit();
} catch (Exception e) {
LOG.error("Exception in test", e);
fail(e.toString());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
pmf.getDataStoreCache().evictAll();
// make sure we have nothing in cache (metadata in cache)
pm = newPM();
tx = pm.currentTransaction();
try {
// test detach and attach
tx.begin();
Query<ClassOwner> query = pm.newQuery(ClassOwner.class);
Collection<ClassOwner> result = query.executeList();
// add a group just above the detach, so we can see if the detachCopyAll retrieves the fields
pm.getFetchPlan().addGroup("collection");
pm.getFetchPlan().setMaxFetchDepth(2);
result = pm.detachCopyAll(result);
tx.commit();
ClassOwner ownerResult = result.iterator().next();
assertEquals("Expected 2 elements", 2, ownerResult.getElements().size());
} catch (Exception e) {
LOG.error("Exception in test", e);
fail(e.toString());
} finally {
if (tx.isActive())
tx.rollback();
pm.close();
}
} finally {
clean(ClassOwner.class);
}
}
Aggregations