Search in sources :

Example 36 with Employee

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

the class JDOQLQueryTest method testAPISubquery.

/**
 * Test a subquery using the JDOQL Query API.
 */
public void testAPISubquery() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        // Persist 2 Employees
        Employee emp1 = new Employee(101, "F1", "S1", "f1.s1@company.com", 100f, "10001");
        Employee emp2 = new Employee(102, "F2", "S2", "f2.s2@company.com", 200f, "10002");
        pm.makePersistent(emp1);
        pm.makePersistent(emp2);
        pm.flush();
        // Find the Employees earning more than the average salary
        LOG.info(">> Querying for Employees with salary above the average, and the average is defined by subquery");
        Query q = pm.newQuery(Employee.class, "salary > averageSalary");
        q.declareVariables("double averageSalary");
        Query averageSalaryQuery = pm.newQuery("SELECT avg(salary) FROM " + Employee.class.getName());
        q.addSubquery(averageSalaryQuery, "double averageSalary", null);
        List results = (List) q.execute();
        assertNotNull("No results from query!", results);
        assertEquals("Number of Employees with more than average salary was wrong", 1, results.size());
        tx.commit();
    } catch (JDOUserException e) {
        e.printStackTrace();
        fail(e.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
        clean(Employee.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) JDOUserException(javax.jdo.JDOUserException)

Example 37 with Employee

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

the class JDOQLBasicTest method testSingleString.

/**
 * Test case to use JDOQL single-string
 */
public void testSingleString() {
    try {
        Employee woody = new Employee(1, "Woody", "Woodpecker", "woody@woodpecker.com", 13, "serial 1");
        Employee bart = new Employee(2, "Bart", "Simpson", "bart@simpson.com", 2, "serial 2");
        // Eh, what's up, doc?
        Employee bunny = new Employee(3, "Bugs", "Bunny", "bugs.bunny@warnerbros.com", 12, "serial 3");
        // Meep! Meep!
        Employee roadrunner = new Employee(4, "Road", "Runner", "road.runner@warnerbros.com", 11, "serial 4");
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            pm.makePersistent(woody);
            pm.makePersistent(bart);
            pm.makePersistent(bunny);
            pm.makePersistent(roadrunner);
            tx.commit();
            tx.begin();
            try {
                Query q = pm.newQuery("SELECT UNIQUE FROM " + Employee.class.getName() + " EXCLUDE SUBCLASSES  WHERE firstName == \"Bugs\"");
                q.setClass(Employee.class);
                Object results = q.execute();
                assertTrue("UNIQUE query returned an object of an incorrect type : should have been Employee but was " + results, results instanceof Employee);
                q.closeAll();
                q = pm.newQuery("SELECT UNIQUE FROM " + Employee.class.getName() + " EXCLUDE SUBCLASSES  WHERE firstName == \"Bugs\" import " + Employee.class.getName());
                q.setClass(Employee.class);
                results = q.execute();
                assertTrue("UNIQUE query returned an object of an incorrect type : should have been Employee but was " + results, results instanceof Employee);
                q.closeAll();
            } catch (JDOUserException e) {
                fail(e.getMessage());
            }
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        clean(Employee.class);
    }
}
Also used : Employee(org.jpox.samples.models.company.Employee) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) JDOUserException(javax.jdo.JDOUserException)

Example 38 with Employee

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

the class JDOQLBasicTest method testSingleStringParameters.

/**
 * Test case to use JDOQL single-string parameters
 */
public void testSingleStringParameters() {
    try {
        Employee woody = new Employee(1, "Woody", "Woodpecker", "woody@woodpecker.com", 13, "serial 1");
        Employee bart = new Employee(2, "Bart", "Simpson", "bart@simpson.com", 2, "serial 2");
        // Eh, what's up, doc?
        Employee bunny = new Employee(3, "Bugs", "Bunny", "bugs.bunny@warnerbros.com", 12, "serial 3");
        // Meep! Meep!
        Employee roadrunner = new Employee(4, "Road", "Runner", "road.runner@warnerbros.com", 11, "serial 4");
        Manager bart2 = new Manager(5, "Bart", "Smith", "bart@smith.com", 4, "serial 5");
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            pm.makePersistent(woody);
            pm.makePersistent(bart);
            pm.makePersistent(bunny);
            pm.makePersistent(roadrunner);
            pm.makePersistent(bart2);
            tx.commit();
            tx.begin();
            Query q = pm.newQuery("SELECT FROM " + Employee.class.getName() + " EXCLUDE SUBCLASSES WHERE firstName == christianname PARAMETERS java.lang.String christianname");
            List results = (List) q.execute("Bart");
            assertEquals("Query returned incorrect number of objects from single-string parameter query : was " + results.size() + " but should have been 1", 1, results.size());
            q.closeAll();
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out our data
        CompanyHelper.clearCompanyData(pmf);
    }
}
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) ArrayList(java.util.ArrayList) Manager(org.jpox.samples.models.company.Manager) StoreManager(org.datanucleus.store.StoreManager) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager) PersistenceManager(javax.jdo.PersistenceManager)

