Search in sources :

Example 36 with Department

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

the class JDOQLContainerTest method testQueryUsesContainsTwiceOnFieldNoNamespace.

/**
 * test query with "field.contains(x) && field.contains(y)"
 */
public void testQueryUsesContainsTwiceOnFieldNoNamespace() {
    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);
    boss.addSubordinate(boss2);
    Department deptA = new Department("DeptA");
    Department deptB = new Department("DeptB");
    bart.addDepartment(deptB);
    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\" && subordinates.contains(emp2) && emp2.lastName == \"WakesUp2\"");
        q.declareVariables("Employee emp1; Employee emp2");
        q.declareImports("import org.jpox.samples.models.company.Employee");
        Collection c = (Collection) q.execute();
        assertEquals(1, c.size());
        q = pm.newQuery(Manager.class);
        q.setFilter("subordinates.contains(emp1) && subordinates.contains(emp2) && emp1.lastName == \"WakesUp\" && emp2.lastName == \"WakesUp2\"");
        q.declareVariables("Employee emp1; Employee emp2");
        q.declareImports("import org.jpox.samples.models.company.Employee");
        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)

Example 37 with Department

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

the class JDOQLContainerTest method testQueryUsesContainsTwiceOnOneUnboundVariable.

/**
 * test query with "field.contains(x) && field.contains(x)" using a workaround
 */
public void testQueryUsesContainsTwiceOnOneUnboundVariable() {
    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);
    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) && subordinates.contains(emp1) && (emp1.lastName == \"WakesUp\" || emp1.lastName == \"WakesUp2\")");
        q.declareVariables("Employee emp1");
        q.declareImports("import org.jpox.samples.models.company.Employee");
        Collection c = (Collection) q.execute();
        assertEquals(1, c.size());
        q = pm.newQuery(Manager.class);
        q.setFilter("subordinates.contains(emp1) && subordinates.contains(emp1) && (emp1.lastName == \"WakesUp\" || emp1.lastName == \"WakesUp4\")");
        q.declareVariables("Employee emp1");
        q.declareImports("import org.jpox.samples.models.company.Employee");
        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 38 with Department

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

the class JDOQLContainerTest method testContainsResultVariableNestedContains2.

/**
 * Test of collection(A) contains C1 and C1.contains(B) contains D1
 */
public void testContainsResultVariableNestedContains2() {
    try {
        Manager mgr1 = new Manager(101, "Fred", "Flintstone", "fred.flintstone@jpox.com", (float) 250.0, "12345");
        Manager mgr2 = new Manager(102, "Barney", "Rubble", "barney.rubble@jpox.com", (float) 220.0, "12356");
        Department dept1 = new Department("Gravel");
        Department dept2 = new Department("Coal");
        Department dept3 = new Department("Cement");
        Department dept4 = new Department("Sales");
        Department dept5 = new Department("Marketing");
        Project proj1 = new Project("proj1", 12000);
        Project proj2 = new Project("proj2", 24000);
        Project proj3 = new Project("proj3", 18000);
        mgr1.addDepartment(dept1);
        mgr1.addDepartment(dept2);
        mgr1.addDepartment(dept3);
        mgr2.addDepartment(dept4);
        mgr2.addDepartment(dept5);
        dept1.addProject(proj1);
        dept2.addProject(proj1);
        dept2.addProject(proj2);
        dept3.addProject(proj1);
        dept3.addProject(proj2);
        dept4.addProject(proj3);
        dept5.addProject(proj3);
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            pm.makePersistent(mgr1);
            pm.makePersistent(mgr2);
            tx.commit();
            tx.begin();
            Query q = pm.newQuery(Manager.class);
            q.setFilter("departments.contains(d) && d.projects.contains(p)");
            q.setResult("d, p");
            q.declareVariables("Department d; Project p");
            Collection c = (Collection) q.execute();
            assertEquals(7, c.size());
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out our data
        CompanyHelper.clearCompanyData(pmf);
    }
}
Also used : Project(org.jpox.samples.models.company.Project) 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 39 with Department

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

the class JDOQLContainerTest method testContainsInParameterArray2.

public void testContainsInParameterArray2() {
    try {
        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.setManager(boss2);
        boss.setManager(boss4);
        homer.setManager(bart);
        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");
        boss.addDepartment(deptA);
        boss.addDepartment(deptB);
        boss.addDepartment(deptC);
        boss2.addDepartment(deptD);
        boss2.addDepartment(deptE);
        Collection emps = new HashSet();
        emps.add(bart);
        emps.add(boss);
        emps.add(homer);
        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();
            Department[] depts = (Department[]) boss2.getDepartments().toArray(new Department[boss2.getDepartments().size()]);
            // Get all Managers whose Departments are present in our array
            // This is the same as the test above yet with the clauses rearranged
            Query q = pm.newQuery(Manager.class);
            q.setResult("distinct this");
            q.setFilter("dept.contains(dept0) && dept0.manager == this");
            q.declareParameters("org.jpox.samples.models.company.Department[] dept");
            q.declareVariables("org.jpox.samples.models.company.Department dept0");
            q.setOrdering("this.firstName ascending");
            Collection c = (Collection) q.execute(depts);
            // boss2
            assertEquals(1, c.size());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // 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) HashSet(java.util.HashSet)

Example 40 with Department

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

the class JDOQLContainerTest method testContainsInParameterCollection.

public void testContainsInParameterCollection() {
    try {
        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.setManager(boss2);
        boss.setManager(boss4);
        homer.setManager(bart);
        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");
        boss.addDepartment(deptA);
        boss.addDepartment(deptB);
        boss.addDepartment(deptC);
        boss2.addDepartment(deptD);
        boss2.addDepartment(deptE);
        deptA.setManager(boss);
        deptB.setManager(boss);
        deptC.setManager(boss);
        deptD.setManager(boss2);
        deptE.setManager(boss2);
        Collection emps = new HashSet();
        emps.add(bart);
        emps.add(boss);
        emps.add(homer);
        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("emps.contains(this.manager)");
            q.declareParameters("java.util.Collection emps");
            Collection c = (Collection) q.execute(emps);
            assertEquals(1, c.size());
            assertEquals(((Manager) c.iterator().next()).getFirstName(), "Homer");
            q = pm.newQuery(Manager.class);
            q.setFilter("emps.contains(this.manager) && (this.lastName == 'WakesUp2' || this.lastName == 'WakesUp4')");
            q.declareParameters("java.util.Collection emps");
            c = (Collection) q.execute(emps);
            assertEquals(0, c.size());
        // should work... but
        // q = pm.newQuery(Manager.class);
        // q.setFilter("mgr0.departments.contains(dept) && (dept.manager != this)");
        // q.setFilter("(dept.manager != this) && mgr0.departments.contains(dept)");
        // q.declareVariables("org.jpox.samples.models.company.Department dept");
        // q.declareParameters("org.jpox.samples.models.company.Manager mgr0");
        // c = (Collection) q.execute(boss2);
        // assertEquals(1,c.size());
        // tx.commit();
        // q = pm.newQuery(Department.class);
        // q.setFilter("emps.contains(mgr0) && (mgr0.lastName == 'WakesUp2' || mgr0.lastName == 'WakesUp4') && !emps.departments.contains(this)");
        // q.declareParameters("java.util.Collection emps");
        // q.declareVariables("org.jpox.samples.models.company.Manager mgr0");
        // c = (Collection) q.execute(emps);
        // tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // 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) HashSet(java.util.HashSet)

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