Search in sources :

Example 46 with Employee

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

the class JDOQLSubqueryTest method testSingleStringSubquery.

/**
 * Test a simple subquery using single-string form.
 */
public void testSingleStringSubquery() {
    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
        Query q = pm.newQuery("SELECT FROM " + Employee.class.getName() + " WHERE salary > (SELECT avg(salary) FROM " + Employee.class.getName() + " e)");
        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());
        // Don't commit the data
        tx.rollback();
    } catch (JDOUserException e) {
        e.printStackTrace();
        fail(e.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
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 47 with Employee

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

the class JDOQLSubqueryTest method testAPISubqueryWithParameter.

/**
 * Test a simple subquery using API form and a parameter in the subquery.
 */
public void testAPISubqueryWithParameter() {
    if (storeMgr instanceof RDBMSStoreManager) {
        DatastoreAdapter dba = ((RDBMSStoreManager) storeMgr).getDatastoreAdapter();
        if (!dba.supportsOption(DatastoreAdapter.ACCESS_PARENTQUERY_IN_SUBQUERY_JOINED)) {
            // Access of outer query cols not supported by this datastore so dont test it
            LOG.warn("Database doesnt support use of parameters with subqueries so omitting the test");
            return;
        }
    }
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        // Persist Employees
        Employee emp1 = new Employee(101, "Fred", "Smith", "fred.smith@company.com", 100f, "10001");
        Employee emp2 = new Employee(102, "John", "Smith", "john.smith@company.com", 80f, "10002");
        Employee emp3 = new Employee(103, "Jim", "Smith", "jim.smith@company.com", 80f, "10003");
        Employee emp4 = new Employee(104, "Geoff", "Jones", "f2.s2@company.com", 200f, "10004");
        pm.makePersistent(emp1);
        pm.makePersistent(emp2);
        pm.makePersistent(emp3);
        pm.makePersistent(emp4);
        pm.flush();
        // Find the Employees earning more than the average salary of people with the same surname
        Query subquery = pm.newQuery(Employee.class);
        subquery.setResult("avg(this.salary)");
        subquery.setFilter("this.lastName == :lastNameParam");
        Query q = pm.newQuery(Employee.class, "salary > averageSalaryForFamily");
        q.addSubquery(subquery, "double averageSalaryForFamily", null, "this.lastName");
        // NOTE : HSQL <= 1.8 doesnt seem to support and conditions back to the outer query
        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());
        // Don't commit the data
        tx.rollback();
    } catch (JDOUserException e) {
        e.printStackTrace();
        fail(e.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Employee(org.jpox.samples.models.company.Employee) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) DatastoreAdapter(org.datanucleus.store.rdbms.adapter.DatastoreAdapter) List(java.util.List) JDOUserException(javax.jdo.JDOUserException) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager)

Example 48 with Employee

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

the class JDOQLSubqueryTest method testSingleStringSubqueryInResult.

/**
 * Test a simple subquery using single-string form and a subquery in the result.
 */
public void testSingleStringSubqueryInResult() {
    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 of people with surname "S2"
        Query q = pm.newQuery("SELECT this, (SELECT avg(e.salary) FROM " + Employee.class.getName() + " e WHERE e.lastName == 'S1') FROM " + Employee.class.getName() + " ORDER BY this.salary");
        List results = (List) q.execute();
        assertNotNull("No results from query!", results);
        assertEquals(2, results.size());
        Object result0 = results.get(0);
        assertNotNull(result0);
        assertTrue(result0.getClass().isArray());
        assertEquals(2, Array.getLength(result0));
        assertTrue(Array.get(result0, 0) instanceof Employee);
        assertEquals(((Employee) Array.get(result0, 0)).getLastName(), "S1");
        assertEquals(100.0, Array.get(result0, 1));
        Object result1 = results.get(1);
        assertTrue(result1.getClass().isArray());
        assertEquals(2, Array.getLength(result1));
        assertTrue(Array.get(result1, 0) instanceof Employee);
        assertEquals(((Employee) Array.get(result1, 0)).getLastName(), "S2");
        assertEquals(100.0, Array.get(result1, 1));
        // Don't commit the data
        tx.rollback();
    } catch (JDOUserException e) {
        e.printStackTrace();
        fail(e.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
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 49 with Employee

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

the class JDOQLBasicTest method testStringIndexOf.

/**
 * Test for the use of the String.indexOf() method.
 */
public void testStringIndexOf() {
    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("emailAddress.indexOf(\"@\") >= 0");
            HashSet results = new HashSet((Collection) q.execute());
            assertEquals("received: " + results, 7, results.size());
            q.closeAll();
        } catch (JDOUserException e) {
            fail(e.getMessage());
        }
        try {
            Query q = pm.newQuery(pm.getExtent(org.jpox.samples.models.company.Employee.class, false));
            q.setFilter("emailAddress.indexOf(\"woodpecker\") == 6");
            HashSet results = new HashSet((Collection) q.execute());
            assertEquals("received: " + results, 1, results.size());
            q.closeAll();
        } catch (JDOUserException e) {
            fail(e.getMessage());
        }
        try {
            Query q = pm.newQuery(pm.getExtent(org.jpox.samples.models.company.Employee.class, false));
            q.setFilter("emailAddress.indexOf(\"wood\",7) >= 0");
            HashSet results = new HashSet((Collection) q.execute());
            assertEquals("received: " + results, 0, results.size());
            q.closeAll();
        } catch (JDOUserException 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) JDOUserException(javax.jdo.JDOUserException) HashSet(java.util.HashSet)

Example 50 with Employee

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

the class JDOQLBasicTest method testEscapeSingleQuoteInString.

/**
 * Test case for autoamtic escaping of single quote within a parameter String.
 */
public void testEscapeSingleQuoteInString() {
    try {
        Employee woody = new Employee(1, "Woody", "Woodpecker", "woody@woodpecker.com", 13, "abc'def");
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            // Persist some data to play with
            tx.begin();
            pm.makePersistent(woody);
            tx.commit();
            // Query for a parameter that contains a single-quote
            tx.begin();
            try {
                Query q = pm.newQuery(pm.getExtent(Employee.class, false));
                q.setFilter("serialNo == theSerial");
                q.declareParameters("java.lang.String theSerial");
                String serial = "abc'def";
                List results = (List) q.execute(serial);
                assertTrue("Test for a string containing a single-quote returned no results!", results != null && results.size() > 0);
                assertTrue("Test for a string containing a single-quote returned incorrect number of objects - returned " + results.size() + " but should have been 1", results.size() == 1);
                q.closeAll();
            } catch (JDOUserException e) {
                LOG.error(e);
                fail(e.getMessage());
            }
            // Query for something that starts with something containing a single-quote
            try {
                Query q = pm.newQuery(pm.getExtent(Employee.class, false));
                q.setFilter("serialNo.startsWith(theSerial)");
                q.declareParameters("java.lang.String theSerial");
                String serial = "abc'";
                List results = (List) q.execute(serial);
                assertTrue("Test for a string.startsWith() containing a single-quote returned no results!", results != null && results.size() > 0);
                assertTrue("Test for a string.startsWith() containing a single-quote returned incorrect number of objects - returned " + results.size() + " but should have been 1", results.size() == 1);
                q.closeAll();
            } catch (JDOUserException e) {
                LOG.error(e);
                fail(e.getMessage());
            }
            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) JDOUserException(javax.jdo.JDOUserException)

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