Example 39 with Employee

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

the class JDOQLBasicTest method testStaticFields.

/**
 * Test for the use of the static fields in query method.
 */
public void testStaticFields() {
    try {
        Employee woody = new Employee(1, "Woody", "Woodpecker", "woody@woodpecker.com", 13, "serial 1");
        Employee bart = new Employee(2, "Bart", "Simpson", "bart@simpson.com", 2, "serial 2");
        // Eh, what's up, doc?
        Employee bunny = new Employee(3, "Bugs", "Bunny", "bugs.bunny@warnerbros.com", 12, "serial 3");
        // Beep! Beep!
        Employee roadrunner = new Employee(4, "Road", "Runner", "road.runner@warnerbros.com", 11, "serial 4");
        // I hate the gravity
        Employee coyote = new Employee(5, "Wile", "E. Coyote", "wile.coyote@acme.com", 9, "serial 5");
        // paranoid, and neurotic
        Employee duck = new Employee(6, "Daffy", "Duck", "daffy.duck@warnerbros.com", 7, "serial 6");
        // You are my peanut.
        Employee pepe = new Employee(7, "Pepe", "le Pew", "pepe.lepew@warnerbros.com", 8, "serial 7");
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            pm.makePersistent(woody);
            pm.makePersistent(bart);
            pm.makePersistent(bunny);
            pm.makePersistent(roadrunner);
            pm.makePersistent(coyote);
            pm.makePersistent(duck);
            pm.makePersistent(pepe);
            tx.commit();
            tx.begin();
            try {
                Query q = pm.newQuery(pm.getExtent(org.jpox.samples.models.company.Employee.class, false));
                q.setFilter("firstName == org.jpox.samples.models.company.Employee.FIRSTNAME");
                HashSet results = new HashSet((Collection) q.execute());
                assertEquals("received: " + results, 1, results.size());
                assertEquals(org.jpox.samples.models.company.Employee.FIRSTNAME, ((Person) results.iterator().next()).getFirstName());
                q.closeAll();
                q = pm.newQuery(pm.getExtent(org.jpox.samples.models.company.Employee.class, false));
                q.setFilter("firstName == org.jpox.samples.models.company.Person.FIRSTNAME");
                results = new HashSet((Collection) q.execute());
                assertEquals("received: " + results, 1, results.size());
                assertEquals(org.jpox.samples.models.company.Person.FIRSTNAME, ((Person) results.iterator().next()).getFirstName());
                q.closeAll();
                q = pm.newQuery(pm.getExtent(org.jpox.samples.models.company.Employee.class, false));
                q.setFilter("firstName == Person.FIRSTNAME");
                results = new HashSet((Collection) q.execute());
                assertEquals("received: " + results, 1, results.size());
                assertEquals(org.jpox.samples.models.company.Person.FIRSTNAME, ((Person) results.iterator().next()).getFirstName());
                q.closeAll();
                q = pm.newQuery(pm.getExtent(org.jpox.samples.models.company.Employee.class, false));
                q.setFilter("firstName == Employee.FIRSTNAME");
                results = new HashSet((Collection) q.execute());
                assertEquals("received: " + results, 1, results.size());
                assertEquals(org.jpox.samples.models.company.Employee.FIRSTNAME, ((Person) results.iterator().next()).getFirstName());
                q.closeAll();
                q = pm.newQuery(pm.getExtent(org.jpox.samples.models.company.Employee.class, false));
                q.setFilter("firstName == FIRSTNAME");
                results = new HashSet((Collection) q.execute());
                assertEquals("received: " + results, 1, results.size());
                assertEquals(org.jpox.samples.models.company.Employee.FIRSTNAME, ((Person) results.iterator().next()).getFirstName());
                q.closeAll();
            } catch (JDOUserException e) {
                fail(e.getMessage());
            }
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        clean(Employee.class);
    }
}
Also used : Employee(org.jpox.samples.models.company.Employee) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Collection(java.util.Collection) JDOUserException(javax.jdo.JDOUserException) HashSet(java.util.HashSet)

