-->
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.  [ 4 posts ] 
Author Message
 Post subject: HQL: joining to / filtering by collection elements?
PostPosted: Tue Nov 01, 2005 7:23 pm 
Expert
Expert

Joined: Fri Oct 28, 2005 5:38 pm
Posts: 390
Location: Cedarburg, WI
Given an Employee class, with a collection property Phones, is it possible to do queries like the folllowing in HQL?

1. Select employees and their primary phone

??? ideally something like

SELECT e.FullName, p.PhoneNumber
FROM Employee AS e
JOIN e.Phones[?] AS p ON p.IsPrimary = true

2. Select employees and their "first" cell phone

??? ideally something like

SELECT e.FullName, p.PhoneNumber
FROM Employee AS e
JOIN e.Phones[?] AS p ON p.PhoneType = 'CELL' HAVING MIN(p.Index)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 01, 2005 8:24 pm 
Expert
Expert

Joined: Fri Oct 28, 2005 5:38 pm
Posts: 390
Location: Cedarburg, WI
OK, with a little experimenting, the 1st case can be done as

SELECT e.FullName, p.PhoneNumber
FROM Employee AS e
LEFT JOIN e.Phones AS p
WHERE p.IsPrimary = true

It's a little odd that you join to the collection when you really mean one of its members, but it works. Too bad you can't leave out the verbose JOIN and just do

SELECT e.FullName, e.Phones.PhoneNumber ...

I still have no idea how to do the 2nd case. Can someone point me to examples of using HQL's "collection expressions", i.e.
{ some, exists, all, any }
I picked up the book Pro Hibernate 3, which mentions them, but no details or examples are given :(


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 02, 2005 4:46 am 
Senior
Senior

Joined: Thu Jun 02, 2005 5:03 pm
Posts: 135
Location: Paris
Maybe use a subselect with a TOP 1 for the Phone Id?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 03, 2005 12:52 pm 
Expert
Expert

Joined: Fri Oct 28, 2005 5:38 pm
Posts: 390
Location: Cedarburg, WI
I went to the (Java) Hibernate site and got their Hibernate 2.1.7 reference manual. That has the "official" documentation for HQL, and helped a lot. I assume NHibernate essentially supports whatever's in Hibernate 2.1.7 as far as HQL syntax goes ...


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