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.