-->
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.  [ 13 posts ] 
Author Message
 Post subject: Criteria API .addOrder(...) problem.
PostPosted: Tue Dec 02, 2003 3:59 pm 
Senior
Senior

Joined: Sun Aug 31, 2003 3:14 pm
Posts: 151
Location: Earth (at the moment)
I'm displaying lists of persisted objects in html grids in which the user can click on the columns to pick which property they want to order by.

This all works just fine until one of the columns displayed is a sub property on a child, then I can't seem to create the ordering and instead get Exceptions:

if I try to do:
Code:
criteria.addOrder(Order.asc("child.name"));

I get the Exception:
Code:
net.sf.hibernate.QueryException: could not resolve property: child.name of: package.Parent
        at net.sf.hibernate.persister.AbstractPropertyMapping.toColumns(AbstractPropertyMapping.java:99)
        at net.sf.hibernate.expression.AbstractCriterion.getColumns(AbstractCriterion.java:69)
        at net.sf.hibernate.expression.Order.toSqlString(Order.java:73)
        at net.sf.hibernate.loader.CriteriaLoader.<init>(CriteriaLoader.java:149)
        at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:3430)
        at net.sf.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:353)


if I try to do (which I think is basically the same as above?):
Code:
criteria = criteria.createAlias("child", "kid");
criteria.addOrder(Order.asc("kid.name"));

I get the Exception:
Code:
net.sf.hibernate.QueryException: could not resolve property: kid.name of: package.Parent
        at net.sf.hibernate.persister.AbstractPropertyMapping.toColumns(AbstractPropertyMapping.java:99)
        at net.sf.hibernate.expression.AbstractCriterion.getColumns(AbstractCriterion.java:69)
        at net.sf.hibernate.expression.Order.toSqlString(Order.java:73)
        at net.sf.hibernate.loader.CriteriaLoader.<init>(CriteriaLoader.java:149)
        at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:3430)
        at net.sf.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:353)


if I try to do:
Code:
criteria = criteria.createCriteria("child");
criteria.addOrder(Order.asc("name"));

I get the Exception:
Code:
java.lang.UnsupportedOperationException: subcriteria cannot be ordered
     at net.sf.hibernate.impl.CriteriaImpl$Subcriteria.addOrder(CriteriaImpl.java:139)


Is there a way to do this and I'm just not doing it correctly or is this simply not supported no matter what?
I'm using the latest v21branch from CVS as of this posting.
Any help would be greatly appreciated.
Thank-you.

P.S. I can order by sub properties of components, just not children.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 02, 2003 6:58 pm 
Expert
Expert

Joined: Tue Sep 16, 2003 4:06 pm
Posts: 318
Location: St. Petersburg, Russia
It looks you last query should do the job. But you need to put subcriteria into another variable and add order to "outer" criteria.

Code:
    subCriteria = criteria.createCriteria("child");
    criteria.addOrder(Order.asc("name"));


I haven't tries it. Just a guess...


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 02, 2003 7:26 pm 
Senior
Senior

Joined: Sun Aug 31, 2003 3:14 pm
Posts: 151
Location: Earth (at the moment)
If I ignore the subCriteria it would be as if I never made that call and it will try to find the field "name" on the parent where it does not exist.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 02, 2003 7:33 pm 
Expert
Expert

Joined: Tue Sep 16, 2003 4:06 pm
Posts: 318
Location: St. Petersburg, Russia
What about

Code:
    subCriteria = criteria.createCriteria("child");
    criteria.addOrder(Order.asc("child.name"));


?

Also some of createCriteria() methods allow specifying alias. Maybe if you can set alias to child, you will be able to order by it using ALIAS.name...


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 02, 2003 8:40 pm 
Senior
Senior

Joined: Sun Aug 31, 2003 3:14 pm
Posts: 151
Location: Earth (at the moment)
Thank-you for your help,

I do appreciate your willingness to assist, however, if you re-read my original post you will notice that I already tried creating the sub Criteria and the Alias and both fail.
As for creating a criteria and ignoring it, that is essentially the same as creating a new String via the + operator and expecting to get the new value out of the original immutable String when they aren't the same instance.
I have tried your suggestions anyway, just in case, but they do not work.

Sincerely,
David


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 02, 2003 10:01 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
It is not possible to order by a property of an associated object in the Criteria API. It seems a bit strange, since Criteria queries ALWAYs return the parent object. (Yes, I realise it is not necessarily wrong to order parents by properties of children.)


Top
 Profile  
 
 Post subject: I kind of suspected that...
PostPosted: Tue Dec 02, 2003 11:45 pm 
Senior
Senior

Joined: Sun Aug 31, 2003 3:14 pm
Posts: 151
Location: Earth (at the moment)
Will that functionality be available in future by any chance?

Thank-you very much for letting me know I can't do it and sparing me any further attempts.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 02, 2003 11:54 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Well, its not difficult to implement. But certainly not a priority.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 01, 2004 4:55 pm 
Senior
Senior

Joined: Sun Aug 31, 2003 3:14 pm
Posts: 151
Location: Earth (at the moment)
Quote:
Well, its not difficult to implement. But certainly not a priority.


I was just going through the todo's in my code and a couple of them are regarding this functionality. So, I'm just wondering if this might be coming in the near future and if not, where should I look in the code to try and do it myself and submit a patch to JIRA?

Thank-you.


Top
 Profile  
 
 Post subject: Any progress or plans in this area?
PostPosted: Wed May 19, 2004 11:30 am 
Newbie

Joined: Wed Apr 28, 2004 2:12 pm
Posts: 6
... I have just bumped up against the problem described by the original poster, namely, sorting html table columns by columns joined to 'this' object.


Seems to me that HQL will solve this problem, but I'm really liking the Criteria API for everything up to this point.

Thanks,
Tim Lucia


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 19, 2004 11:34 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
There's a JIRA entry you can vote forhttp://opensource.atlassian.com/projects/hibernate/browse/HB-132


Top
 Profile  
 
 Post subject: Re: Criteria API .addOrder(...) problem.
PostPosted: Wed Apr 21, 2010 4:48 am 
Newbie

Joined: Wed Apr 21, 2010 4:42 am
Posts: 1
The subCriteria has to be used, like this:
Code:
subCriteria = criteria.createCriteria("child");
subCriteria.addOrder(Order.asc("name"));


Top
 Profile  
 
 Post subject: Re: Criteria API .addOrder(...) problem.
PostPosted: Wed Mar 09, 2011 5:16 pm 
Newbie

Joined: Wed Mar 09, 2011 5:14 pm
Posts: 2
above solution works.


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

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.