Search in sources :

Example 11 with Department

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

the class JDOQLContainerTest method testQueryUsesContainsOnceOnOneUnboundVariableImplicitVariables.

/**
 * test query with "field.contains(x)" using a workaround
 */
public void testQueryUsesContainsOnceOnOneUnboundVariableImplicitVariables() {
    Manager homer = new Manager(1, "Homer", "Simpson", "homer@simpson.com", 1, "serial 1");
    Manager bart = new Manager(2, "Bart", "Simpson", "bart@simpson.com", 2, "serial 2");
    Manager boss = new Manager(3, "Boss", "WakesUp", "boss@wakes.up", 4, "serial 3");
    Manager boss2 = new Manager(4, "Boss", "WakesUp2", "boss2@wakes.up", 5, "serial 4");
    Manager boss4 = new Manager(6, "Boss", "WakesUp4", "boss4@wakes.up", 7, "serial 6");
    bart.addSubordinate(boss);
    bart.addSubordinate(boss2);
    homer.addSubordinate(boss4);
    Department deptA = new Department("DeptA");
    Department deptB = new Department("DeptB");
    bart.addDepartment(deptB);
    boss4.addSubordinate(bart);
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        pm.makePersistent(bart);
        pm.makePersistent(homer);
        pm.makePersistent(boss);
        pm.makePersistent(boss2);
        pm.makePersistent(boss4);
        pm.makePersistent(deptA);
        pm.makePersistent(deptB);
        tx.commit();
        tx.begin();
        Query q = pm.newQuery(Manager.class);
        q.setFilter("subordinates.contains(emp1) && emp1.lastName == \"WakesUp\"");
        Collection c = (Collection) q.execute();
        assertEquals(1, c.size());
        q = pm.newQuery(Manager.class);
        q.setFilter("subordinates.contains(emp1) && (emp1.lastName == \"WakesUp\" || emp1.lastName == \"WakesUp2\")");
        c = (Collection) q.execute();
        assertEquals(1, c.size());
        q = pm.newQuery(Manager.class);
        q.setFilter("subordinates.contains(emp1) && (emp1.lastName == \"WakesUp\" || emp1.lastName == \"WakesUp4\")");
        c = (Collection) q.execute();
        assertEquals(2, c.size());
        q = pm.newQuery(Manager.class);
        q.setFilter("departments.contains(db) && e.departments.contains(db) && db.name =='DeptB'");
        q.declareVariables("Department db; Manager e");
        c = (Collection) q.execute();
        assertEquals(1, c.size());
        q = pm.newQuery(Manager.class);
        q.setFilter("subordinates.contains(e) && (e.departments.contains(db) && db.name =='DeptB')");
        q.declareVariables("Department db; Manager e");
        c = (Collection) q.execute();
        assertEquals(1, c.size());
        assertEquals("WakesUp4", ((Manager) c.iterator().next()).getLastName());
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
        // Clean out our data
        CompanyHelper.clearCompanyData(pmf);
    }
}
Also used : Department(org.jpox.samples.models.company.Department) InsuranceDepartment(org.jpox.samples.models.company.InsuranceDepartment) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Collection(java.util.Collection) Manager(org.jpox.samples.models.company.Manager) PersistenceManager(javax.jdo.PersistenceManager)

Example 12 with Department

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

the class JDOQLContainerTest method testQueryUsesContainsOnceAndEqualsOnOneUnboundVariable.

/**
 * test query with "field.contains(x)" using a workaround
 */
