Search in sources :

Example 66 with Transaction

use of javax.jdo.Transaction in project tests by datanucleus.

the class ManyOneTest method testAddChildReferenceDetached.

public void testAddChildReferenceDetached() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        pm.getFetchPlan().addGroup("persons_of_department");
        tx.begin();
        Department engineering = pm.getObjectById(Department.class, "Engineering");
        assertEquals(2, engineering.getPersons().size());
        Department detachedEngineering = pm.detachCopy(engineering);
        tx.commit();
        pm.close();
        // add new child references
        Person dduck = new Person("Daffy", "Duck", "Daffy Duck", null, null, detachedEngineering);
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        tx.begin();
        pm.makePersistent(dduck);
        tx.commit();
        pm.close();
        // now check the new child reference
        pm = pmf.getPersistenceManager();
        pm.getFetchPlan().addGroup("persons_of_department");
        tx = pm.currentTransaction();
        tx.begin();
        dduck = pm.getObjectById(Person.class, "Daffy Duck");
        engineering = pm.getObjectById(Department.class, "Engineering");
        assertEquals(3, engineering.getPersons().size());
        assertTrue(engineering.getPersons().contains(dduck));
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager)

Example 67 with Transaction

use of javax.jdo.Transaction in project tests by datanucleus.

the class ManyOneTest method testUpdateChildReference.

public void testUpdateChildReference() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        pm.getFetchPlan().addGroup("persons_of_department");
        tx.begin();
        Department engineering = pm.getObjectById(Department.class, "Engineering");
        Department sales = pm.getObjectById(Department.class, "Sales");
        Person bugsBunny = pm.getObjectById(Person.class, "Bugs Bunny");
        assertEquals(engineering, bugsBunny.getDepartment());
        // set new child references
        sales.getPersons().add(bugsBunny);
        engineering.getPersons().remove(bugsBunny);
        bugsBunny.setDepartment(sales);
        tx.commit();
        pm.close();
        // now check the updated reference
        pm = pmf.getPersistenceManager();
        pm.getFetchPlan().addGroup("persons_of_department");
        tx = pm.currentTransaction();
        tx.begin();
        bugsBunny = pm.getObjectById(Person.class, "Bugs Bunny");
        sales = pm.getObjectById(Department.class, "Sales");
        engineering = pm.getObjectById(Department.class, "Engineering");
        assertEquals(sales, bugsBunny.getDepartment());
        assertTrue(sales.getPersons().contains(bugsBunny));
        assertFalse(engineering.getPersons().contains(bugsBunny));
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager)

Example 68 with Transaction

use of javax.jdo.Transaction in project tests by datanucleus.

the class ManyOneTest method testDeleteReferenceByDeletingObject.

public void testDeleteReferenceByDeletingObject() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        pm.getFetchPlan().addGroup(FetchPlan.ALL);
        tx.begin();
        Department engineering = pm.getObjectById(Department.class, "Engineering");
        Person bbunny = pm.getObjectById(Person.class, "Bugs Bunny");
        assertEquals(2, engineering.getPersons().size());
        assertTrue(engineering.getPersons().contains(bbunny));
        assertEquals(engineering, bbunny.getDepartment());
        // delete object
        pm.deletePersistent(bbunny);
        tx.commit();
        pm.close();
        // now check the removed reference
        pm = pmf.getPersistenceManager();
        pm.getFetchPlan().addGroup(FetchPlan.ALL);
        tx = pm.currentTransaction();
        tx.begin();
        engineering = pm.getObjectById(Department.class, "Engineering");
        assertEquals(1, engineering.getPersons().size());
        try {
            pm.getObjectById(Person.class, "Bugs Bunny");
            fail("Object 'Bugs Bunny' should not exist any more!");
        } catch (JDOObjectNotFoundException e) {
        // expected
        }
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException) PersistenceManager(javax.jdo.PersistenceManager)

Example 69 with Transaction

use of javax.jdo.Transaction in project tests by datanucleus.

the class ManyOneTest method testUpdateParentReferenceDetached.

