-->
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.  [ 3 posts ] 
Author Message
 Post subject: How to order by optional attributes without restricting?
PostPosted: Thu Sep 09, 2010 11:02 am 
Newbie

Joined: Thu Sep 03, 2009 12:37 pm
Posts: 13
I like to order by optional attributes in a child relation:

Object A has a one-to-many relationship to Object B. Some A's have a relevant B (B.isRelevant, for example), some others don't. I like to sort them by B.name, but the following HQL removes all A without a relevant B:

Code:
from A join A.B b
  where B.isRelevant=true
  order by B.name


I want to have all A (just like "from A").

Can this be done with HQL (or criteria)? Or do I really have to sort all the A's manually (urghs..) after fetching?


Top
 Profile  
 
 Post subject: Re: How to order by optional attributes without restricting?
PostPosted: Thu Sep 09, 2010 5:05 pm 
Regular
Regular

Joined: Sun Feb 14, 2010 3:29 pm
Posts: 58
Location: USA
The default association is a INNER JOIN, but what you want is a LEFT JOIN, which will return your "optional" values.

try
Code:
from A left join A.B as b
  where b.isRelevant=true
  order by b.name

_________________
Zemian Deng
------------
Need a Java Scheduler? Try
http://bitbucket.org/timemachine/scheduler


Top
 Profile  
 
 Post subject: Re: How to order by optional attributes without restricting?
PostPosted: Thu Sep 09, 2010 6:47 pm 
Newbie

Joined: Thu Sep 03, 2009 12:37 pm
Posts: 13
I already tried this, but on the wrong part ;)

My case is a m:n relationship with A:N, N:M, M:B tables.I put the "left join" on the M:N object. Your posting encouraged me to try it again and I found the solution in putting the "left join" on the M:B relation ;)

Thank you.


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