public void testQueryUsesContainsOnceAndEqualsOnOneUnboundVariable() {
    Manager homer = new Manager(1, "Homer", "Simpson", "homer@simpson.com", 1, "serial 1");
    Manager bart = new Manager(2, "Bart", "Simpson", "bart@simpson.com", 2, "serial 2");
    Manager boss = new Manager(3, "Boss", "WakesUp", "boss@wakes.up", 4, "serial 3");
    Manager boss2 = new Manager(4, "Boss", "WakesUp2", "boss2@wakes.up", 5, "serial 4");
    Manager boss4 = new Manager(6, "Boss", "WakesUp4", "boss4@wakes.up", 7, "serial 6");
    bart.addSubordinate(boss);
    bart.addSubordinate(boss2);
    homer.addSubordinate(boss4);
    Department deptA = new Department("DeptA");
    Department deptB = new Department("DeptB");
    bart.addDepartment(deptB);
    boss4.addSubordinate(bart);
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        pm.makePersistent(bart);
        pm.makePersistent(homer);
        pm.makePersistent(boss);
        pm.makePersistent(boss2);
        pm.makePersistent(boss4);
        pm.makePersistent(deptA);
        pm.makePersistent(deptB);
        tx.commit();
        tx.begin();
        Query q = pm.newQuery(Department.class);
        q.setFilter("m1.departments.contains(this) && m1.firstName == \"Bart\" && m1.lastName == \"Simpson\"");
        q.setResult("distinct this");
        q.declareVariables("Manager m1");
        q.declareImports("import org.jpox.samples.models.company.Manager");
        Collection c = (Collection) q.execute();
        assertEquals(1, c.size());
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
        // Clean out our data
        CompanyHelper.clearCompanyData(pmf);
    }
}
Also used : Department(org.jpox.samples.models.company.Department) InsuranceDepartment(org.jpox.samples.models.company.InsuranceDepartment) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Collection(java.util.Collection) Manager(org.jpox.samples.models.company.Manager) PersistenceManager(javax.jdo.PersistenceManager)

Example 13 with Department

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

the class JDOQLContainerTest method testQueryUsesContainsTwiceOnOneUnboundVariableInverse.

/**
 * test query with "field.contains(x) && field.contains(x)" using a workaround
 */
public void testQueryUsesContainsTwiceOnOneUnboundVariableInverse() {
    Manager homer = new Manager(1, "Homer", "Simpson", "homer@simpson.com", 1, "serial 1");
    Manager bart = new Manager(2, "Bart", "Simpson", "bart@simpson.com", 2, "serial 2");
    Manager boss = new Manager(3, "Boss", "WakesUp", "boss@wakes.up", 4, "serial 3");
    Manager boss2 = new Manager(4, "Boss", "WakesUp2", "boss2@wakes.up", 5, "serial 4");
    Manager boss4 = new Manager(6, "Boss", "WakesUp4", "boss4@wakes.up", 7, "serial 6");
    Department deptA = new Department("DeptA");
    Department deptB = new Department("DeptB");
    Department deptC = new Department("DeptC");
    Department deptD = new Department("DeptD");
    Department deptE = new Department("DeptE");
    bart.addDepartment(deptB);
    bart.addDepartment(deptA);
    homer.addDepartment(deptC);
    boss.addDepartment(deptD);
    boss.addDepartment(deptE);
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        pm.makePersistent(bart);
        pm.makePersistent(homer);
        pm.makePersistent(boss);
        pm.makePersistent(boss2);
        pm.makePersistent(boss4);
        tx.commit();
        tx.begin();
        Query q = pm.newQuery(Manager.class);
        q.setFilter("departments.contains(dept1) && departments.contains(dept1) && (dept1.name == \"DeptA\" || dept1.name == \"DeptB\")");
        q.declareVariables("Department dept1");
        Collection c = (Collection) q.execute();
        assertEquals(1, c.size());
        q = pm.newQuery(Manager.class);
        q.setFilter("departments.contains(dept1) && departments.contains(dept1) && (dept1.name == \"DeptA\" || dept1.name == \"DeptD\")");
        q.declareVariables("Department dept1");
        c = (Collection) q.execute();
        assertEquals(2, c.size());
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
        // Clean out our data
        CompanyHelper.clearCompanyData(pmf);
    }
}
Also used : Department(org.jpox.samples.models.company.Department) InsuranceDepartment(org.jpox.samples.models.company.InsuranceDepartment) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Collection(java.util.Collection) Manager(org.jpox.samples.models.company.Manager) PersistenceManager(javax.jdo.PersistenceManager)

