Search in sources :

Example 86 with Employee

use of org.jpox.samples.models.company.Employee in project tests by datanucleus.

the class JDOQLTest method testResultAggregate.

public void testResultAggregate() 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();
        }
        pmf.getDataStoreCache().evictAll();
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Query q2 = pm.newQuery("SELECT MAX(this.personNum), MIN(this.personNum) FROM " + Person.class.getName());
            Object[] results2 = (Object[]) q2.execute();
            assertEquals(2, results2.length);
            assertEquals(103l, results2[0]);
            assertEquals(1l, results2[1]);
            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) Person(org.jpox.samples.models.company.Person)

Example 87 with Employee

use of org.jpox.samples.models.company.Employee in project tests by datanucleus.

the class JDOQLTest method testMathSin.

public void testMathSin() throws Exception {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Employee e = new Employee();
            e.setFirstName("Barney");
            e.setLastName("Rubble");
            e.setPersonNum(103);
            e.setGlobalNum("103");
            e.setSalary(124.50f);
            pm.makePersistent(e);
            pm.flush();
            Query q1 = pm.newQuery("SELECT FROM " + Employee.class.getName() + " WHERE Math.sin(:param) == this.salary");
            Map<String, Object> params = new HashMap<>();
            params.put("param", 0.0);
            q1.setNamedParameters(params);
            List<Employee> results1 = (List<Employee>) q1.executeList();
            assertEquals(0, results1.size());
            tx.rollback();
        } catch (Exception e) {
            LOG.error("Exception during JDOQL equality operator test", e);
            fail("Exception thrown when running test " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
    }
}
Also used : Employee(org.jpox.samples.models.company.Employee) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) HashMap(java.util.HashMap) List(java.util.List)

Example 88 with Employee

use of org.jpox.samples.models.company.Employee in project tests by datanucleus.

the class JDOQLTest method testFilterEquals.

public void testFilterEquals() throws Exception {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Employee e = new Employee();
            e.setFirstName("Barney");
            e.setLastName("Rubble");
            e.setPersonNum(103);
            e.setGlobalNum("103");
            e.setSalary(124.50f);
            pm.makePersistent(e);
            pm.flush();
            // Query using equality operator
            Query q1 = pm.newQuery("SELECT FROM " + Employee.class.getName() + " WHERE this.firstName == 'Barney'");
            List<Employee> results1 = (List<Employee>) q1.execute();
            assertEquals(1, results1.size());
            Employee e1 = results1.iterator().next();
            assertEquals("Wrong Employee", "Rubble", e1.getLastName());
            tx.rollback();
        } catch (Exception e) {
            LOG.error("Exception during JDOQL equality operator test", e);
            fail("Exception thrown when running test " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
    }
}
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)

Example 89 with Employee

use of org.jpox.samples.models.company.Employee in project tests by datanucleus.

the class JDOQLTest method testFilterWithNullParameter.

/**
 * Query with a simple filter with one clause ("field == :param")
 * @throws Exception
 */
public void testFilterWithNullParameter() 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(null);
            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 lastName == :name");
            List<Person> results1 = (List<Person>) q1.execute(null);
            assertEquals(1, results1.size());
            Iterator<Person> iter = results1.iterator();
            Person p = iter.next();
            assertEquals("Daffy", p.getFirstName());
            assertNull(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 90 with Employee

use of org.jpox.samples.models.company.Employee in project tests by datanucleus.

the class JDOQLTest method testFilterUsingOrNestedAnd.

/**
 * Query with a simple filter with two ORed clauses
 * "(field >= value1 && field < value2)" and "(field >= value3 && field < value4)".
 * @throws Exception
 */
public void testFilterUsingOrNestedAnd() 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 (personNum >= 1 && personNum < 2) || (personNum >= 100 && personNum < 200)");
            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 barney = false;
            boolean bugs = false;
            if (p1.getPersonNum() == 1) {
                assertEquals("Bugs", p1.getFirstName());
                assertEquals("Bunny", p1.getLastName());
                assertEquals(1, p1.getPersonNum());
                bugs = true;
            } else {
                assertEquals("Barney", p1.getFirstName());
                assertEquals("Rubble", p1.getLastName());
                assertEquals(103, p1.getPersonNum());
                barney = true;
            }
            if (p2.getPersonNum() == 1) {
                assertEquals("Bugs", p2.getFirstName());
                assertEquals("Bunny", p2.getLastName());
                assertEquals(1, p2.getPersonNum());
                bugs = true;
            } else {
                assertEquals("Barney", p2.getFirstName());
                assertEquals("Rubble", p2.getLastName());
                assertEquals(103, p2.getPersonNum());
                barney = true;
            }
            assertTrue("Bugs not present in results!", bugs);
            assertTrue("Barney not present in results!", barney);
            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)

Aggregations

Employee (org.jpox.samples.models.company.Employee)129 PersistenceManager (javax.jdo.PersistenceManager)126 Transaction (javax.jdo.Transaction)119 Query (javax.jdo.Query)68 JDOUserException (javax.jdo.JDOUserException)62 Manager (org.jpox.samples.models.company.Manager)46 List (java.util.List)44 JDOPersistenceManager (org.datanucleus.api.jdo.JDOPersistenceManager)37 JDOObjectNotFoundException (javax.jdo.JDOObjectNotFoundException)24 Person (org.jpox.samples.models.company.Person)24 Collection (java.util.Collection)21 PersistenceManagerFactory (javax.jdo.PersistenceManagerFactory)18 Iterator (java.util.Iterator)17 Properties (java.util.Properties)17 JDODetachedFieldAccessException (javax.jdo.JDODetachedFieldAccessException)16 SQLException (java.sql.SQLException)12 StoreManager (org.datanucleus.store.StoreManager)12 Department (org.jpox.samples.models.company.Department)12 ArrayList (java.util.ArrayList)11 HashSet (java.util.HashSet)11