Search in sources :

Example 1 with FNameView2

use of org.jpox.samples.rdbms.views.FNameView2 in project tests by datanucleus.

the class ViewTest method testNameViewWithId.

/**
 * Use of a simple view for an object, using application identity for the view objects.
 */
public void testNameViewWithId() {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            pm.makePersistent(new NameObject(1, "FIRST"));
            pm.makePersistent(new NameObject(2, "SECOND"));
            pm.makePersistent(new NameObject(3, "THIRD"));
            pm.makePersistent(new NameObject(4, "FOURTH"));
            tx.commit();
            tx.begin();
            Query q = pm.newQuery("SELECT FROM " + NameObject.class.getName() + " ORDER BY id");
            List<NameObject> persons = (List<NameObject>) q.execute();
            assertEquals(4, persons.size());
            assertEquals("FIRST", persons.get(0).getName());
            Query q1 = pm.newQuery("SELECT FROM " + FNameView2.class.getName() + " ORDER BY id");
            List<FNameView2> fNames = ((List<FNameView2>) q1.execute());
            assertEquals(2, fNames.size());
            assertEquals("FIRST", fNames.get(0).getName());
            assertEquals("FOURTH", fNames.get(1).getName());
            tx.commit();
        } catch (Throwable thr) {
            LOG.error(">> Exception thrown persist/view data with FNameView2", thr);
            fail("Failed to persist data : " + thr.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        clean(NameObject.class);
    }
}
Also used : FNameView2(org.jpox.samples.rdbms.views.FNameView2) NameObject(org.jpox.samples.rdbms.views.NameObject) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) List(java.util.List)

Aggregations

List (java.util.List)1 PersistenceManager (javax.jdo.PersistenceManager)1 Query (javax.jdo.Query)1 Transaction (javax.jdo.Transaction)1 FNameView2 (org.jpox.samples.rdbms.views.FNameView2)1 NameObject (org.jpox.samples.rdbms.views.NameObject)1