-->
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: How to order by a column in a child table?
PostPosted: Fri May 19, 2006 3:20 pm 
Senior
Senior

Joined: Sat Mar 25, 2006 9:16 am
Posts: 150
I am reading rows from table Foo with a join fetch mode with child table Bar. Everything works and I can order by doing this:

crit.AddOrder(NHibernate.Expression.Order.Asc("FooName"));

But I want to order the rows by a column in child table Bar. I want to do this:

crit.AddOrder(NHibernate.Expression.Order.Asc("Bar.BarName"));

However, this doesn't work (exception "could not resolve property:Bar.BarName of :NhibPOC.Foo").

How can I order by a column in the child table? Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 19, 2006 4:53 pm 
Expert
Expert

Joined: Fri May 13, 2005 5:56 pm
Posts: 308
Location: Santa Barbara, California, USA
you need a second criteria:

Code:
session.CreateCriteria(typeof(Foo))
    .CreateCriteria(typeof(Bar))
        .AddOrder(Order.Asc("BarName"))
    .List();


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 19, 2006 5:10 pm 
Senior
Senior

Joined: Sat Mar 25, 2006 9:16 am
Posts: 150
Thanks... In general, do I have to call List() on the 2nd criteria? Or can I call on either one? I'm just wondering, if I create a hierarchy of criteria, which one I would need to call List()

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 19, 2006 5:24 pm 
Expert
Expert

Joined: Fri May 13, 2005 5:56 pm
Posts: 308
Location: Santa Barbara, California, USA
hmmm, that's a good question. i've never tried to call list on anything but the top criteria because i always use method chaining. that sort of forces me to call list to end the chain... sorry i don't know the official answer for that.

-devon


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 19, 2006 7:42 pm 
Senior
Senior

Joined: Sat Mar 25, 2006 9:16 am
Posts: 150
Woops... I tried just tried your suggestion and (A) it did not compile and (B) when I got it to compile, I got the exception "subcriteria cannot be ordered". So it looks like your idea actually doesn't work. By the way, I had to change it to look like this:

Code:
session.CreateCriteria(typeof(Foo))
    .CreateCriteria("Bar")
        .AddOrder(Order.Asc("BarName"))
    .List();


There isn't any way to pass a type into the second CreateCriteria call. You have to pass the "AssociationPath", which apparently is the name of the parameter referencing through the join.

In any case, I got the exception about subcriteria cannot be ordered. So I'm back to square one.


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.