Search in sources :

Example 51 with Manager

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

the class JDOQLBasicTest method testExecuteUnique.

/**
 * test use of executeUnique()
 */
public void testExecuteUnique() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        Manager homer = new Manager(1, "Homer", "Simpson", "homer@simpson.com", 1, "serial 1");
        pm.makePersistent(homer);
        pm.flush();
        try {
            Query q = pm.newQuery(Manager.class).filter("firstName == :myName").setParameters("Homer");
            // Should return single object not a List
            Object obj = q.executeUnique();
            assertNotNull(obj);
            assertTrue(obj instanceof Manager);
        } catch (JDOUserException ue) {
        // Expected
        }
        tx.rollback();
    } finally {
        if (tx.isActive()) {
            pm.currentTransaction().rollback();
        }
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Manager(org.jpox.samples.models.company.Manager) StoreManager(org.datanucleus.store.StoreManager) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager) PersistenceManager(javax.jdo.PersistenceManager) JDOUserException(javax.jdo.JDOUserException)

Example 52 with Manager

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

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

the class JDOQLBasicTest method testInstanceof.

/**
 * Test case to use JDOQL "instanceof"
 */
public void testInstanceof() {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            // 1. instanceof where the candidate class uses "new-table" (union)
            tx.begin();
            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");
            // Meep! Meep!
            Manager roadrunner = new Manager(4, "Road", "Runner", "road.runner@warnerbros.com", 11, "serial 4");
            pm.makePersistent(woody);
            pm.makePersistent(bart);
            pm.makePersistent(bunny);
            pm.makePersistent(roadrunner);
            tx.commit();
            tx.begin();
            try {
                Query q = pm.newQuery(Employee.class, "this instanceof " + Manager.class.getName());
                List results = (List) q.execute();
                assertEquals("Number of objects returned from instanceof was incorrect", results.size(), 1);
                Object obj = results.get(0);
                assertTrue("Type of object returned should have been Manager but wasnt", obj instanceof Manager);
                q.closeAll();
            } catch (JDOUserException e) {
                fail(e.getMessage());
            }
            tx.commit();
            // 2. instanceof where the candidate class using "superclass-table" (discriminator)
            tx.begin();
            ElectronicJournal elecJournal = new ElectronicJournal(1, "Electronics Weekly");
            PrintJournal printJournal = new PrintJournal(2, "Glossy magazine");
            pm.makePersistent(elecJournal);
            pm.makePersistent(printJournal);
            tx.commit();
            tx.begin();
            try {
                Query q = pm.newQuery(AbstractJournal.class, "this instanceof " + PrintJournal.class.getName());
                List results = (List) q.execute();
                assertEquals("Number of objects returned from instanceof was incorrect", results.size(), 1);
                Object obj = results.get(0);
                assertTrue("Type of object returned should have been PrintJournal but wasnt", obj instanceof PrintJournal);
                q.closeAll();
            } catch (JDOUserException e) {
                fail(e.getMessage());
            }
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out our data
        clean(PrintJournal.class);
        clean(ElectronicJournal.class);
        CompanyHelper.clearCompanyData(pmf);
    }
}
Also used : Employee(org.jpox.samples.models.company.Employee) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) ElectronicJournal(org.jpox.samples.one_one.bidir_3.ElectronicJournal) List(java.util.List) ArrayList(java.util.ArrayList) Manager(org.jpox.samples.models.company.Manager) StoreManager(org.datanucleus.store.StoreManager) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager) PersistenceManager(javax.jdo.PersistenceManager) JDOUserException(javax.jdo.JDOUserException) PrintJournal(org.jpox.samples.one_one.bidir_3.PrintJournal)

Example 54 with Manager

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

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

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