-->
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.  [ 6 posts ] 
Author Message
 Post subject: Ordering by Nested properties Criteria API
PostPosted: Thu Dec 08, 2005 11:44 am 
Newbie

Joined: Fri Aug 12, 2005 12:13 pm
Posts: 5
Hi

Has anyone successfully ordered by properties in associated objects? I can't find any reference in the documentation.

E.g. Suppose I have objects Company and Director and I want to order Company objects by Director.surname.

Thanks

Kevin


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 08, 2005 12:02 pm 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
there is no reason that it wouldn't work. criteria generates a sql statement just like hql.

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject: Ordering by Nested properties Criteria API
PostPosted: Thu Dec 08, 2005 12:23 pm 
Newbie

Joined: Fri Aug 12, 2005 12:13 pm
Posts: 5
I tried it using dot notation which is how I would have expected it to be implemented and it didn't work.
E.g. I have objects A, B.
A has a B called 'b'. B has a property called 'name'.
I tried criteria.addOrder(Order.asc("b.name")). Hibernate complains that it can't find the property b.name.
It's works using properties of A so for example:
criteria.addOrder(Order.asc("somePropertyName")) works fine.
I really want to know if this is possible in Hibernate and then has anyone seen this documented anywhere or has an example so I can get the syntax right.
Thanks
Kevin


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 08, 2005 12:37 pm 
Newbie

Joined: Fri Nov 25, 2005 12:26 pm
Posts: 4
I belive u're asking the same thing i'm asking in a thread arround here .

Can anybody shed any light on this ? can it be done or not ?

I was also unable to find anything in the docs about it , but it looks like a big limitation if it couldn't be done ... i'm hoping that i've just missed it


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 08, 2005 12:52 pm 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
This will do it!

Code:
Criteria j = session.createCriteria(A.class).createCriteria("b").addOrder(Order.asc("name"));



you have to tell hibernate to look in that class for the property :)

note, make sure to use the attribute name (how it exists in class A) for the second createCriteria, not the class name.

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 08, 2005 12:57 pm 
Newbie

Joined: Fri Aug 12, 2005 12:13 pm
Posts: 5
Ahhh. That makes sense. Thank heavens for the forum! Many thanks. Kevin


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