Search in sources :

Example 41 with Extent

use of javax.jdo.Extent 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 42 with Extent

use of javax.jdo.Extent 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 43 with Extent

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

the class PersistenceManagerTest method testMakePersistent.

/**
 * Tests storage of various datatypes using the makePersistent method.
 */
public void testMakePersistent() throws Exception {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            // Test insert of native and native wrapper data types
            BigDecimal bd = new BigDecimal("12345.12345");
            BigInteger bi = new BigInteger("12345");
            java.util.Date date1 = (new java.util.GregorianCalendar()).getTime();
            java.sql.Date date2 = java.sql.Date.valueOf("2001-01-01");
            java.sql.Time time3 = java.sql.Time.valueOf("10:01:59");
            java.sql.Timestamp timestamp = java.sql.Timestamp.valueOf("2001-01-01 23:23:23.050500000");
            tx.begin();
            Primitive p = new Primitive();
            setPrimitiveValues(p, true, (byte) 23, 'z', 33, (short) 43, 123456789L, 123.456F, 123.456, "fixed", "normal", "huge", bd, bi, date1, date2, time3, timestamp);
            pm.makePersistent(p);
            tx.commit();
            p = null;
            tx.begin();
            // Find the Primitive and check that it was persisted correctly
            Extent clnPrimitive = pm.getExtent(org.datanucleus.samples.widget.Primitive.class, false);
            p = (Primitive) clnPrimitive.iterator().next();
            assertNotNull(p);
            assertPrimitiveValues(p, true, (byte) 23, 'z', 33, (short) 43, 123456789L, 123.456F, 123.456, "fixed", "normal", "huge", bd, bi, date1, date2, time3, timestamp);
            Object id = pm.getObjectId(p);
            tx.commit();
            tx.begin();
            pm.makePersistent(p);
            tx.commit();
            Object id2 = pm.getObjectId(p);
            assertEquals(id, id2);
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out our data
        clean(Primitive.class);
    }
}
Also used : JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) Extent(javax.jdo.Extent) BigDecimal(java.math.BigDecimal) InversePrimitive(org.datanucleus.samples.widget.InversePrimitive) Primitive(org.datanucleus.samples.widget.Primitive) Transaction(javax.jdo.Transaction) BigInteger(java.math.BigInteger)

Example 44 with Extent

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

the class PersistenceManagerTest method testNonTransactionalUpdateWithRollback.

/**
 * Test for NontransactionalWrite and use of rollback after an update.
 */
public void testNonTransactionalUpdateWithRollback() throws Exception {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        Primitive p = null;
        Integer intValue = null;
        Short shortValue = null;
        try {
            // Test insert of native and native wrapper data types
            BigDecimal bd = new BigDecimal("12345.12345");
            BigInteger bi = new BigInteger("12345");
            java.util.Date date1 = (new java.util.GregorianCalendar()).getTime();
            java.sql.Date date2 = java.sql.Date.valueOf("2001-01-01");
            java.sql.Time time3 = java.sql.Time.valueOf("10:01:59");
            java.sql.Timestamp timestamp = java.sql.Timestamp.valueOf("2001-01-01 23:23:23.050500000");
            p = new Primitive();
            setPrimitiveValues(p, true, (byte) 23, 'z', 33, (short) 43, 123456789L, 123.456F, 123.456, "fixed", "normal", "huge", bd, bi, date1, date2, time3, timestamp);
            intValue = p.getIntObject();
            shortValue = p.getShortObject();
            tx.setRetainValues(true);
            tx.setNontransactionalRead(true);
            tx.setNontransactionalWrite(true);
            tx.begin();
            pm.makePersistent(p);
            tx.commit();
            // Make a nontransactional update
            p.setIntObject(new Integer(1));
            p.setShortObject(new Short((short) 2));
            // Necessary when using nontx.atomic for updates
            intValue = 1;
            // Necessary when using nontx.atomic for updates
            shortValue = 2;
            // Make a change and roll it back
            tx.setRestoreValues(true);
            tx.begin();
            p.setIntObject(new Integer(3));
            tx.rollback();
            assertEquals(1, p.getIntObject().intValue());
            assertEquals(2, p.getShortObject().shortValue());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        tx.setNontransactionalRead(true);
        tx.setNontransactionalWrite(true);
        try {
            // Check the datastore for the effect of the updates
            Extent clnPrimitive = pm.getExtent(org.datanucleus.samples.widget.Primitive.class, false);
            p = (Primitive) clnPrimitive.iterator().next();
            assertNotNull(p);
            assertEquals(intValue.intValue(), p.getIntObject().intValue());
            assertEquals(shortValue.shortValue(), p.getShortObject().shortValue());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out our data
        clean(Primitive.class);
    }
}
Also used : JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) Extent(javax.jdo.Extent) BigDecimal(java.math.BigDecimal) BigInteger(java.math.BigInteger) InversePrimitive(org.datanucleus.samples.widget.InversePrimitive) Primitive(org.datanucleus.samples.widget.Primitive) Transaction(javax.jdo.Transaction) BigInteger(java.math.BigInteger)

Example 45 with Extent

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

the class CompoundIdentityTest method testOneToManyUniJoinTableDetachAttach.

/**
 * Test for detach-attach of 1-N uni join table compound relations.
 */
