I can do this with Criteria:
Code:
DetachedCriteria lineItemCriteria = DetachedCriteria.forClass(LineItem.class)
.addOrder(Order.asc("product"));
List<LineItem> lineItems = lineItemCriteria.getExecutableCriteria(session)
.list();
where "product" is an object (of type Product) within the LineItem class. The result is sorted by product number, which is the primary key of the product table, and the key held in each row of LineItem.
What if I want to sort by product name instead of number? This code:
Code:
DetachedCriteria lineItemCriteria = DetachedCriteria.forClass(LineItem.class)
.addOrder(Order.asc("product.productName"));
leads to this exception: org.hibernate.QueryException: could not resolve property: product.productName of: pojos.LineItem (pojos.LineItem being the class describing LineItem).
Results are the same for:
Code:
DetachedCriteria lineItemCriteria = DetachedCriteria.forClass(LineItem.class)
.addOrder(Property.forName("product").getProperty("productName").asc());
This is a recreation of a problem I'm having at work, where generated query joins the tables (not so here, but the outcome is the same). And yes, I have the names right.
I'm using DetachedCriteria rather than Criteria because in the work environment, I don't have access to the session at this point in the code.
Is there a proper way to use the Criteria API to order on a non-key column of a a contained class--especially using DetachedCriteria?