Search in sources :

Example 81 with Transaction

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

the class JDOQLTest method testFilterSingle.

/**
 * Query with a simple filter with one clause ("field == value")
 * @throws Exception
 */
public void testFilterSingle() throws Exception {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Person p1 = new Person();
            p1.setPersonNum(1);
            p1.setGlobalNum("1");
            p1.setFirstName("Bugs");
            p1.setLastName("Bunny");
            Person p2 = new Person();
            p2.setPersonNum(2);
            p2.setGlobalNum("2");
            p2.setFirstName("Daffy");
            p2.setLastName("Duck");
            Employee e3 = new Employee();
            e3.setFirstName("Barney");
            e3.setLastName("Rubble");
            e3.setPersonNum(103);
            e3.setGlobalNum("103");
            e3.setSalary(124.50f);
            pm.makePersistent(p1);
            pm.makePersistent(p2);
            pm.makePersistent(e3);
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception during persist", e);
            fail("Exception thrown when running test " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Query q1 = pm.newQuery("SELECT FROM " + Person.class.getName() + " WHERE firstName == 'Daffy'");
            List<Person> results1 = (List<Person>) q1.execute();
            assertEquals(1, results1.size());
            Iterator<Person> iter = results1.iterator();
            Person p = iter.next();
            assertEquals("Daffy", p.getFirstName());
            assertEquals("Duck", p.getLastName());
            assertEquals(2, p.getPersonNum());
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception during query", e);
            fail("Exception thrown when running test " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        clean(Employee.class);
        clean(Person.class);
    }
}
Also used : Employee(org.jpox.samples.models.company.Employee) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) List(java.util.List) Person(org.jpox.samples.models.company.Person)

Example 82 with Transaction

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

the class JDOQLTest method testCandidateWithSubclasses.

public void testCandidateWithSubclasses() throws Exception {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Person p1 = new Person();
            p1.setPersonNum(1);
            p1.setGlobalNum("1");
            p1.setFirstName("Bugs");
            p1.setLastName("Bunny");
            Person p2 = new Person();
            p2.setPersonNum(2);
            p2.setGlobalNum("2");
            p2.setFirstName("Daffy");
            p2.setLastName("Duck");
            Employee e3 = new Employee();
            e3.setFirstName("Barney");
            e3.setLastName("Rubble");
            e3.setPersonNum(103);
            e3.setGlobalNum("103");
            e3.setSalary(124.50f);
            pm.makePersistent(p1);
            pm.makePersistent(p2);
            pm.makePersistent(e3);
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception during persist", e);
            fail("Exception thrown when running test " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            // Check number of objects present
            Query q1 = pm.newQuery(pm.getExtent(Person.class, true));
            List<Person> results1 = (List<Person>) q1.execute();
            assertEquals(3, results1.size());
            Iterator<Person> iter = results1.iterator();
            int numEmployees = 0;
            int numPeople = 0;
            while (iter.hasNext()) {
                Person p = iter.next();
                if (p instanceof Employee) {
                    numEmployees++;
                } else {
                    numPeople++;
                }
            }
            assertEquals("Number of Employees wrong", 1, numEmployees);
            assertEquals("Number of Person wrong", 2, numPeople);
            Query q2 = pm.newQuery(pm.getExtent(Person.class, false));
            List<Person> results2 = (List<Person>) q2.execute();
            assertEquals(2, results2.size());
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception during 1-N retrieve and check", e);
            fail("Exception thrown when running test " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        clean(Employee.class);
        clean(Person.class);
    }
}
Also used : Employee(org.jpox.samples.models.company.Employee) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) List(java.util.List) Person(org.jpox.samples.models.company.Person)

Example 83 with Transaction

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

the class JDOQLTest method testFilterOneToOne.

/**
 * Query with a filter on 1-1 relation value.
 * @throws Exception
 */
