Search in sources :

Example 71 with PersistenceManager

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

the class ManyOneTest method testRemoveChildReferenceDetached.

public void testRemoveChildReferenceDetached() {
    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());
        pm.getFetchPlan().setMaxFetchDepth(-1);
        Person detachedBugsBunny = pm.detachCopy(bugsBunny);
        Department detachedEngineering = pm.detachCopy(engineering);
        tx.commit();
        pm.close();
        // remove a child reference
        detachedEngineering.getPersons().remove(detachedBugsBunny);
        // detachedBugsBunny.setDepartment(null);
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        tx.begin();
        pm.makePersistent(detachedEngineering);
        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 72 with PersistenceManager

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

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

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

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

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