use of org.jpox.samples.identity.application.FourByFour 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);
}
}
Aggregations