Search in sources :

Example 56 with Manager

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

the class JDOQLContainerTest method testContainsInSetFields.

/**
 * Tests contains() in Set fields
 */
public void testContainsInSetFields() {
    try {
        Object managerId = null;
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Manager manager = new Manager(1, "John", "Doe", "john.doe@acme.com", 10000f, "1");
            Department dept1 = new Department("accounting");
            Department dept2 = new Department("entertainment");
            Employee emp1 = new Employee(2, "Harvey", "Hacker", "harvey.hacker@acme.com", 500f, "2");
            Employee emp2 = new Employee(3, "Geoffrey", "Gimp", "geoffrey.gimp@acme.com", 500f, "3");
            pm.makePersistentAll(new Object[] { manager, dept1, dept2, emp1, emp2 });
            managerId = JDOHelper.getObjectId(manager);
            Set depts = manager.getDepartments();
            depts.add(dept1);
            depts.add(dept2);
            Set emps = manager.getSubordinates();
            emps.add(emp1);
            emps.add(emp2);
            pm.flush();
            Query q = pm.newQuery(Manager.class, "departments.contains(d) && subordinates.contains(e)");
            q.declareParameters(Department.class.getName() + " d, " + Employee.class.getName() + " e");
            q.compile();
            Collection c = (Collection) q.execute(dept1, emp1);
            assertEquals(1, c.size());
            assertEquals(managerId, JDOHelper.getObjectId(c.iterator().next()));
            tx.rollback();
        } 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) Employee(org.jpox.samples.models.company.Employee) HashSet(java.util.HashSet) Set(java.util.Set) 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 57 with Manager

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

the class JDOQLContainerTest method testQueryUsesContainsTwiceOnFieldUsingWorkaround.

/**
 * test query with "field.contains(x) && field.contains(y)" using "or"
 * workaround. Use the workaround to bypass a deficiency on query generation
 */
public void testQueryUsesContainsTwiceOnFieldUsingWorkaround() {
    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(emp2) && emp1.lastName == \"WakesUp\" || 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(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 58 with Manager

use of org.jpox.samples.models.company.Manager 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 59 with Manager

use of org.jpox.samples.models.company.Manager 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 60 with Manager

use of org.jpox.samples.models.company.Manager 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)

Aggregations

PersistenceManager (javax.jdo.PersistenceManager)106 Manager (org.jpox.samples.models.company.Manager)106 Transaction (javax.jdo.Transaction)97 Department (org.jpox.samples.models.company.Department)50 Query (javax.jdo.Query)48 Employee (org.jpox.samples.models.company.Employee)45 JDOUserException (javax.jdo.JDOUserException)41 Collection (java.util.Collection)40 JDOPersistenceManager (org.datanucleus.api.jdo.JDOPersistenceManager)38 StoreManager (org.datanucleus.store.StoreManager)34 InsuranceDepartment (org.jpox.samples.models.company.InsuranceDepartment)29 JDOObjectNotFoundException (javax.jdo.JDOObjectNotFoundException)27 Iterator (java.util.Iterator)22 JDODetachedFieldAccessException (javax.jdo.JDODetachedFieldAccessException)17 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)15 JDOException (javax.jdo.JDOException)14 Extent (javax.jdo.Extent)13 PersistenceManagerFactory (javax.jdo.PersistenceManagerFactory)13 List (java.util.List)11 Properties (java.util.Properties)10