-->
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.  [ 5 posts ] 
Author Message
 Post subject: Ordering by attribute of associated entity
PostPosted: Thu Sep 24, 2009 10:56 am 
Newbie

Joined: Tue Aug 26, 2008 12:18 pm
Posts: 9
Hello,

I have very simple question, but I couldn't find answer anywhere.
How can I order the result of Criteria query by the atribute of asociated entity?

For example if you want to order the result of Childs entities by the age of their Mother, where Mother is many-to-one asociated entity.
I succed when I did this by HQL: "from Child child order by child.mother.age asc";

But I need to work with Criteria object, so I need something like this:

Code:
DetachedCriteria crit = DetachedCriteria.forClass(Child.class);
crit.addOrder(Order.asc("mother.age"));


But this doesn't work, hibernate throws exception with the message, that attribute 'mother.age' of class Child is unknown.
Does anybody knows, if this kind of ordering is possible in Criteria queries?

Thanks in advance.

_________________
Arguing on the Internet is like running in the Special Olympics.

Even if you win, you’re still retarded. :P


Top
 Profile  
 
 Post subject: Re: Ordering by attribute of associated entity
PostPosted: Thu Sep 24, 2009 12:21 pm 
Newbie

Joined: Tue Jan 06, 2009 5:03 am
Posts: 7
If you always want ordering by age in that case you can specify this in annotation above collection in your Parent class

@OrderBy(age) .

Then child elements will always be in order by age.
May be helpful


Top
 Profile  
 
 Post subject: Re: Ordering by attribute of associated entity
PostPosted: Thu Sep 24, 2009 4:08 pm 
Newbie

Joined: Tue Aug 26, 2008 12:18 pm
Posts: 9
Thanks, but it is not, what I wask asking about.
This annotation only causes, that all childs in collection will be ordered.

What do I need is to order in criteria query by attribute of associated entity (e.g. order childs by age of their mothers), as I'm describing in my first, original post.

_________________
Arguing on the Internet is like running in the Special Olympics.

Even if you win, you’re still retarded. :P


Top
 Profile  
 
 Post subject: Re: Ordering by attribute of associated entity
PostPosted: Fri Sep 25, 2009 11:13 am 
Beginner
Beginner

Joined: Wed Jul 09, 2008 5:34 am
Posts: 41
Location: Brno, Czech Republic
You missed the createAlias :-)

Code:
      DetachedCriteria c = DetachedCriteria.forClass(Child.class)
         .createAlias("mother", "m")
         .addOrder(Order.asc("m.age"));

http://docs.jboss.org/hibernate/core/3. ... teria.html - Section 15.4


Top
 Profile  
 
 Post subject: Re: Ordering by attribute of associated entity
PostPosted: Mon Sep 28, 2009 5:11 pm 
Newbie

Joined: Tue Aug 26, 2008 12:18 pm
Posts: 9
Thank you very much, jpkrohling.

This was exactly what I was looking for!

_________________
Arguing on the Internet is like running in the Special Olympics.

Even if you win, you’re still retarded. :P


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.