Example 40 with Employee

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

the class JDOQLBasicTest method testStringStartsWith.

/**
 * Test the use of the String.startsWith() method.
 */
public void testStringStartsWith() {
    Employee woody = new Employee(1, "Woody", "Woodpecker", "Woody@woodpecker.com", 13, "serial 1");
    Employee bart = new Employee(2, "Bart", "Simpson", "Bart@simpson.com", 2, "serial 2");
    // Eh, what's up, doc?
    Employee bunny = new Employee(3, "Bugs", "Bunny", "Bugs.bunny@warnerbros.com", 12, "serial 3");
    // Beep! Beep!
    Employee roadrunner = new Employee(4, "Road", "Runner", "Road.runner@warnerbros.com", 11, "serial 4");
    // I hate the gravity
    Employee coyote = new Employee(5, "Wile", "E. Coyote", "Wile.coyote@acme.com", 9, "serial 5");
    // paranoid, and neurotic
    Employee duck = new Employee(6, "Daffy", "Duck", "Daffy.duck@warnerbros.com", 7, "serial 6");
    // You are my peanut.
    Employee pepe = new Employee(7, "Pepe Changed", "le Pew", "Pepe.lepew@warnerbros.com", 8, "serial 7");
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        pm.makePersistent(woody);
        pm.makePersistent(bart);
        pm.makePersistent(bunny);
        pm.makePersistent(roadrunner);
        pm.makePersistent(coyote);
        pm.makePersistent(duck);
        pm.makePersistent(pepe);
        tx.commit();
        tx.begin();
        try {
            // test 1
            Query q = pm.newQuery(pm.getExtent(org.jpox.samples.models.company.Employee.class, false));
            q.setFilter("emailAddress.startsWith(\"Road\")");
            HashSet results = new HashSet((Collection) q.execute());
            assertTrue("received: " + results, results.size() == 1);
            q.closeAll();
            // test 2
            q = pm.newQuery(pm.getExtent(org.jpox.samples.models.company.Employee.class, false));
            q.setFilter("emailAddress.startsWith(firstName)");
            results = new HashSet((Collection) q.execute());
            assertTrue("received: " + results, results.size() == 6);
            q.closeAll();
            // test 3
            q = pm.newQuery(pm.getExtent(org.jpox.samples.models.company.Employee.class, false));
            q.setFilter("\"Road Runner\".startsWith(firstName)");
            results = new HashSet((Collection) q.execute());
            assertTrue("received: " + results, results.size() == 1);
            q.closeAll();
            // test 4
            q = pm.newQuery(pm.getExtent(org.jpox.samples.models.company.Employee.class, false));
            q.declareParameters("java.lang.String var1");
            q.setFilter("var1.startsWith(firstName)");
            results = new HashSet((Collection) q.execute("Road Runner"));
            assertTrue("received: " + results, results.size() == 1);
            q.closeAll();
        } catch (JDOUserException e) {
            LOG.error("Exception during test", e);
            fail(e.getMessage());
        }
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
        // Clean out our data
        clean(Employee.class);
    }
}
Also used : Employee(org.jpox.samples.models.company.Employee) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Collection(java.util.Collection) JDOUserException(javax.jdo.JDOUserException) HashSet(java.util.HashSet)

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