Java Reference
In-Depth Information
parameters specify the desired page, and the OrderSearchCriteria parameter con-
tains the search criteria entered by the user. It returns a PagedQueryResult that con-
tains a list of Order objects and a boolean flag indicating whether there are
more.The OrderRepository interface is implemented by the JDOOrderRepository-
Impl class. This class generates a query from the OrderSearchCriteria and uses a
Spring JdoTemplate to execute it. Because it uses the JDO API s to configure the
fetch groups, the query is executed using a JdoCallback rather than a JdoTemplate
convenience method. Let's look at the JDOOrderRepositoryImpl and JDOFind-
OrdersCallback classes.
11.4.1
The JDOOrderRepositoryImpl class
The biggest challenge when implementing a method such as findOrders() is
dynamically constructing the query. JDO does not provide any support for gener-
ating queries dynamically. The repository must concatenate fragments of JDOQL
to create a complete query, which requires some conditional logic. Listing 11.2
shows findOrders() and its helper methods, which construct a query using a
StringBuffer .
Listing 11.2
JDOOrderRepositoryImpl
public class JDOOrderRepositoryImpl extends JdoDaoSupport implements
OrderRepository {
public JDOOrderRepositoryImpl(JdoTemplate jdoTemplate) {
setJdoTemplate(jdoTemplate);
}
B Calls helper methods
to execute query
public PagedQueryResult
findOrders(int startIndex,
int pageSize,
OrderSearchCriteria criteria) {
StringBuffer queryString = makeSelectFrom();
Map parameters = addWhere(queryString, criteria);
addOrderBy(queryString, criteria);
addRange(queryString, startIndex, pageSize);
return executePagedQuery(pageSize, queryString, parameters);
}
C Creates
start of query
private StringBuffer makeSelectFrom() {
StringBuffer queryString = new StringBuffer();
queryString
.append("select from ")
.append(Order.class.getName());
return queryString;
 
 
Search WWH ::




Custom Search