public void testFilterOneToOne() throws Exception {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        Object p1Id = null;
        try {
            tx.begin();
            Person p1 = new Person();
            p1.setPersonNum(1);
            p1.setGlobalNum("1");
            p1.setFirstName("Bugs");
            p1.setLastName("Bunny");
            Person p2 = new Person();
            p2.setPersonNum(2);
            p2.setGlobalNum("2");
            p2.setFirstName("Daffy");
            p2.setLastName("Duck");
            p2.setBestFriend(p1);
            pm.makePersistent(p1);
            pm.makePersistent(p2);
            tx.commit();
            p1Id = pm.getObjectId(p1);
        } catch (Exception e) {
            LOG.error("Exception during persist", e);
            fail("Exception thrown when running test " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Person p1 = (Person) pm.getObjectById(p1Id);
            Query q1 = pm.newQuery("SELECT FROM " + Person.class.getName() + " WHERE bestFriend == :p");
            List<Person> results1 = (List<Person>) q1.execute(p1);
            assertEquals(1, results1.size());
            Iterator<Person> iter = results1.iterator();
            Person p = iter.next();
            assertEquals("Daffy", p.getFirstName());
            assertEquals("Duck", p.getLastName());
            assertEquals(2, p.getPersonNum());
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception during query", e);
            fail("Exception thrown when running test " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        clean(Employee.class);
        clean(Person.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) List(java.util.List) Person(org.jpox.samples.models.company.Person)

Example 84 with Transaction

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

the class JDOQLTest method testFilterUsingAndNestedAnd.

/**
 * Query with a simple filter with two ANDed clauses
 * "(field >= value1 && field < value2)" and "(field >= value3 && field < value4)".
 * @throws Exception
 */
public void testFilterUsingAndNestedAnd() throws Exception {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Person p1 = new Person();
            p1.setPersonNum(1);
            p1.setGlobalNum("1");
            p1.setFirstName("Bugs");
            p1.setLastName("Bunny");
            p1.setAge(15);
            Person p2 = new Person();
            p2.setPersonNum(2);
            p2.setGlobalNum("2");
            p2.setFirstName("Daffy");
            p2.setLastName("Duck");
            p2.setAge(16);
            Employee e3 = new Employee();
            e3.setFirstName("Barney");
            e3.setLastName("Rubble");
            e3.setPersonNum(103);
            e3.setGlobalNum("103");
            e3.setSalary(124.50f);
            e3.setAge(18);
            pm.makePersistent(p1);
            pm.makePersistent(p2);
            pm.makePersistent(e3);
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception during persist", e);
            fail("Exception thrown when running test " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Query q1 = pm.newQuery("SELECT FROM " + Person.class.getName() + " WHERE (personNum >= 1 && personNum <=2) && (age >= 14 && age < 18)");
            List<Person> results1 = (List<Person>) q1.execute();
            assertEquals(2, results1.size());
            Iterator<Person> iter = results1.iterator();
            Person p1 = iter.next();
            Person p2 = iter.next();
            boolean daffy = false;
            boolean bugs = false;
            if (p1.getPersonNum() == 1) {
                assertEquals("Bugs", p1.getFirstName());
                assertEquals("Bunny", p1.getLastName());
                assertEquals(1, p1.getPersonNum());
                bugs = true;
            } else {
                assertEquals("Daffy", p1.getFirstName());
                assertEquals("Duck", p1.getLastName());
                assertEquals(2, p1.getPersonNum());
                daffy = true;
            }
            if (p2.getPersonNum() == 1) {
                assertEquals("Bugs", p2.getFirstName());
                assertEquals("Bunny", p2.getLastName());
                assertEquals(1, p2.getPersonNum());
                bugs = true;
            } else {
                assertEquals("Daffy", p2.getFirstName());
                assertEquals("Duck", p2.getLastName());
                assertEquals(2, p2.getPersonNum());
                daffy = true;
            }
            assertTrue("Bugs not present in results!", bugs);
            assertTrue("Daffy not present in results!", daffy);
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception during query", e);
            fail("Exception thrown when running test " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        clean(Employee.class);
        clean(Person.class);
    }
}
Also used : Employee(org.jpox.samples.models.company.Employee) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) List(java.util.List) Person(org.jpox.samples.models.company.Person)

Example 85 with Transaction

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

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