Search in sources :

Example 61 with JDOUserException

use of javax.jdo.JDOUserException in project tests by datanucleus.

the class JDOQLResultTest method testRangeAndUnique.

/**
 * Test case to use the JDO 2.0 setUnique() and setRange() methods to
 * control the number and type of objects returned from the query.
 */
public void testRangeAndUnique() {
    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!
    Employee roadrunner = new Employee(4, "Road", "Runner", "road.runner@warnerbros.com", 11, "serial 4");
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        pm.makePersistent(woody);
        pm.makePersistent(bart);
        pm.makePersistent(bunny);
        pm.makePersistent(roadrunner);
        tx.commit();
        tx.begin();
        try {
            Query q = pm.newQuery(pm.getExtent(org.jpox.samples.models.company.Employee.class, false));
            q.setFilter("firstName == \"Bugs\"");
            q.setUnique(true);
            Object results = q.execute();
            assertTrue("setUnique() test returned an object of an incorrect type ", results instanceof Employee);
            q.closeAll();
        } catch (JDOUserException e) {
            fail(e.getMessage());
        }
        boolean success = false;
        try {
            Query q = pm.newQuery(pm.getExtent(org.jpox.samples.models.company.Employee.class, false));
            q.setUnique(true);
            q.execute();
            q.closeAll();
        } catch (JDOUserException e) {
            success = true;
        }
        assertTrue("expected JdoUserException for unique == true and returned more than one instance", success);
        try {
            Query q = pm.newQuery(pm.getExtent(org.jpox.samples.models.company.Employee.class, false));
            q.setRange(1, 3);
            Collection results = (Collection) q.execute();
            assertTrue("setRange() test returned an incorrect number of results : should have been 2 but was " + results.size(), results.size() == 2);
            q.closeAll();
        } catch (JDOUserException e) {
            fail(e.getMessage());
        }
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
        // Clean out our data
        clean(Employee.class);
    }
}
Also used : Employee(org.jpox.samples.models.company.Employee) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Collection(java.util.Collection) JDOUserException(javax.jdo.JDOUserException)

Example 62 with JDOUserException

use of javax.jdo.JDOUserException in project tests by datanucleus.

the class JDOQLSubqueryTest method testSingleStringSubquery.

/**
 * Test a simple subquery using single-string form.
 */
