Search in sources :

Example 76 with PersistenceManager

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

the class ManyOneTest method testUpdateChildReferenceDetached.

public void testUpdateChildReferenceDetached() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        pm.getFetchPlan().addGroup(FetchPlan.ALL);
        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());
        assertEquals(2, engineering.getPersons().size());
        assertEquals(1, sales.getPersons().size());
        Person detachedBugsBunny = pm.detachCopy(bugsBunny);
        Department detachedSales = pm.detachCopy(sales);
        Department detachedEngineering = pm.detachCopy(engineering);
        tx.commit();
        pm.close();
        // set new child references
        detachedSales.getPersons().add(detachedBugsBunny);
        detachedEngineering.getPersons().remove(detachedBugsBunny);
        detachedBugsBunny.setDepartment(detachedSales);
        detachedBugsBunny.setFirstName("BBB");
        pm = pmf.getPersistenceManager();
        pm.getFetchPlan().addGroup(FetchPlan.ALL);
        tx = pm.currentTransaction();
        tx.begin();
        pm.makePersistent(detachedBugsBunny);
        tx.commit();
        pm.close();
        // now check the updated reference
        pm = pmf.getPersistenceManager();
        pm.getFetchPlan().addGroup(FetchPlan.ALL);
        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());
        assertEquals(2, sales.getPersons().size());
        assertTrue(sales.getPersons().contains(bugsBunny));
        assertEquals(1, engineering.getPersons().size());
        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 77 with PersistenceManager

use of javax.jdo.PersistenceManager 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 78 with PersistenceManager

use of javax.jdo.PersistenceManager 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 79 with PersistenceManager

use of javax.jdo.PersistenceManager 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 80 with PersistenceManager

use of javax.jdo.PersistenceManager 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)

Aggregations

PersistenceManager (javax.jdo.PersistenceManager)1664 Transaction (javax.jdo.Transaction)1542 Query (javax.jdo.Query)671 List (java.util.List)397 JDOUserException (javax.jdo.JDOUserException)352 Collection (java.util.Collection)309 Iterator (java.util.Iterator)239 JDOObjectNotFoundException (javax.jdo.JDOObjectNotFoundException)185 JDOPersistenceManager (org.datanucleus.api.jdo.JDOPersistenceManager)170 Person (org.jpox.samples.models.company.Person)146 Employee (org.jpox.samples.models.company.Employee)128 Manager (org.jpox.samples.models.company.Manager)107 HashSet (java.util.HashSet)101 ArrayList (java.util.ArrayList)85 SQLException (java.sql.SQLException)82 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)77 JDOException (javax.jdo.JDOException)74 Extent (javax.jdo.Extent)72 JDOFatalUserException (javax.jdo.JDOFatalUserException)68 JDODataStoreException (javax.jdo.JDODataStoreException)65