Java Reference
In-Depth Information
then executes a SELECT statement that does a join between the PLACED_ORDER and
RESTAURANT tables and returns only those columns corresponding to the fields
specified in the fetch group.
Using JDO projection queries
Another way a JDO application can load a subset of an object's fields is to use pro-
jection queries, which are a new feature of JDO 2.0 , and return DTO s containing
selected fields. Here is an example of how to use the Kodo JDO 3 . x extensions to
execute a projection query that finds all orders whose delivery date is in the past
week. It returns a collection of OrderSummaryDTO objects containing the ID , deliv-
ery time, phone number, email, and restaurant name of each order:
public class JDOOrderRepositoryImpl extends
JdoDaoSupport implements OrderRepository {
public List findOrdersUsingProjection() {
return (List) getJdoTemplate().executeFind(
new ProjectionQueryCallback());
}
private final class ProjectionQueryCallback
implements JdoCallback {
public Object doInJdo(PersistenceManager pm)
throws JDOException {
Calendar c = Calendar.getInstance();
c.add(Calendar.DAY_OF_WEEK, -7);
Date deliveryTime = c.getTime();
String queryString =
"select id as orderId, deliveryTime, "
+ " paymentInformation.email as email, "
+ " paymentInformation.phoneNumber as phoneNumber, "
+ " restaurant.name as restaurantName "
+ " into " + OrderSummaryDTO.class.getName()
+ " from " + Order.class.getName()
+ " where deliveryTime >= :pDeliveryTime";
Query query = pm.newQuery(queryString);
Map map = new HashMap();
map.put("pDeliveryTime", deliveryTime);
List result = (List) query
.executeWithMap(map);
return result;
}
}
}
 
Search WWH ::




Custom Search