Search in sources :

Example 31 with Department

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

the class JDOQLBasicTest method testQueryUsesInnerJoin.

public void testQueryUsesInnerJoin() {
    try {
        Manager woody = new Manager(1, "Woody", "Woodpecker", "woody@woodpecker.com", 13, "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");
        Department deptA = new Department("DeptA");
        Department deptB = new Department("DeptB");
        deptB.setManager(bart);
        Department deptC = new Department("DeptC");
        deptC.setManager(boss);
        // I hate the gravity
        Person coyote = new Person(5, "Wile", "E. Coyote", "wile.coyote@acme.com");
        // paranoid, and neurotic
        Person duck = new Person(6, "Daffy", "Duck", "daffy.duck@warnerbros.com");
        // You are my peanut.
        Person pepe = new Person(7, "Pepe", "le Pew", "pepe.lepew@warnerbros.com");
        // You are my peanut.
        Person pepe2 = new Person(8, "Pepe", "le Dawn", "pepe.dawn@warnerbros.com");
        Qualification qA = new Qualification("QA");
        Qualification qB = new Qualification("QB");
        qB.setPerson(duck);
        Qualification qC = new Qualification("QC");
        qC.setPerson(pepe);
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            // test with inheritance
            tx.begin();
            pm.makePersistent(deptC);
            pm.makePersistent(deptA);
            pm.makePersistent(deptB);
            pm.makePersistent(woody);
            pm.makePersistent(boss2);
            tx.commit();
            tx.begin();
            Query q = pm.newQuery(Department.class, "manager.firstName == \"Boss\"");
            Collection c = q.executeList();
            assertEquals(c.size(), 1);
            assertEquals(((Department) c.iterator().next()).getName(), "DeptC");
            tx.commit();
            // test without inheritance
            tx.begin();
            pm.makePersistent(qC);
            pm.makePersistent(qA);
            pm.makePersistent(qB);
            pm.makePersistent(coyote);
            pm.makePersistent(pepe2);
            tx.commit();
            tx.begin();
            q = pm.newQuery(Qualification.class, "person.firstName == \"Pepe\"");
            c = q.executeList();
            assertEquals(c.size(), 1);
            assertEquals(((Qualification) c.iterator().next()).getName(), "QC");
            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) Qualification(org.jpox.samples.models.company.Qualification) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Collection(java.util.Collection) Manager(org.jpox.samples.models.company.Manager) StoreManager(org.datanucleus.store.StoreManager) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager) PersistenceManager(javax.jdo.PersistenceManager) Person(org.jpox.samples.models.company.Person)

Example 32 with Department

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

the class JDOQLBasicTest method testQueryUnboundVariablesInheritanceRightHandDeclared1.

public void testQueryUnboundVariablesInheritanceRightHandDeclared1() {
    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.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(deptA);
            pm.makePersistent(deptB);
            tx.commit();
            tx.begin();
            Query q = pm.newQuery(Manager.class);
            Collection c = q.filter("this.subordinates.contains(emp) && emp.lastName == \"WakesUp2\" ").variables("Employee emp").imports("import org.jpox.samples.models.company.Employee").executeList();
            assertEquals(1, c.size());
            assertEquals(((Manager) c.iterator().next()).getFirstName(), "Bart");
            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) StoreManager(org.datanucleus.store.StoreManager) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager) PersistenceManager(javax.jdo.PersistenceManager)

Example 33 with Department

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

the class JDOQLContainerTest method testQueryUsesContainsOnceOnOneUnboundVariable.

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

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

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

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