-->
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: Query help - collections
PostPosted: Sun Aug 08, 2004 7:36 pm 
Newbie

Joined: Mon May 10, 2004 6:53 pm
Posts: 11
If I have the following:

eg.Manager
with a collection of Employees,
who each have a single ContactList property
which has a collection of PhoneNumbers
that have a Type property (work/home)

How do I query for all the work numbers for a particular manager's employees?

I'm not sure how to use the elements() function here, or where to join, and a pointer would be very much appreciated.

I believe the SQL would be like
SELECT p.*
FROM Managers m
LEFT OUTER JOIN Employees e on e.manager = m.id
LEFT OUTER JOIN PhoneNumbers p on p.contactlist = e.contactlist
WHERE p.type = 'work'

My actual working SQL is as follows, but I think the above is simpler to follow.

SELECT tn.formattedNumber
FROM Candidate c
LEFT OUTER JOIN CandidateProfileContactMethod cpcm on cpcm.candidate = c.id
LEFT OUTER JOIN ContactMethods cm on cm.id = cpcm.contactMethod
LEFT OUTER JOIN TelecomNumber tn on cm.telephone = tn.id
WHERE c.id =41 AND cm.telephone is not null;

_________________
--
Matthew Van Horn


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 09, 2004 3:54 am 
Expert
Expert

Joined: Fri Feb 06, 2004 7:49 am
Posts: 255
Location: Moscow, Russia
Quote:
I believe the SQL would be like
SELECT p.*
FROM Managers m
LEFT OUTER JOIN Employees e on e.manager = m.id
LEFT OUTER JOIN PhoneNumbers p on p.contactlist = e.contactlist
WHERE p.type = 'work'

In HQL it should look like:
Code:
select p from Manager m
    left outer join m.eployees e
    left outer join e.contactList c
    left outer join c.phoneNumbers p
where m.id = 124 and p.type = 'work'

_________________
Leonid Shlyapnikov


Top
 Profile  
 
 Post subject: Thanks
PostPosted: Mon Aug 09, 2004 8:31 am 
Newbie

Joined: Mon May 10, 2004 6:53 pm
Posts: 11
I managed to get something working through trial and error, if it could be more efficient/better, let me know.

Here is my HQL query:

select cm from Candidate as c
left join c.candidateProfiles cp
left join cp.personalData.contactMethods.elements cm
where cm.telephone is not null and c.id = ?

Candidate has a List of CandidateProfile objects
each CandidateProfile has a PersonalData component
PersonalData has a List of ContactMethod objects
each ContactMethod has a Telephone object property

This query gets what I wanted, which is a list of all the Candidate's ContactMethods which contain a non-null Telephone

As it works, I am happy. If it could be better, I would be happier to know more on the subject.

_________________
--
Matthew Van Horn


Top
 Profile  
 
 Post subject: Re: Thanks
PostPosted: Mon Aug 09, 2004 8:45 am 
Expert
Expert

Joined: Fri Feb 06, 2004 7:49 am
Posts: 255
Location: Moscow, Russia
mvanhorn wrote:
As it works, I am happy. If it could be better, I would be happier to know more on the subject.

I am not a database guru, but this HQL looks like it should generate rather optimal SQL select query. If you need "better" performance, set database indices on corresponding columns (ask your DBA to help you) and think about second-level caching and maybe HQL query caching.

_________________
Leonid Shlyapnikov


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.