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.  [ 5 posts ] 
Author Message
 Post subject: HQL to get latest entry of a multiple association
PostPosted: Tue Jan 27, 2009 11:35 am 
Beginner
Beginner

Joined: Wed Jul 14, 2004 11:51 am
Posts: 43
Hello,

I have a question concerning HQL. In my model I have:
- a Sale entity which has n SaleHistoryEntries.
- each SaleHistoryEntry is associated to a status and has a date.
- a Status has a code name (TREATED, IN_TRANSIT, COMPLETED, CANCELED, etc.)

For example:
Code:
Sale A => SaleHistoryEntry (2008-01-01) => TREATED
           => SaleHistoryEntry (2008-01-02) => IN_TRANSIT
           => SaleHistoryEntry (2008-01-03) => COMPLETED

Sale B => SaleHistoryEntry (2008-01-01) => TREATED
           => SaleHistoryEntry (2008-01-02) => CANCELED

Sale C => SaleHistoryEntry (2008-01-01) => TREATED
           => SaleHistoryEntry (2008-01-02) => IN_TRANSIT

Sale D => SaleHistoryEntry (2008-01-01) => TREATED
           => SaleHistoryEntry (2008-01-02) => COMPLETED



I would like to make a HQL request to get all the Sales that are in a certain state (that is that their latest SaleHistoryEntry has a particular Status.

For example (using the previous model example). I would like to fetch the Sales that are currently in the COMPLETED state. I should therefore receive A and D.

I have read the HQL documentation and have tried a few things but I don't know how to retrieve the latest of the SaleHistoryEntries of the sale.

Any idea or clue would be greatly appreciated. At the limit, I could put a currentStatus flag on the SaleHistoryEntry but I don't believe I should need that.

Thanks for any help,

Francois


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 27, 2009 7:20 pm 
Senior
Senior

Joined: Thu Jan 08, 2009 3:48 pm
Posts: 168
Code:
List<Sale> completedSales = (List<Sale>)session.createQuery("select s from sale s left join s.saleHistoryEntry she left join she.status st where status.code = :code")
.addString("code","COMPLETED")
.list();


something like that i guess should do the job


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 28, 2009 11:16 am 
Beginner
Beginner

Joined: Wed Jul 14, 2004 11:51 am
Posts: 43
Hi pkleindl,

Thank you for your answer but unfortunately your answer does not include the complexity of wanting the latest status.

With my example and your code, if I used the status: IN_TRANSIT, I would receive sales A and C when I would only want to receive sale C (the one that has a "current" status of IN_TRANSIT).

The complexity of this request is to request the latest saleHistoryEntry of a sale and then check the status.

Any other ideas?

Thanks,

Francois


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 28, 2009 12:12 pm 
Beginner
Beginner

Joined: Wed Jul 14, 2004 11:51 am
Posts: 43
I've been able to solve this using sql:

Code:
SELECT s2.ID, ss.CODE
  from SALE s2
  left outer join SALE_STATUS_HISTORY_ENTRY sshe2 on sshe2.SALE_STATUS_HISTORY_ENTRIES_FK = s2.ID
  left outer join SALE_STATUS ss on ss.ID = sshe2.SALE_STATUS_STATUS_FK,
  (SELECT s.ID as SALE_ID, max(sshe.date) as SSHE_MAX_DATE FROM sale s
    left outer join SALE_STATUS_HISTORY_ENTRY sshe on sshe.SALE_STATUS_HISTORY_ENTRIES_FK = s.ID
    group by s.ID
  ) as t
  where s2.ID = t.SALE_ID
  and sshe2.DATE = t.SSHE_MAX_DATE
  and ss.CODE = 'ORA'
;


Is there any way to do this using HQL?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 28, 2009 1:13 pm 
Senior
Senior

Joined: Thu Jan 08, 2009 3:48 pm
Posts: 168
Hm..

Try add
Code:
and she.date = (select max(she2.date) from SaleHistoryEntry she2 where she2.sale.id = s.id)


so you only take those records where the date matches the highest date of the SaleHistoryEntries for the same sale

Not sure if it works that way in HQL and I didn't try out the code, hope you get the idea

Rating appreciated if it works out :o)


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