Search in sources :

Example 1 with PCFKListElementSub1

use of org.jpox.samples.one_many.collection.PCFKListElementSub1 in project tests by datanucleus.

the class RelationshipTest method test1toNUnidirFKListInheritanceTarget.

/**
 * Test 1-N uni FK List relation having subclasses on the target side (1=source;N=target)
 */
public void test1toNUnidirFKListInheritanceTarget() {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        Object holderId = null;
        // Persist some objects
        try {
            tx.begin();
            ListHolder holder = new ListHolder();
            PCFKListElementSub1 sub1 = new PCFKListElementSub1("Element 1");
            PCFKListElementSub2 sub2 = new PCFKListElementSub2("Element 2");
            holder.getFkListPC().add(sub1);
            holder.getFkListPC().add(sub2);
            pm.makePersistent(holder);
            tx.commit();
            holderId = pm.getObjectId(holder);
        } catch (Exception e) {
            LOG.error("Exception in test", e);
            fail("Exception in test : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Query the objects
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            ListHolder holder = (ListHolder) pm.getObjectById(holderId, true);
            List elements = holder.getFkListPC();
            assertEquals("Number of elements in 1-N unidir FK with inheritance is incorrect", 2, elements.size());
            Object element = null;
            element = elements.get(0);
            assertTrue("First element of 1-N uni FK list is incorrect type", element instanceof PCFKListElementSub1);
            element = elements.get(1);
            assertTrue("Second element of 1-N uni FK list is incorrect type", element instanceof PCFKListElementSub2);
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception in test", e);
            fail(e.toString());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out our data
        clean(ListHolder.class);
        clean(PCFKListElementSub1.class);
        clean(PCFKListElementSub1.class);
    }
}
Also used : PCFKListElementSub1(org.jpox.samples.one_many.collection.PCFKListElementSub1) Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) List(java.util.List) ArrayList(java.util.ArrayList) PCFKListElementSub2(org.jpox.samples.one_many.collection.PCFKListElementSub2) ListHolder(org.jpox.samples.one_many.collection.ListHolder) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException)

Aggregations

ArrayList (java.util.ArrayList)1 List (java.util.List)1 JDOObjectNotFoundException (javax.jdo.JDOObjectNotFoundException)1 PersistenceManager (javax.jdo.PersistenceManager)1 Transaction (javax.jdo.Transaction)1 ListHolder (org.jpox.samples.one_many.collection.ListHolder)1 PCFKListElementSub1 (org.jpox.samples.one_many.collection.PCFKListElementSub1)1 PCFKListElementSub2 (org.jpox.samples.one_many.collection.PCFKListElementSub2)1