-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Ordering on non-key column of contained object
PostPosted: Sun Jul 12, 2009 9:50 pm 
Newbie

Joined: Sun Jul 12, 2009 9:38 pm
Posts: 3
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?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.