Search in sources :

Example 96 with PersistenceManager

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

the class OneOneTest method testPersistWithNoChild.

/**
 * Only the parent object is persisted, the reference to the child object is null.
 */
public void testPersistWithNoChild() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        Person daffyDuck = new Person("Daffy", "Duck", "Daffy Duck", null, null, getDepartment(pm));
        pm.makePersistent(daffyDuck);
        helper.ids.add(pm.getObjectId(daffyDuck));
        tx.commit();
        pm.close();
        // test
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        tx.begin();
        daffyDuck = pm.getObjectById(Person.class, "Daffy Duck");
        assertNull(daffyDuck.getAccount());
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager)

Example 97 with PersistenceManager

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

the class OneOneTest method testSwapParentReferenceDetached.

/**
 * Swap the parent reference of the child objects.
 */
public void testSwapParentReferenceDetached() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        Person bugsBunny = pm.getObjectById(Person.class, "Bugs Bunny");
        Person anaHicks = pm.getObjectById(Person.class, "Ana Hicks");
        Account bbunny = pm.getObjectById(AccountWithPassword.class, "bbunny");
        Account ahicks = pm.getObjectById(Account.class, "ahicks");
        assertNotNull(bugsBunny.getAccount());
        assertNotNull(anaHicks.getAccount());
        assertNotNull(bbunny.getPerson());
        assertNotNull(ahicks.getPerson());
        assertEquals(bbunny, bugsBunny.getAccount());
        assertEquals(ahicks, anaHicks.getAccount());
        assertEquals(bugsBunny, bbunny.getPerson());
        assertEquals(anaHicks, ahicks.getPerson());
        pm.getFetchPlan().setMaxFetchDepth(-1);
        Person detachedBugsBunny = pm.detachCopy(bugsBunny);
        Person detachedAnaHicks = pm.detachCopy(anaHicks);
        Account detachedBbunny = pm.detachCopy(bbunny);
        Account detachedAhicks = pm.detachCopy(ahicks);
        tx.commit();
        pm.close();
        // swap references
        detachedBbunny.setPerson(detachedAnaHicks);
        detachedAnaHicks.setAccount(detachedBbunny);
        detachedAhicks.setPerson(detachedBugsBunny);
        detachedBugsBunny.setAccount(detachedAhicks);
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        tx.begin();
        pm.makePersistent(detachedBbunny);
        pm.makePersistent(detachedAhicks);
        tx.commit();
        pm.close();
        // now check the updated reference
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        tx.begin();
        bugsBunny = pm.getObjectById(Person.class, "Bugs Bunny");
        anaHicks = pm.getObjectById(Person.class, "Ana Hicks");
        bbunny = pm.getObjectById(AccountWithPassword.class, "bbunny");
        ahicks = pm.getObjectById(Account.class, "ahicks");
        assertNotNull(bugsBunny.getAccount());
        assertNotNull(anaHicks.getAccount());
        assertNotNull(bbunny.getPerson());
        assertNotNull(ahicks.getPerson());
        assertEquals(ahicks, bugsBunny.getAccount());
        assertEquals(bbunny, anaHicks.getAccount());
        assertEquals(anaHicks, bbunny.getPerson());
        assertEquals(bugsBunny, ahicks.getPerson());
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager)

Example 98 with PersistenceManager

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

the class OneOneTest method testNullOutParentReference.

public void testNullOutParentReference() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        Address ddAddress = new Address("D-City", "D-Street", null);
        Account dduck = new AccountWithPassword("dduck", "secret", null);
        Person daffyDuck = new Person("Daffy", "Duck", "Daffy Duck", dduck, ddAddress, getDepartment(pm));
        ddAddress.setPerson(daffyDuck);
        dduck.setPerson(daffyDuck);
        pm.makePersistent(daffyDuck);
        pm.makePersistent(dduck);
        pm.makePersistent(ddAddress);
        helper.ids.add(pm.getObjectId(ddAddress));
        helper.ids.add(pm.getObjectId(dduck));
        helper.ids.add(pm.getObjectId(daffyDuck));
        tx.commit();
        pm.close();
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        tx.begin();
        daffyDuck = pm.getObjectById(Person.class, "Daffy Duck");
        assertNotNull(daffyDuck.getAccount());
        // set to null
        daffyDuck.getAccount().setPerson(null);
        daffyDuck.getAddress().setPerson(null);
        tx.commit();
        pm.close();
        // now check deleted reference
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        tx.begin();
        daffyDuck = pm.getObjectById(Person.class, "Daffy Duck");
        assertNull(daffyDuck.getAccount());
        assertNull(daffyDuck.getAddress());
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager)