Example 14 with Department

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

the class JDOQLContainerTest method testQueryUsesContainsOnceOnOneUnboundVariableInverseUsingParameter.

/**
 * test query with "field.contains(x)" using a workaround
 */
public void testQueryUsesContainsOnceOnOneUnboundVariableInverseUsingParameter() {
    Manager homer = new Manager(1, "Homer", "Simpson", "homer@simpson.com", 1, "serial 1");
    Manager bart = new Manager(2, "Bart", "Simpson", "bart@simpson.com", 2, "serial 2");
    Manager boss = new Manager(3, "Boss", "WakesUp", "boss@wakes.up", 4, "serial 3");
    Manager boss2 = new Manager(4, "Boss", "WakesUp2", "boss2@wakes.up", 5, "serial 4");
    Manager boss4 = new Manager(6, "Boss", "WakesUp4", "boss4@wakes.up", 7, "serial 6");
    Department deptA = new Department("DeptA");
    Department deptB = new Department("DeptB");
    Department deptC = new Department("DeptC");
    Department deptD = new Department("DeptD");
    Department deptE = new Department("DeptE");
    bart.addDepartment(deptB);
    bart.addDepartment(deptA);
    homer.addDepartment(deptC);
    boss.addDepartment(deptD);
    boss.addDepartment(deptE);
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        pm.makePersistent(bart);
        pm.makePersistent(homer);
        pm.makePersistent(boss);
        pm.makePersistent(boss2);
        pm.makePersistent(boss4);
        tx.commit();
        tx.begin();
        Query q = pm.newQuery(Department.class);
        q.setFilter("name == 'DeptA'");
        q.setUnique(true);
        Department da = (Department) q.execute();
        q = pm.newQuery(Manager.class);
        q.setFilter("departments.contains(deptA)");
        q.declareParameters("Department deptA");
        Collection c = (Collection) q.execute(da);
        assertEquals(1, c.size());
        q = pm.newQuery(Department.class);
        q.setFilter("name == 'DeptC'");
        q.setUnique(true);
        Department dc = (Department) q.execute();
        q = pm.newQuery(Manager.class);
        q.setFilter("departments.contains(deptA) || departments.contains(deptC) ");
        q.declareParameters("Department deptA, Department deptC");
        c = (Collection) q.execute(da, dc);
        assertEquals(2, c.size());
        q = pm.newQuery(Manager.class);
        q.setFilter("departments.contains(deptA) && deptA.manager == this");
        q.declareParameters("Department deptA");
        c = (Collection) q.execute(da);
        assertEquals(1, c.size());
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
        // Clean out our data
        CompanyHelper.clearCompanyData(pmf);
    }
}
Also used : Department(org.jpox.samples.models.company.Department) InsuranceDepartment(org.jpox.samples.models.company.InsuranceDepartment) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Collection(java.util.Collection) Manager(org.jpox.samples.models.company.Manager) PersistenceManager(javax.jdo.PersistenceManager)

Example 15 with Department

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

the class JDOQLContainerTest method testQueryUsesContainsTwiceOnFieldWithNamespaceInverse.

/**
 * test query with "field.contains(x) && field.contains(y)"
 *
 * namespace put related expressions inside parentheses
 */
