Search in sources :

Example 76 with Employee

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

the class JDOQLSubqueryTest method testAPISubquery.

/**
 * Test a subquery using the JDOQL Query API.
 */
public void testAPISubquery() {
    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(Employee.class, "salary > averageSalary");
        q.declareVariables("double averageSalary");
        Query averageSalaryQuery = pm.newQuery("SELECT avg(salary) FROM " + Employee.class.getName());
        q.addSubquery(averageSalaryQuery, "double averageSalary", null);
        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 77 with Employee

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

the class JDOQLSubqueryTest method testSingleStringSubqueryWithFilter.

/**
 * Test a simple subquery using single-string form and a filter in the subquery.
 */
public void testSingleStringSubqueryWithFilter() {
    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 FROM " + Employee.class.getName() + " WHERE salary > (SELECT avg(e.salary) FROM " + Employee.class.getName() + " e WHERE e.lastName == 'S1')");
        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 78 with Employee

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

the class NucleusJDOHelperTest method testObjectState.

/**
 * Check the return of object state values.
 */
public void testObjectState() throws Exception {
    try {
        Employee emp = new Employee(1, "Donald", "Duck", "donald.duck@warnerbros.com", 123, "ABCD");
        assertEquals("Expected state is incorrect", ObjectState.TRANSIENT, JDOHelper.getObjectState(emp));
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            // Persist object and it moves to "persistent-new"
            Employee emp1 = (Employee) pm.makePersistent(emp);
            assertEquals("Expected state is incorrect", ObjectState.PERSISTENT_NEW, JDOHelper.getObjectState(emp1));
            tx.commit();
            // leave transaction and it moves to "hollow / persistent-nontransactional"
            assertEquals("Expected state is incorrect", ObjectState.HOLLOW_PERSISTENT_NONTRANSACTIONAL, JDOHelper.getObjectState(emp1));
            tx.begin();
            // Update a field and it moves to "persistent-dirty"
            emp1.setAge(28);
            assertEquals("Expected state is incorrect", ObjectState.PERSISTENT_DIRTY, JDOHelper.getObjectState(emp1));
            tx.commit();
            // leave transaction and it moves to "hollow / persistent-nontransactional"
            assertEquals("Expected state is incorrect", ObjectState.HOLLOW_PERSISTENT_NONTRANSACTIONAL, JDOHelper.getObjectState(emp1));
            tx.begin();
            pm.makeTransient(emp1);
            assertEquals("Expected state is incorrect", ObjectState.TRANSIENT, JDOHelper.getObjectState(emp1));
            tx.commit();
            tx.begin();
            // Persist it again
            emp1 = (Employee) pm.makePersistent(emp1);
            assertEquals("Expected state is incorrect", ObjectState.PERSISTENT_NEW, JDOHelper.getObjectState(emp1));
            // Detach it
            Employee detachedEmp = (Employee) pm.detachCopy(emp1);
            tx.commit();
            assertEquals("Expected state is incorrect", ObjectState.DETACHED_CLEAN, JDOHelper.getObjectState(detachedEmp));
            // Modify it so it moves to "detached-dirty"
            detachedEmp.setFirstName("Billy");
            assertEquals("Expected state is incorrect", ObjectState.DETACHED_DIRTY, JDOHelper.getObjectState(detachedEmp));
        } catch (Exception e) {
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out our data
        clean(Employee.class);
    }
}
Also used : Employee(org.jpox.samples.models.company.Employee) Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager)

Example 79 with Employee

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

the class PersistenceManagerTest method testNontransactionalPersist.

/**
 * Test use of makePersistent with nontransactionalWrite.
 */
public void testNontransactionalPersist() throws Exception {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        tx.setNontransactionalRead(true);
        tx.setNontransactionalWrite(true);
        Object id = null;
        try {
            Employee emp = new Employee(101, "George", "Green", "george.green@mydomain.com", (float) 123.5, "12346");
            pm.makePersistent(emp);
            id = pm.getObjectId(emp);
        } catch (Exception e) {
            fail("Exception thrown while calling makePersistent with nontransactionalWrite " + e.getMessage());
        } finally {
            pm.close();
        }
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        tx.setNontransactionalRead(true);
        try {
            tx.begin();
            Employee emp = (Employee) pm.getObjectById(id);
            assertNotNull("Object persisted using nontransactionalWrite is null so wasnt persisted", emp);
            assertEquals("First name is incorrect", "George", emp.getFirstName());
            assertEquals("Second name is incorrect", "Green", emp.getLastName());
            assertEquals("Email is incorrect", "george.green@mydomain.com", emp.getEmailAddress());
            tx.commit();
        } catch (Exception e) {
            fail("Exception thrown while querying data persisted using nontransactionalWrite " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out our data
        clean(Employee.class);
    }
}
Also used : Employee(org.jpox.samples.models.company.Employee) Transaction(javax.jdo.Transaction) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) TransactionNotActiveException(org.datanucleus.api.jdo.exceptions.TransactionNotActiveException) JDOException(javax.jdo.JDOException) JDOUserException(javax.jdo.JDOUserException) TransactionNotReadableException(org.datanucleus.api.jdo.exceptions.TransactionNotReadableException) SQLException(java.sql.SQLException) JDOUserCallbackException(javax.jdo.JDOUserCallbackException) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException) TransactionNotWritableException(org.datanucleus.api.jdo.exceptions.TransactionNotWritableException) JDOUnsupportedOptionException(javax.jdo.JDOUnsupportedOptionException)