Example 99 with PersistenceManager

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

the class OneOneTest method testUpdateChildReferenceDetached.

/**
 * Update the child reference of the parent object.
 */
public void testUpdateChildReferenceDetached() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        // create account
        tx.begin();
        Account dduck = new Account("dduck", null);
        Person daffyDuck = new Person("Daffy", "Duck", "Daffy Duck", dduck, null, getDepartment(pm));
        dduck.setPerson(daffyDuck);
        pm.makePersistent(daffyDuck);
        pm.makePersistent(dduck);
        tx.commit();
        helper.ids.add(pm.getObjectId(dduck));
        helper.ids.add(pm.getObjectId(daffyDuck));
        pm.close();
        // detach
        pm = pmf.getPersistenceManager();
        pm.getFetchPlan().setMaxFetchDepth(-1);
        tx = pm.currentTransaction();
        tx.begin();
        Account detachedBbunny = pm.detachCopy(pm.getObjectById(AccountWithPassword.class, "bbunny"));
        Account detachedDduck = pm.detachCopy(pm.getObjectById(Account.class, "dduck"));
        Person detachedDaffyDuck = pm.detachCopy(pm.getObjectById(Person.class, "Daffy Duck"));
        tx.commit();
        pm.close();
        // set a new account
        detachedDaffyDuck.setAccount(detachedBbunny);
        detachedBbunny.setPerson(detachedDaffyDuck);
        detachedDduck.setPerson(null);
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        tx.begin();
        pm.makePersistent(detachedDaffyDuck);
        pm.deletePersistent(detachedDduck);
        tx.commit();
        pm.close();
        // now check the updated reference
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        tx.begin();
        daffyDuck = pm.getObjectById(Person.class, "Daffy Duck");
        assertNotNull(daffyDuck.getAccount());
        assertEquals("bbunny", daffyDuck.getAccount().getUid());
        Person bugsBunny = pm.getObjectById(Person.class, "Bugs Bunny");
        assertNull(bugsBunny.getAccount());
        try {
            pm.getObjectById(Account.class, "dduck");
            fail("Object 'dduck' 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 100 with PersistenceManager

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

the class OneOneTest method testSwapChildReference.

/**
 * Swap the child reference of the parent object.
 */
public void testSwapChildReference() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        Person bugsBunny = pm.getObjectById(Person.class, "Bugs Bunny");
        Person anaHicks = pm.getObjectById(Person.class, "Ana Hicks");
        Account bbunny = pm.getObjectById(AccountWithPassword.class, "bbunny");
        Account ahicks = pm.getObjectById(Account.class, "ahicks");
        assertNotNull(bugsBunny.getAccount());
        assertNotNull(anaHicks.getAccount());
        assertNotNull(bbunny.getPerson());
        assertNotNull(ahicks.getPerson());
        assertEquals(bbunny, bugsBunny.getAccount());
        assertEquals(ahicks, anaHicks.getAccount());
        assertEquals(bugsBunny, bbunny.getPerson());
        assertEquals(anaHicks, ahicks.getPerson());
        // swap references
        bugsBunny.setAccount(ahicks);
        anaHicks.setAccount(bbunny);
        ahicks.setPerson(bugsBunny);
        bbunny.setPerson(anaHicks);
        tx.commit();
        helper.ids.remove(pm.getObjectId(bugsBunny));
        helper.ids.add(pm.getObjectId(bugsBunny));
        helper.ids.remove(pm.getObjectId(anaHicks));
        helper.ids.add(pm.getObjectId(anaHicks));
        pm.close();
        // now check the updated reference
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        tx.begin();
        bugsBunny = pm.getObjectById(Person.class, "Bugs Bunny");
        anaHicks = pm.getObjectById(Person.class, "Ana Hicks");
        bbunny = pm.getObjectById(AccountWithPassword.class, "bbunny");
        ahicks = pm.getObjectById(Account.class, "ahicks");
        assertNotNull(bugsBunny.getAccount());
        assertNotNull(anaHicks.getAccount());
        assertNotNull(bbunny.getPerson());
        assertNotNull(ahicks.getPerson());
        assertEquals(ahicks, bugsBunny.getAccount());
        assertEquals(bbunny, anaHicks.getAccount());
        assertEquals(anaHicks, bbunny.getPerson());
        assertEquals(bugsBunny, ahicks.getPerson());
        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