Search in sources :

Example 76 with Manager

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

Example 77 with Manager

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

the class PersistenceManagerTest method testInheritedFieldsPersisted.

/**
 * Test that inherited fields are persisted.
 */
public void testInheritedFieldsPersisted() {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        Manager mgr = null;
        try {
            tx.begin();
            mgr = new Manager(0, FIRSTNAME[0], LASTNAME[0], EMAIL[0], EMP_SALARY[0], EMP_SERIAL[0]);
            pm.makePersistent(mgr);
            tx.commit();
            tx.begin();
            Extent ext = pm.getExtent(Manager.class, false);
            java.util.Iterator it = ext.iterator();
            assertTrue(it.hasNext());
            mgr = (Manager) it.next();
            assertEquals(FIRSTNAME[0], mgr.getFirstName());
            assertEquals(LASTNAME[0], mgr.getLastName());
            assertEquals(EMP_SALARY[0], mgr.getSalary(), 0.0F);
            pm.currentTransaction().commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
                pm.close();
                fail("Failed to persist object and commit transaction");
            }
            pm.close();
        }
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            // get a fresh pm to ensure that data is coming from the store,
            // not the cache
            tx.begin();
            Extent ext = pm.getExtent(Manager.class, false);
            java.util.Iterator it = ext.iterator();
            assertTrue(it.hasNext());
            mgr = (Manager) it.next();
            assertEquals(FIRSTNAME[0], mgr.getFirstName());
            assertEquals(LASTNAME[0], mgr.getLastName());
            assertEquals(EMP_SALARY[0], mgr.getSalary(), 0.0F);
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        clean(Manager.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) Iterator(java.util.Iterator) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) Extent(javax.jdo.Extent) Manager(org.jpox.samples.models.company.Manager) StoreManager(org.datanucleus.store.StoreManager) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager)

Example 78 with Manager

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

the class PersistenceManagerTest method testNormalFCOCollectionFieldPersistence3.

/**
 * Test that removing a member of a normal Collection field does NOT delete
 * the object that was removed
 */
public void testNormalFCOCollectionFieldPersistence3() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    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 {
        mgr.addSubordinate(emp1);
        tx.begin();
        pm.makePersistent(mgr);
        tx.commit();
        tx.begin();
        mgr.getSubordinates().remove(emp1);
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
            pm.close();
            fail("Failed to persist object and commit transaction");
        }
        pm.close();
    }
    pm = pmf.getPersistenceManager();
    tx = pm.currentTransaction();
    try {
        tx.begin();
        Extent ex = pm.getExtent(Employee.class, false);
        java.util.Iterator it = ex.iterator();
        assertTrue(it.hasNext());
        Employee emp = (Employee) it.next();
        assertEquals(1, emp.getPersonNum());
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Employee(org.jpox.samples.models.company.Employee) Transaction(javax.jdo.Transaction) Iterator(java.util.Iterator) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) Extent(javax.jdo.Extent) Manager(org.jpox.samples.models.company.Manager) StoreManager(org.datanucleus.store.StoreManager) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager)

Example 79 with Manager

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

the class PersistenceManagerTest method testInverseFCOCollectionFieldPersistence4.

/**
 * Test that setting the inverse reference of an object implicitly adds it
 * to the inverse collection of the "owning" object
 */
public void testInverseFCOCollectionFieldPersistence4() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    Manager mgr = new Manager(0, FIRSTNAME[0], LASTNAME[0], EMAIL[0], EMP_SALARY[0], EMP_SERIAL[0]);
    try {
        Department d = new Department("Engineering");
        d.setManager(mgr);
        tx.begin();
        pm.makePersistent(d);
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.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
    pm = pmf.getPersistenceManager();
    tx = pm.currentTransaction();
    try {
        tx.begin();
        Extent ext = pm.getExtent(Manager.class, false);
        java.util.Iterator it = ext.iterator();
        assertTrue(it.hasNext());
        mgr = (Manager) it.next();
        Collection c = mgr.getDepartments();
        assertEquals(1, c.size());
        ext = pm.getExtent(Department.class, false);
        it = ext.iterator();
        assertTrue(it.hasNext());
        Department d = (Department) it.next();
        assertTrue(c.contains(d));
        tx.commit();
    } finally {
        if (tx.isActive())
            tx.rollback();
        pm.close();
    }
}
Also used : Department(org.jpox.samples.models.company.Department) Transaction(javax.jdo.Transaction) 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)

Example 80 with Manager

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

the class PersistenceManagerTest method testTransientObjectCollections.

/**
 * Tests that objects can be added to a Collection owned by a persistent object made transient.
 */
public void testTransientObjectCollections() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    Employee emp = new Employee(1, FIRSTNAME[1], LASTNAME[1], EMAIL[1], EMP_SALARY[1], EMP_SERIAL[1]);
    Manager mgr = new Manager(0, FIRSTNAME[0], LASTNAME[0], EMAIL[0], EMP_SALARY[0], EMP_SERIAL[0]);
    try {
        tx.begin();
        pm.makePersistent(mgr);
        tx.commit();
        tx = pm.currentTransaction();
        tx.begin();
        pm.retrieve(mgr);
        pm.retrieveAll(mgr.getSubordinates());
        pm.retrieveAll(mgr.getDepartments());
        pm.makeTransient(mgr);
        mgr.addSubordinate(emp);
        tx.commit();
    } catch (Exception e) {
        LOG.error(">> Exception thrown in test", e);
        fail("Exception thrown while making object with collection transient : " + e.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Employee(org.jpox.samples.models.company.Employee) Transaction(javax.jdo.Transaction) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) Manager(org.jpox.samples.models.company.Manager) StoreManager(org.datanucleus.store.StoreManager) 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)

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