Java Reference
In-Depth Information
where.append(" && ");
where.append("restaurant.name == :pRestaurant");
}
Query query = pm.createQuery(Order.class, where.toString());
List orders = (List)query.execute(…);
}
}
The code contains conditional logic that concatenates JDOQL fragments. Sec-
tion 11.4 shows an in-depth example that uses JDOQL queries.
Using Hibernate criteria queries
Hibernate, on the other hand, has criteria queries, which provide an OO API for
constructing object queries dynamically. The application instantiates objects and
calls methods instead of concatenating string fragments, which makes the code
significantly simpler. Here is a code fragment that shows how to use a Hibernate
criteria query:
public class HibernateOrderRepositoryImpl … {
public PagedQueryResult findOrders(int startingIndex,
int pageSize,
OrderSearchCriteria searchCriteria) {
Criteria criteria = session
.createCriteria(Order.class);
if (searchCriteria.isDeliveryTimeSpecified())
criteria.add(Restrictions.ge("deliveryTime",
searchCriteria.getDeliveryTime()));
if (searchCriteria.isRestaurantSpecified()) {
criteria.createCriteria("restaurant").add(
Restrictions.like("name", searchCriteria
.getRestaurantName()));
List orders = criteria.list();
}
}
This code fragment creates a criteria object by calling a Session.createCriteria()
and then adds restrictions to it based on the properties of the OrderSearchCrite-
ria . We must still write conditional logic, but the code is a lot cleaner because it
 
 
Search WWH ::




Custom Search