Search in sources :

Example 91 with Transaction

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

the class OneOneTest method testSwapParentReference.

/**
 * Swap the parent reference of the child objects.
 */
public void testSwapParentReference() {
    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
        bbunny.setPerson(anaHicks);
        anaHicks.setAccount(bbunny);
        ahicks.setPerson(bugsBunny);
        bugsBunny.setAccount(ahicks);
        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 92 with Transaction

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

the class OneOneTest method testPersist.

/**
 * Persist an object with an hierarchical mapped relation.
 */
public void testPersist() {
    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();
        // test
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        tx.begin();
        daffyDuck = pm.getObjectById(Person.class, "Daffy Duck");
        assertNotNull(daffyDuck.getAccount());
        assertTrue(daffyDuck.getAccount() instanceof AccountWithPassword);
        assertEquals("dduck", daffyDuck.getAccount().getUid());
        assertNotNull(daffyDuck.getAddress());
        assertEquals("D-Street", daffyDuck.getAddress().getStreet());
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager)

Example 93 with Transaction

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

the class OneOneTest method testUpdateChildReference.

/**
 * Update the child reference of the parent object.
 */
public void testUpdateChildReference() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        Account dduck = new AccountWithPassword("dduck", "secret", 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();
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        tx.begin();
        daffyDuck = pm.getObjectById(Person.class, "Daffy Duck");
        assertNotNull(daffyDuck.getAccount());
        assertEquals("dduck", daffyDuck.getAccount().getUid());
        // set a new account
        Account bbunny = pm.getObjectById(AccountWithPassword.class, "bbunny");
        daffyDuck.setAccount(bbunny);
        bbunny.setPerson(daffyDuck);
        tx.commit();
        helper.ids.remove(pm.getObjectId(daffyDuck));
        helper.ids.add(pm.getObjectId(daffyDuck));
        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());
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager)

Example 94 with Transaction

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

the class OneOneTest method testUpdateParentReference.

/**
 * Update the parent reference of the child object.
 */
public void testUpdateParentReference() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        Account dduck = new AccountWithPassword("dduck", "secret", 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();
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        tx.begin();
        daffyDuck = pm.getObjectById(Person.class, "Daffy Duck");
        dduck = pm.getObjectById(AccountWithPassword.class, "dduck");
        assertNotNull(dduck.getPerson());
        assertEquals("Daffy Duck", dduck.getPerson().getFullName());
        // set a new person
        Person bugsBunny = pm.getObjectById(Person.class, "Bugs Bunny");
        dduck.setPerson(bugsBunny);
        tx.commit();
        helper.ids.remove(pm.getObjectId(bugsBunny));
        helper.ids.add(pm.getObjectId(bugsBunny));
        pm.close();
        // now check the updated reference
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        tx.begin();
        dduck = pm.getObjectById(AccountWithPassword.class, "dduck");
        assertNotNull(dduck.getPerson());
        assertEquals("Bugs Bunny", dduck.getPerson().getFullName());
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager)

Example 95 with Transaction

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

the class OneOneTest method testDeleteReferenceByDeletingObject.

public void testDeleteReferenceByDeletingObject() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        Account dduck = new AccountWithPassword("dduck", "secret", null);
        Person daffyDuck = new Person("Daffy", "Duck", "Daffy Duck", dduck, null, getDepartment(pm));
        dduck.setPerson(daffyDuck);
        pm.makePersistent(daffyDuck);
        pm.makePersistent(dduck);
        helper.ids.add(pm.getObjectId(dduck));
        helper.ids.add(pm.getObjectId(daffyDuck));
        tx.commit();
        pm.close();
        // delete referenced object
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        tx.begin();
        daffyDuck = pm.getObjectById(Person.class, "Daffy Duck");
        assertNotNull(daffyDuck.getAccount());
        pm.deletePersistent(daffyDuck.getAccount());
        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());
        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