@SuppressWarnings("unchecked")
public void testQuerySubclass() {
Object id = null;
try {
getPM().currentTransaction().begin();
Kitchen kitchen = new Kitchen(1);
MultifunctionOven oven = new MultifunctionOven("Westinghouse", "Economy");
oven.setMicrowave(true);
oven.setCapabilities("TIMER,CLOCK");
kitchen.setOven(oven);
getPM().makePersistent(kitchen);
getPM().currentTransaction().commit();
id = getPM().getObjectId(kitchen);
}
catch (Exception e) {
fail("Exception on persist : " + e.getMessage());
}
finally {
if (getPM().currentTransaction().isActive()) {
getPM().currentTransaction().rollback();
}
}
getPM().currentTransaction().begin();
Query q1 = getPM().newQuery("SELECT FROM " + Kitchen.class.getName() +
" WHERE this.oven instanceof org.apache.jdo.tck.pc.building.MultifunctionOven");
List<Kitchen> kitchens1 = (List<Kitchen>)q1.execute();
assertNotNull("No results from query!", kitchens1);
assertEquals("Number of query results was incorrect", 1, kitchens1.size());
// Query using cast and a field of the subclass embedded class
Query q2 = getPM().newQuery("SELECT FROM " + Kitchen.class.getName() +
" WHERE ((org.apache.jdo.tck.pc.building.MultifunctionOven)this.oven).capabilities == 'TIMER,CLOCK'");
List<Kitchen> kitchens2 = (List<Kitchen>)q2.execute();
assertNotNull("No results from query!", kitchens2);
assertEquals("Number of query results was incorrect", 1, kitchens2.size());
Kitchen kit = kitchens2.iterator().next();
assertEquals("Kitchen result is incorrect", id, getPM().getObjectId(kit));
getPM().currentTransaction().commit();
}