public void testQueryUsesContainsTwiceOnFieldWithNamespaceInverse() {
    Manager homer = new Manager(1, "Homer", "Simpson", "homer@simpson.com", 1, "serial 1");
    Manager bart = new Manager(2, "Bart", "Simpson", "bart@simpson.com", 2, "serial 2");
    Manager boss = new Manager(3, "Boss", "WakesUp", "boss@wakes.up", 4, "serial 3");
    Manager boss2 = new Manager(4, "Boss", "WakesUp2", "boss2@wakes.up", 5, "serial 4");
    Manager boss4 = new Manager(6, "Boss", "WakesUp4", "boss4@wakes.up", 7, "serial 6");
    Department deptA = new Department("DeptA");
    Department deptB = new Department("DeptB");
    Department deptC = new Department("DeptC");
    Department deptD = new Department("DeptD");
    Department deptE = new Department("DeptE");
    bart.addDepartment(deptB);
    bart.addDepartment(deptA);
    homer.addDepartment(deptC);
    boss.addDepartment(deptD);
    boss.addDepartment(deptE);
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        pm.makePersistent(bart);
        pm.makePersistent(homer);
        pm.makePersistent(boss);
        pm.makePersistent(boss2);
        pm.makePersistent(boss4);
        tx.commit();
        tx.begin();
        Query q = pm.newQuery(Manager.class);
        q.setFilter("(departments.contains(dept1) && dept1.name == \"DeptA\") && (departments.contains(dept2) && dept2.name == \"DeptB\")");
        q.declareVariables("Department dept1; Department dept2");
        Collection c = (Collection) q.execute();
        assertEquals(1, c.size());
        tx.commit();
        tx.begin();
        q = pm.newQuery(Manager.class);
        q.setFilter("(departments.contains(dept1) && (dept1.name == \"DeptA\" || dept1.name == \"DeptC\")) && (departments.contains(dept2) && dept2.name == \"DeptB\")");
        q.declareVariables("Department dept1; Department dept2");
        c = (Collection) q.execute();
        assertEquals(1, c.size());
        tx.commit();
        tx.begin();
        q = pm.newQuery(Manager.class);
        q.setFilter("(departments.contains(dept1) && (dept1.name == \"DeptA\" || dept1.name == \"DeptC\")) || (departments.contains(dept2) && dept2.name == \"DeptD\")");
        q.declareVariables("Department dept1; Department dept2");
        c = (Collection) q.execute();
        assertEquals(3, c.size());
        tx.commit();
        tx.begin();
        q = pm.newQuery(Manager.class);
        q.setFilter("departments.contains(dept1) && (dept1.name == \"DeptA\" && (departments.contains(dept2) && (dept2.name.matches(\"Dept.*\") && dept1 != dept2)))");
        q.declareVariables("Department dept1; Department dept2");
        c = (Collection) q.execute();
        assertEquals(1, c.size());
        tx.commit();
    } finally {
        if (tx.isActive()) {
            pm.currentTransaction().rollback();
        }
        pm.close();
        // Clean out our data
        CompanyHelper.clearCompanyData(pmf);
    }
}
Also used : Department(org.jpox.samples.models.company.Department) InsuranceDepartment(org.jpox.samples.models.company.InsuranceDepartment) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Collection(java.util.Collection) Manager(org.jpox.samples.models.company.Manager) PersistenceManager(javax.jdo.PersistenceManager)

Aggregations

Department (org.jpox.samples.models.company.Department)56 PersistenceManager (javax.jdo.PersistenceManager)55 Transaction (javax.jdo.Transaction)54 Manager (org.jpox.samples.models.company.Manager)50 Query (javax.jdo.Query)33 InsuranceDepartment (org.jpox.samples.models.company.InsuranceDepartment)32 Collection (java.util.Collection)31 JDOUserException (javax.jdo.JDOUserException)20 JDOPersistenceManager (org.datanucleus.api.jdo.JDOPersistenceManager)19 JDOObjectNotFoundException (javax.jdo.JDOObjectNotFoundException)13 StoreManager (org.datanucleus.store.StoreManager)13 Employee (org.jpox.samples.models.company.Employee)12 Iterator (java.util.Iterator)11 JDOException (javax.jdo.JDOException)11 Extent (javax.jdo.Extent)8 List (java.util.List)7 JDODetachedFieldAccessException (javax.jdo.JDODetachedFieldAccessException)7 ArrayList (java.util.ArrayList)6 Office (org.jpox.samples.models.company.Office)6 SQLException (java.sql.SQLException)5