Search in sources :

Example 61 with Transaction

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

the class ManyOneTest method testUpdateField.

public void testUpdateField() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        Person bbunny = pm.getObjectById(Person.class, "Bugs Bunny");
        bbunny.setFirstName("BBB");
        tx.commit();
        pm.close();
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        tx.begin();
        bbunny = pm.getObjectById(Person.class, "Bugs Bunny");
        assertEquals("BBB", bbunny.getFirstName());
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager)

Example 62 with Transaction

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

the class ManyOneTest method testSwapParentReference.

public void testSwapParentReference() {
    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");
        Person lamiPuxa = pm.getObjectById(Person.class, "Lami Puxa");
        assertEquals(2, engineering.getPersons().size());
        assertEquals(1, sales.getPersons().size());
        assertTrue(engineering.getPersons().contains(bugsBunny));
        assertEquals(engineering, bugsBunny.getDepartment());
        assertTrue(sales.getPersons().contains(lamiPuxa));
        assertEquals(sales, lamiPuxa.getDepartment());
        // swap references
        bugsBunny.setDepartment(sales);
        lamiPuxa.setDepartment(engineering);
        tx.commit();
        pm.close();
        // now check the swapped references
        pm = pmf.getPersistenceManager();
        pm.getFetchPlan().addGroup(FetchPlan.ALL);
        tx = pm.currentTransaction();
        tx.begin();
        engineering = pm.getObjectById(Department.class, "Engineering");
        sales = pm.getObjectById(Department.class, "Sales");
        bugsBunny = pm.getObjectById(Person.class, "Bugs Bunny");
        lamiPuxa = pm.getObjectById(Person.class, "Lami Puxa");
        assertEquals(2, engineering.getPersons().size());
        assertEquals(1, sales.getPersons().size());
        assertTrue(engineering.getPersons().contains(lamiPuxa));
        assertTrue(sales.getPersons().contains(bugsBunny));
        assertEquals(engineering, lamiPuxa.getDepartment());
        assertEquals(sales, bugsBunny.getDepartment());
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager)

Example 63 with Transaction

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

the class ManyOneTest method testPersistWithNoChild.

/**
 * Only the parent object is persisted, the reference to the child object is empty.
 */
public void testPersistWithNoChild() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        Company c = new Company("company");
        pm.makePersistent(c);
        tx.commit();
        helper.ids.add(pm.getObjectId(c));
        pm.close();
        // test
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        tx.begin();
        c = pm.getObjectById(Company.class, "company");
        assertNotNull(c.getDepartments());
        assertTrue(c.getDepartments().isEmpty());
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager)

Example 64 with Transaction

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

the class ManyOneTest method testRemoveChildReference.

public void testRemoveChildReference() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        pm.getFetchPlan().addGroup(FetchPlan.ALL);
        tx.begin();
        Department engineering = pm.getObjectById(Department.class, "Engineering");
        Person bugsBunny = pm.getObjectById(Person.class, "Bugs Bunny");
        assertEquals(2, engineering.getPersons().size());
        // remove a child reference
        engineering.getPersons().remove(bugsBunny);
        bugsBunny.setDepartment(null);
        tx.commit();
        pm.close();
        // now check the removed child 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());
        // check object doesn't exist
        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 65 with Transaction

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

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