Search in sources :

Example 1 with ClassElements

use of org.datanucleus.samples.detach.ClassElements 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);
    }
}
Also used : ClassElements(org.datanucleus.samples.detach.ClassElements) Transaction(javax.jdo.Transaction) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) JDODetachedFieldAccessException(javax.jdo.JDODetachedFieldAccessException) JDOUserException(javax.jdo.JDOUserException) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException) ClassOwner(org.datanucleus.samples.detach.ClassOwner)

Aggregations

JDODetachedFieldAccessException (javax.jdo.JDODetachedFieldAccessException)1 JDOObjectNotFoundException (javax.jdo.JDOObjectNotFoundException)1 JDOUserException (javax.jdo.JDOUserException)1 PersistenceManager (javax.jdo.PersistenceManager)1 Transaction (javax.jdo.Transaction)1 JDOPersistenceManager (org.datanucleus.api.jdo.JDOPersistenceManager)1 ClassElements (org.datanucleus.samples.detach.ClassElements)1 ClassOwner (org.datanucleus.samples.detach.ClassOwner)1