public void testSingleStringSubquery() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        // Persist 2 Employees
        Employee emp1 = new Employee(101, "F1", "S1", "f1.s1@company.com", 100f, "10001");
        Employee emp2 = new Employee(102, "F2", "S2", "f2.s2@company.com", 200f, "10002");
        pm.makePersistent(emp1);
        pm.makePersistent(emp2);
        pm.flush();
        // Find the Employees earning more than the average salary
        Query q = pm.newQuery("SELECT FROM " + Employee.class.getName() + " WHERE salary > (SELECT avg(salary) FROM " + Employee.class.getName() + " e)");
        List results = (List) q.execute();
        assertNotNull("No results from query!", results);
        assertEquals("Number of Employees with more than average salary was wrong", 1, results.size());
        // Don't commit the data
        tx.rollback();
    } catch (JDOUserException e) {
        e.printStackTrace();
        fail(e.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Employee(org.jpox.samples.models.company.Employee) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) List(java.util.List) JDOUserException(javax.jdo.JDOUserException)

Example 63 with JDOUserException

use of javax.jdo.JDOUserException in project tests by datanucleus.

the class JDOQLSubqueryTest method testAPISubqueryWithParameter.

/**
 * Test a simple subquery using API form and a parameter in the subquery.
 */
public void testAPISubqueryWithParameter() {
    if (storeMgr instanceof RDBMSStoreManager) {
        DatastoreAdapter dba = ((RDBMSStoreManager) storeMgr).getDatastoreAdapter();
        if (!dba.supportsOption(DatastoreAdapter.ACCESS_PARENTQUERY_IN_SUBQUERY_JOINED)) {
            // Access of outer query cols not supported by this datastore so dont test it
            LOG.warn("Database doesnt support use of parameters with subqueries so omitting the test");
            return;
        }
    }
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        // Persist Employees
        Employee emp1 = new Employee(101, "Fred", "Smith", "fred.smith@company.com", 100f, "10001");
        Employee emp2 = new Employee(102, "John", "Smith", "john.smith@company.com", 80f, "10002");
        Employee emp3 = new Employee(103, "Jim", "Smith", "jim.smith@company.com", 80f, "10003");
        Employee emp4 = new Employee(104, "Geoff", "Jones", "f2.s2@company.com", 200f, "10004");
        pm.makePersistent(emp1);
        pm.makePersistent(emp2);
        pm.makePersistent(emp3);
        pm.makePersistent(emp4);
        pm.flush();
        // Find the Employees earning more than the average salary of people with the same surname
        Query subquery = pm.newQuery(Employee.class);
        subquery.setResult("avg(this.salary)");
        subquery.setFilter("this.lastName == :lastNameParam");
        Query q = pm.newQuery(Employee.class, "salary > averageSalaryForFamily");
        q.addSubquery(subquery, "double averageSalaryForFamily", null, "this.lastName");
        // NOTE : HSQL <= 1.8 doesnt seem to support and conditions back to the outer query
        List results = (List) q.execute();
        assertNotNull("No results from query!", results);
        assertEquals("Number of Employees with more than average salary was wrong", 1, results.size());
        // Don't commit the data
        tx.rollback();
    } catch (JDOUserException e) {
        e.printStackTrace();
        fail(e.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Employee(org.jpox.samples.models.company.Employee) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) DatastoreAdapter(org.datanucleus.store.rdbms.adapter.DatastoreAdapter) List(java.util.List) JDOUserException(javax.jdo.JDOUserException) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager)

Example 64 with JDOUserException

use of javax.jdo.JDOUserException in project tests by datanucleus.

the class JDOQLSubqueryTest method testSingleStringSubqueryInResult.

/**
 * Test a simple subquery using single-string form and a subquery in the result.
 */
public void testSingleStringSubqueryInResult() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        // Persist 2 Employees
        Employee emp1 = new Employee(101, "F1", "S1", "f1.s1@company.com", 100f, "10001");
        Employee emp2 = new Employee(102, "F2", "S2", "f2.s2@company.com", 200f, "10002");
        pm.makePersistent(emp1);
        pm.makePersistent(emp2);
        pm.flush();
        // Find the Employees earning more than the average salary of people with surname "S2"
        Query q = pm.newQuery("SELECT this, (SELECT avg(e.salary) FROM " + Employee.class.getName() + " e WHERE e.lastName == 'S1') FROM " + Employee.class.getName() + " ORDER BY this.salary");
        List results = (List) q.execute();
        assertNotNull("No results from query!", results);
        assertEquals(2, results.size());
        Object result0 = results.get(0);
        assertNotNull(result0);
        assertTrue(result0.getClass().isArray());
        assertEquals(2, Array.getLength(result0));
        assertTrue(Array.get(result0, 0) instanceof Employee);
        assertEquals(((Employee) Array.get(result0, 0)).getLastName(), "S1");
        assertEquals(100.0, Array.get(result0, 1));
        Object result1 = results.get(1);
        assertTrue(result1.getClass().isArray());
        assertEquals(2, Array.getLength(result1));
        assertTrue(Array.get(result1, 0) instanceof Employee);
        assertEquals(((Employee) Array.get(result1, 0)).getLastName(), "S2");
        assertEquals(100.0, Array.get(result1, 1));
        // Don't commit the data
        tx.rollback();
    } catch (JDOUserException e) {
        e.printStackTrace();
        fail(e.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Employee(org.jpox.samples.models.company.Employee) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) List(java.util.List) JDOUserException(javax.jdo.JDOUserException)

Example 65 with JDOUserException

use of javax.jdo.JDOUserException in project tests by datanucleus.

the class ObjectsTest method testOneToOneXcalia.

/**
 * Test for 1-1 relations where 1 side is marked as an Object, though is
 * really PersistenceCapable using xcalia mapping
 */
public void testOneToOneXcalia() {
    try {
        Object folderId = null;
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            // Create some objects.
            tx.begin();
            Customer customer = new Customer(1);
            Folder folder = new Folder(1);
            folder.setCustomer(customer);
            pm.makePersistent(folder);
            tx.commit();
            folderId = pm.getObjectId(folder);
        } catch (JDOUserException ue) {
            assertTrue("Exception thrown during create of Folder and Customer.", false);
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Folder folder = (Folder) pm.getObjectById(folderId);
            assertTrue("Folder was not retrieved correctly", folder != null);
            Customer customer = (Customer) folder.getCustomer();
            assertTrue("Customer was not retrieved correctly", customer != null);
            tx.commit();
        } catch (JDOUserException ue) {
            assertTrue("Exception thrown during retrieval.", false);
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out our data
        clean(Folder.class);
        clean(Customer.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) Customer(org.jpox.samples.models.referenceMapping.Customer) Folder(org.jpox.samples.models.referenceMapping.Folder) JDOUserException(javax.jdo.JDOUserException)

Aggregations

JDOUserException (javax.jdo.JDOUserException)191 PersistenceManager (javax.jdo.PersistenceManager)163 Transaction (javax.jdo.Transaction)161 Query (javax.jdo.Query)94 Iterator (java.util.Iterator)54 Collection (java.util.Collection)45 Employee (org.jpox.samples.models.company.Employee)35 List (java.util.List)33 HashSet (java.util.HashSet)30 ArrayList (java.util.ArrayList)22 Extent (javax.jdo.Extent)22 JDOException (javax.jdo.JDOException)15 JDOPersistenceManager (org.datanucleus.api.jdo.JDOPersistenceManager)15 CollectionHolder (org.jpox.samples.types.container.CollectionHolder)15 Map (java.util.Map)13 MapHolder (org.jpox.samples.types.container.MapHolder)13 JDOFatalUserException (javax.jdo.JDOFatalUserException)12 NucleusException (org.datanucleus.exceptions.NucleusException)10 TransactionActiveOnCloseException (org.datanucleus.exceptions.TransactionActiveOnCloseException)10 Person (org.jpox.samples.models.company.Person)10