public void testOneToManyUniJoinTableDetachAttach() {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        Object id = null;
        Object[] elements_single = new Object[6];
        Object[] elements_double = new Object[6];
        try {
            tx.begin();
            CompoundHolder holder1 = new CompoundHolder("First Holder");
            CompoundRelated related1 = new CompoundRelated("First Related");
            elements_single[0] = new CompoundSingleTarget(holder1, 1.0);
            elements_single[1] = new CompoundSingleTarget(holder1, 2.0);
            elements_single[2] = new CompoundSingleTarget(holder1, 3.0);
            elements_single[3] = new CompoundSingleTarget(holder1, 4.0);
            holder1.getList1().add(elements_single[0]);
            holder1.getList1().add(elements_single[1]);
            holder1.getList1().add(elements_single[2]);
            holder1.getList1().add(elements_single[3]);
            elements_double[0] = new CompoundDoubleTarget(holder1, related1, 1.0);
            elements_double[1] = new CompoundDoubleTarget(holder1, related1, 2.0);
            elements_double[2] = new CompoundDoubleTarget(holder1, related1, 3.0);
            elements_double[3] = new CompoundDoubleTarget(holder1, related1, 4.0);
            holder1.getList2().add(elements_double[0]);
            holder1.getList2().add(elements_double[1]);
            holder1.getList2().add(elements_double[2]);
            holder1.getList2().add(elements_double[3]);
            pm.makePersistent(holder1);
            tx.commit();
            id = pm.getObjectId(holder1);
            // Detach the holder and elements
            tx.begin();
            pm.getFetchPlan().addGroup("detach");
            CompoundHolder holder2 = (CompoundHolder) pm.detachCopy(pm.getObjectById(id, true));
            tx.commit();
            // Add some more elements (single, double) to the holder
            elements_single[4] = new CompoundSingleTarget(holder1, 5.0);
            elements_single[5] = new CompoundSingleTarget(holder1, 6.0);
            holder2.getList1().add(elements_single[4]);
            holder2.getList1().add(elements_single[5]);
            elements_double[4] = new CompoundDoubleTarget(holder1, related1, 5.0);
            elements_double[5] = new CompoundDoubleTarget(holder1, related1, 6.0);
            holder2.getList2().add(elements_double[4]);
            holder2.getList2().add(elements_double[5]);
            // Attach the holder and elements
            tx.begin();
            pm.makePersistent(holder2);
            tx.commit();
            // Check the results
            tx.begin();
            CompoundHolder holder3 = (CompoundHolder) pm.getObjectById(id, true);
            assertEquals(6, holder3.getList1().size());
            assertEquals(elements_single[0], holder3.getList1().get(0));
            assertEquals(elements_single[1], holder3.getList1().get(1));
            assertEquals(elements_single[2], holder3.getList1().get(2));
            assertEquals(elements_single[3], holder3.getList1().get(3));
            assertEquals(elements_single[4], holder3.getList1().get(4));
            assertEquals(elements_single[5], holder3.getList1().get(5));
            assertEquals(6, holder3.getList2().size());
            assertEquals(elements_double[0], holder3.getList2().get(0));
            assertEquals(elements_double[1], holder3.getList2().get(1));
            assertEquals(elements_double[2], holder3.getList2().get(2));
            assertEquals(elements_double[3], holder3.getList2().get(3));
            assertEquals(elements_double[4], holder3.getList2().get(4));
            assertEquals(elements_double[5], holder3.getList2().get(5));
            tx.commit();
        } catch (Exception e) {
            LOG.error(e);
            e.printStackTrace();
            fail(e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Extent ex = pm.getExtent(CompoundHolder.class);
            Iterator iter = ex.iterator();
            while (iter.hasNext()) {
                CompoundHolder holder = (CompoundHolder) iter.next();
                holder.getList1().clear();
                holder.getList2().clear();
            }
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        clean(CompoundHolder.class);
        clean(CompoundSingleTarget.class);
        clean(CompoundDoubleTarget.class);
        clean(CompoundRelated.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) CompoundSingleTarget(org.jpox.samples.compoundidentity.CompoundSingleTarget) PersistenceManager(javax.jdo.PersistenceManager) Extent(javax.jdo.Extent) CompoundRelated(org.jpox.samples.compoundidentity.CompoundRelated) Iterator(java.util.Iterator) CompoundDoubleTarget(org.jpox.samples.compoundidentity.CompoundDoubleTarget) CompoundHolder(org.jpox.samples.compoundidentity.CompoundHolder)

Aggregations

Extent (javax.jdo.Extent)72 PersistenceManager (javax.jdo.PersistenceManager)72 Transaction (javax.jdo.Transaction)70 Iterator (java.util.Iterator)62 JDOUserException (javax.jdo.JDOUserException)35 Collection (java.util.Collection)22 JDOPersistenceManager (org.datanucleus.api.jdo.JDOPersistenceManager)22 Query (javax.jdo.Query)15 Manager (org.jpox.samples.models.company.Manager)13 StoreManager (org.datanucleus.store.StoreManager)11 Method (java.lang.reflect.Method)10 JDOObjectNotFoundException (javax.jdo.JDOObjectNotFoundException)10 PersistenceManagerFactory (javax.jdo.PersistenceManagerFactory)10 Employee (org.jpox.samples.models.company.Employee)10 JDOException (javax.jdo.JDOException)9 SQLException (java.sql.SQLException)8 Department (org.jpox.samples.models.company.Department)8 InversePrimitive (org.datanucleus.samples.widget.InversePrimitive)7 Primitive (org.datanucleus.samples.widget.Primitive)6 List (java.util.List)5