Search in sources :

Example 1 with Car

use of org.jpox.samples.identity.application.Car in project tests by datanucleus.

the class ApplicationIdentityTest method testQueryUsingPrimaryKeyFields.

/**
 * Test for querying of PK fields of a composite PK.
 * Query of candidate class as well as 1-1 relation FK fields.
 */
public void testQueryUsingPrimaryKeyFields() {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Car audi = new Car("A6", "O6");
            Car mercedes = new Car("Class E", "OE");
            Car volks = new Car("Beetle", "OB");
            FourByFour c1 = new FourByFour("C1", "O1");
            FourByFour c2 = new FourByFour("C2", "O2");
            audi.setTowedCar(c1);
            mercedes.setTowedCar(c2);
            pm.makePersistentAll(new Car[] { audi, mercedes, volks });
            pm.flush();
            Query q = pm.newQuery(Car.class);
            q.declareParameters("String pOwnerID, String pCarID");
            q.setFilter("ownerID == pOwnerID && carID == pCarID");
            Collection c = (Collection) q.execute("O6", "A6");
            assertEquals(1, c.size());
            assertEquals("A6", ((Car) c.iterator().next()).getCarID());
            assertEquals("O6", ((Car) c.iterator().next()).getOwnerID());
            q = pm.newQuery(Car.class);
            q.declareVariables("Car tow");
            q.declareParameters("String pOwnerID, String pCarID");
            q.setFilter("towedCar == tow && tow.ownerID == pOwnerID && tow.carID == pCarID");
            c = (Collection) q.execute("O1", "C1");
            assertEquals(1, c.size());
            assertEquals("A6", ((Car) c.iterator().next()).getCarID());
            assertEquals("O6", ((Car) c.iterator().next()).getOwnerID());
            q = pm.newQuery(Car.class);
            q.declareParameters("String pOwnerID, String pCarID");
            q.setFilter("towedCar.ownerID == pOwnerID && towedCar.carID == pCarID");
            c = (Collection) q.execute("O1", "C1");
            assertEquals(1, c.size());
            assertEquals("A6", ((Car) c.iterator().next()).getCarID());
            assertEquals("O6", ((Car) c.iterator().next()).getOwnerID());
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.commit();
            }
            pm.close();
        }
    } finally {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Extent ex = pm.getExtent(Car.class, true);
            Iterator iter = ex.iterator();
            while (iter.hasNext()) {
                Car car = (Car) iter.next();
                car.setTowedCar(null);
            }
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        clean(Car.class);
        clean(FourByFour.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Car(org.jpox.samples.identity.application.Car) Extent(javax.jdo.Extent) FourByFour(org.jpox.samples.identity.application.FourByFour) Iterator(java.util.Iterator) Collection(java.util.Collection)

Aggregations

Collection (java.util.Collection)1 Iterator (java.util.Iterator)1 Extent (javax.jdo.Extent)1 PersistenceManager (javax.jdo.PersistenceManager)1 Query (javax.jdo.Query)1 Transaction (javax.jdo.Transaction)1 Car (org.jpox.samples.identity.application.Car)1 FourByFour (org.jpox.samples.identity.application.FourByFour)1