-->
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.  [ 7 posts ] 
Author Message
 Post subject: Runtime additional filters for outer joins
PostPosted: Mon Dec 05, 2005 6:19 pm 
Expert
Expert

Joined: Fri Oct 28, 2005 5:38 pm
Posts: 390
Location: Cedarburg, WI
A common query scenario that we have is something like this: "Select all contacts and their primary phone". For contacts with no phone (or no phone marked primary), we still want to get the contact back.

Before ANSI SQL, you had to do something like this:

SELECT c.Name, cp.Phone
FROM Contact AS c, ContactPhone as cp
WHERE cp.ContactId *= c.ContactId
AND ISNULL(cp.IsPrimary, 1) = 1

With ANSI SQL, this cleaned up to

SELECT c.Name, cp.Phone
FROM Contact AS c
LEFT OUTER JOIN ContactPhone AS cp
ON cp.ContactId = c.ContactId AND cp.IsPrimary = 1

Now, in HQL, since there's no "ON" clause for joins, it looks like we have to resort to using ISNULL again, or create an entity subclass for "primary phone". We will in fact create an entity subclass for this case, but there will be other cases conceived by the user at runtime.

Maybe I'm not understanding this right, but in the effort to eliminate "ON" clauses from HQL joins, it looks like some elegance was lost :?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 06, 2005 8:55 am 
Contributor
Contributor

Joined: Thu May 12, 2005 9:45 am
Posts: 593
Location: nhibernate.org
Try:
Code:
SELECT cp.Contact.Name, cp.Phone
FROM ContactPhone as cp
WHERE cp.IsPrimary = 1

_________________
Pierre Henri Kuaté.
Get NHibernate in Action Now!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 07, 2005 1:33 pm 
Expert
Expert

Joined: Fri Oct 28, 2005 5:38 pm
Posts: 390
Location: Cedarburg, WI
That would only return contacts with primary phones. We want to return all contacts, whether they have a primary phone or not. Can HQL do this without using IsNull(), and without defining a new entity mapping for "primary phone"?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 08, 2005 11:57 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
No, you can't do this easily with HQL currently, try using a native SQL query.


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

Joined: Thu May 12, 2005 9:45 am
Posts: 593
Location: nhibernate.org
Or you may run two queries...

_________________
Pierre Henri Kuaté.
Get NHibernate in Action Now!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 08, 2005 4:19 pm 
Expert
Expert

Joined: Fri Oct 28, 2005 5:38 pm
Posts: 390
Location: Cedarburg, WI
Resort to a native SQL query? :o :(

Using an HQL query with IsNull() would still seem to be much more preferable than resorting to a native SQL query. Native SQL queries through an NHibernate session can't return entities (or can they?)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 08, 2005 9:17 pm 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
Yes, they can. See http://www.hibernate.org/hib_docs/reference/en/html/querysql.html.


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