-->
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 join with subselect?
PostPosted: Fri Apr 21, 2006 4:42 pm 
Newbie

Joined: Fri Apr 21, 2006 4:33 pm
Posts: 2
I'm having trouble creating an HQL query:

Say I have a Person, who has a collection of ParkingTicket(s), each with a postedDate.

I need to return a list of distinct Person(s) along with their most recent ticket, where the postedDate is between some startDate and endDate, sorted by the postedDate of the most recent ticket. Actually, I don't care if I get all the tickets, but I need to do some HQL operations on the most recent ticket.

Here's something to get you started:

Code:
select distinct person from Person person inner join person.parkingTickets as parkingTicket where parkingTicket.postedDate >= startDate and parkingTicket.postedDate <= endDate


This is great, but no sorting. Let's try:

Code:
select distinct person from Person person inner join person.parkingTickets as parkingTicket where parkingTicket.postedDate >= startDate and parkingTicket.postedDate <= endDate order by parkingTicket.postedDate


The problem with this is that it requires parkingTicket in the select clause, which then causes problems because the distinct now doesn't work as intended. I'm sure there is a correct approach (maybe involving a subselect to only join to the most recent parkingTicket) but i can't quite figure it out.

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 21, 2006 6:14 pm 
Regular
Regular

Joined: Wed Jul 07, 2004 2:00 pm
Posts: 64
Here's one that works for me:
select distinct p from Person p, ParkingTicket pt where pt in elements(p.tickets) and pt.postedDate between :startDate and :endDate order by pt.postedDate


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 21, 2006 6:17 pm 
Newbie

Joined: Fri Apr 21, 2006 4:33 pm
Posts: 2
Thanks...I'll try it with my real world code and see how it works.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 21, 2006 7:20 pm 
Expert
Expert

Joined: Thu Sep 22, 2005 10:29 am
Posts: 285
Location: Almassera/Valencia/Spain/EU/Earth/Solar system/Milky Way/Local Group/Virgo Supercluster
This is from memory, so I don't know if it works

Code:
select
   p,
   pt
from
   Person p
   inner join p.parkingTickets as pt
   with
      startDate <= pt.postedDate and pt.postedDate <= endDate
group by
   p
having
   pt.postedDate = max(pt.postedDate)


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.