Search in sources :

Example 86 with Transaction

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

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

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

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

Example 90 with Transaction

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

the class OneOneTest method testNullOutChildReference.

public void testNullOutChildReference() {
    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.setAccount(null);
        daffyDuck.setAddress(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)

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