public void testUpdateParentReferenceDetached() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        pm.getFetchPlan().addGroup("persons_of_department");
        tx.begin();
        Department engineering = pm.getObjectById(Department.class, "Engineering");
        Department sales = pm.getObjectById(Department.class, "Sales");
        Person bugsBunny = pm.getObjectById(Person.class, "Bugs Bunny");
        assertEquals(engineering, bugsBunny.getDepartment());
        Person detachedBugsBunny = pm.detachCopy(bugsBunny);
        Department detachedSales = pm.detachCopy(sales);
        tx.commit();
        pm.close();
        // set new parent reference
        detachedBugsBunny.setDepartment(detachedSales);
        detachedBugsBunny.setFirstName("BBB");
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        tx.begin();
        pm.makePersistent(detachedBugsBunny);
        tx.commit();
        pm.close();
        // now check the updated reference
        pm = pmf.getPersistenceManager();
        pm.getFetchPlan().addGroup("persons_of_department");
        tx = pm.currentTransaction();
        tx.begin();
        bugsBunny = pm.getObjectById(Person.class, "Bugs Bunny");
        sales = pm.getObjectById(Department.class, "Sales");
        engineering = pm.getObjectById(Department.class, "Engineering");
        assertEquals(sales, bugsBunny.getDepartment());
        assertEquals("BBB", bugsBunny.getFirstName());
        assertTrue(sales.getPersons().contains(bugsBunny));
        assertFalse(engineering.getPersons().contains(bugsBunny));
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager)

Example 70 with Transaction

use of javax.jdo.Transaction in project tests by datanucleus.

the class ApplicationIdPersistenceTest method testOptimisticVersionChecks.

/**
 * Test optimistic checking of surrogate version.
 */
public void testOptimisticVersionChecks() throws Exception {
    try {
        Object id = null;
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Organisation o = new Organisation("DataNucleus");
            o.setDescription("The company behind this software");
            pm.makePersistent(o);
            tx.commit();
            id = pm.getObjectId(o);
        } catch (Exception e) {
            LOG.error("Exception during persist", e);
            fail("Exception thrown when running test " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        PersistenceManager pm1 = pmf.getPersistenceManager();
        Transaction tx1 = pm1.currentTransaction();
        tx1.begin();
        Organisation o1 = (Organisation) pm1.getObjectById(id);
        PersistenceManager pm2 = pmf.getPersistenceManager();
        Transaction tx2 = pm2.currentTransaction();
        tx2.begin();
        Organisation o2 = (Organisation) pm2.getObjectById(id);
        // Update o1 in tx1 and commit it
        try {
            o1.setDescription("Global dataservices company");
            tx1.commit();
        } catch (Exception e) {
            LOG.error("Exception during retrieve/update in tx1", e);
            fail("Exception thrown when running test " + e.getMessage());
        } finally {
            if (tx1.isActive()) {
                tx1.rollback();
            }
            pm1.close();
        }
        // Update o2 in tx2 and (try to) commit it
        try {
            o2.setDescription("Global dataservices company number 2");
            tx2.commit();
            fail("Should have thrown JDOOptimisticVerificationException!");
        } catch (Exception e) {
            if (e instanceof JDOOptimisticVerificationException) {
            // Expected
            } else {
                LOG.error("Incorrect exception during update in tx2", e);
                fail("Incorrect exception thrown when running test " + e.getMessage());
            }
        } finally {
            if (tx2.isActive()) {
                tx2.rollback();
            }
            pm2.close();
        }
    } finally {
        clean(Organisation.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) Organisation(org.jpox.samples.models.company.Organisation) PersistenceManager(javax.jdo.PersistenceManager) JDOOptimisticVerificationException(javax.jdo.JDOOptimisticVerificationException) JDOException(javax.jdo.JDOException) JDOOptimisticVerificationException(javax.jdo.JDOOptimisticVerificationException) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException)

Aggregations

Transaction (javax.jdo.Transaction)1552 PersistenceManager (javax.jdo.PersistenceManager)1542 Query (javax.jdo.Query)635 List (java.util.List)395 JDOUserException (javax.jdo.JDOUserException)347 Collection (java.util.Collection)287 Iterator (java.util.Iterator)216 JDOObjectNotFoundException (javax.jdo.JDOObjectNotFoundException)183 JDOPersistenceManager (org.datanucleus.api.jdo.JDOPersistenceManager)156 Person (org.jpox.samples.models.company.Person)144 Employee (org.jpox.samples.models.company.Employee)122 Manager (org.jpox.samples.models.company.Manager)98 HashSet (java.util.HashSet)97 SQLException (java.sql.SQLException)80 ArrayList (java.util.ArrayList)77 JDOException (javax.jdo.JDOException)73 Extent (javax.jdo.Extent)70 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)68 JDODataStoreException (javax.jdo.JDODataStoreException)65 JDOFatalUserException (javax.jdo.JDOFatalUserException)65