Example 80 with Employee

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

the class PersistenceManagerTest method testNormalFCOCollectionFieldPersistence1.

/**
 * Test that FCOs added to a Collection field are persisted when the owning PC is persisted.
 */
public void testNormalFCOCollectionFieldPersistence1() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Manager mgr = new Manager(0, FIRSTNAME[0], LASTNAME[0], EMAIL[0], EMP_SALARY[0], EMP_SERIAL[0]);
    Employee emp1 = new Employee(1, FIRSTNAME[1], LASTNAME[1], EMAIL[1], EMP_SALARY[1], EMP_SERIAL[1]);
    try {
        pm.currentTransaction().begin();
        mgr.addSubordinate(emp1);
        pm.makePersistent(mgr);
        pm.currentTransaction().commit();
    } finally {
        if (pm.currentTransaction().isActive()) {
            pm.currentTransaction().rollback();
            pm.close();
            fail("Failed to persist object and commit transaction");
        }
        pm.close();
    }
    // get a fresh PM to ensure that any results aren't coming from the cache
    try {
        pm = pmf.getPersistenceManager();
        pm.currentTransaction().begin();
        Extent ext = pm.getExtent(Manager.class, false);
        java.util.Iterator it = ext.iterator();
        assertTrue(it.hasNext());
        mgr = (Manager) it.next();
        Collection c = mgr.getSubordinates();
        assertEquals(1, c.size());
        ext = pm.getExtent(Employee.class, false);
        it = ext.iterator();
        assertTrue(it.hasNext());
        Employee emp = (Employee) it.next();
        assertTrue(c.contains(emp));
    } finally {
        if (pm.currentTransaction().isActive())
            pm.currentTransaction().rollback();
        pm.close();
    }
}
Also used : Employee(org.jpox.samples.models.company.Employee) Iterator(java.util.Iterator) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) Extent(javax.jdo.Extent) Collection(java.util.Collection) Manager(org.jpox.samples.models.company.Manager) StoreManager(org.datanucleus.store.StoreManager) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager)

Aggregations

Employee (org.jpox.samples.models.company.Employee)129 PersistenceManager (javax.jdo.PersistenceManager)126 Transaction (javax.jdo.Transaction)119 Query (javax.jdo.Query)68 JDOUserException (javax.jdo.JDOUserException)62 Manager (org.jpox.samples.models.company.Manager)46 List (java.util.List)44 JDOPersistenceManager (org.datanucleus.api.jdo.JDOPersistenceManager)37 JDOObjectNotFoundException (javax.jdo.JDOObjectNotFoundException)24 Person (org.jpox.samples.models.company.Person)24 Collection (java.util.Collection)21 PersistenceManagerFactory (javax.jdo.PersistenceManagerFactory)18 Iterator (java.util.Iterator)17 Properties (java.util.Properties)17 JDODetachedFieldAccessException (javax.jdo.JDODetachedFieldAccessException)16 SQLException (java.sql.SQLException)12 StoreManager (org.datanucleus.store.StoreManager)12 Department (org.jpox.samples.models.company.Department)12 ArrayList (java.util.ArrayList)11 HashSet (java.util.HashSet)11