Search in sources :

Example 96 with Transaction

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

the class OneOneTest method testPersistChildOnly.

/**
 * Persist an object with an hierarchical mapped relation. When persisting the child object the parent object must
 * be persisted *before*.
 */
public void testPersistChildOnly() {
    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(dduck);
        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());
        assertEquals("dduck", daffyDuck.getAccount().getUid());
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager)

Example 97 with Transaction

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

the class TestHelper method tearDown.

public void tearDown(PersistenceManagerFactory pmf) throws Exception {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        for (Object id : ids) {
            try {
                pm.deletePersistent(pm.getObjectById(id));
            } catch (Exception e) {
            }
        }
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager)

Example 98 with Transaction

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

the class TestHelper method setUp.

public void setUp(PersistenceManagerFactory pmf) throws Exception {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        Company jdo = new Company("JDO Inc.");
        pm.makePersistent(jdo);
        Department engineering = new Department("Engineering", jdo);
        Department sales = new Department("Sales", jdo);
        pm.makePersistent(engineering);
        pm.makePersistent(sales);
        Account bbunny = new AccountWithPassword("bbunny", "secret", null);
        Account ahicks = new Account("ahicks", null);
        Account lpuxa = new AccountWithPassword("lpuxa", "secret", null);
        Address bbAddress = new Address("B-City", "B-Street", null);
        Address ahAddress = new Address("A-City", "A-Street", null);
        Address lpAddress = new Address("L-City", "L-Street", null);
        Person bugsBunny = new Person("Bugs", "Bunny", "Bugs Bunny", bbunny, bbAddress, engineering);
        bbunny.setPerson(bugsBunny);
        bbAddress.setPerson(bugsBunny);
        engineering.getPersons().add(bugsBunny);
        Person anaHicks = new Person("Ana", "Hicks", "Ana Hicks", ahicks, ahAddress, engineering);
        ahicks.setPerson(anaHicks);
        ahAddress.setPerson(anaHicks);
        engineering.getPersons().add(anaHicks);
        Person lamiPuxa = new Person("Lami", "Puxa", "Lami Puxa", lpuxa, lpAddress, sales);
        lpuxa.setPerson(lamiPuxa);
        lpAddress.setPerson(lamiPuxa);
        sales.getPersons().add(lamiPuxa);
        pm.makePersistent(bugsBunny);
        pm.makePersistent(anaHicks);
        pm.makePersistent(lamiPuxa);
        ids.add(pm.getObjectId(bugsBunny));
        ids.add(pm.getObjectId(anaHicks));
        ids.add(pm.getObjectId(lamiPuxa));
        ids.add(pm.getObjectId(engineering));
        ids.add(pm.getObjectId(sales));
        ids.add(pm.getObjectId(jdo));
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager)

Example 99 with Transaction

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

the class JDQLBasicTest method testBasicQuery.

public void testBasicQuery() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        Collection c = (Collection) pm.newQuery(Person.class).execute();
        assertEquals(3, c.size());
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) Collection(java.util.Collection)

Example 100 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 bugs = pm.getObjectById(Person.class, "Bugs Bunny");
        bugs.setFirstName("BBB");
        tx.commit();
        pm.close();
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        tx.begin();
        bugs = pm.getObjectById(Person.class, "Bugs Bunny");
        assertEquals("BBB", bugs.getFirstName());
        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