-->
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: Transforming SQL query into HQL and self join
PostPosted: Thu Mar 22, 2007 7:16 am 
Newbie

Joined: Tue Dec 12, 2006 5:01 am
Posts: 11
Hibernate version: 3.2.1

Hi,

I have following SQL query set up as view:

Code:
select ACCOUNT_ASSET.* from ACCOUNT_ASSET
right join (
    select ACCOUNT_ASSET.ACCOUNT_ID, max(ACCOUNT_ASSET.REPORT_DATE) as REPORT_DATE
    from ACCOUNT_ASSET
    group by ACCOUNT_ASSET.ACCOUNT_ID
) as LATEST_UPDATE
on ACCOUNT_ASSET.ACCOUNT_ID = LATEST_UPDATE.ACCOUNT_ID and ACCOUNT_ASSET.REPORT_DATE = LATEST_UPDATE.REPORT_DATE;


As you can see I do a self join to get the latest update on the table.

Now I need an additional parameter to get the latest update up to a specific reference date:

Code:
select ACCOUNT_ASSET.* from ACCOUNT_ASSET
right join (
    select ACCOUNT_ASSET.ACCOUNT_ID, max(ACCOUNT_ASSET.REPORT_DATE) as REPORT_DATE
    from ACCOUNT_ASSET
    [b]where REPORT_DATE <= [color=red]?[/color][/b]
    group by ACCOUNT_ASSET.ACCOUNT_ID
) as LATEST_UPDATE
on ACCOUNT_ASSET.ACCOUNT_ID = LATEST_UPDATE.ACCOUNT_ID and ACCOUNT_ASSET.REPORT_DATE = LATEST_UPDATE.REPORT_DATE;


But I already fail with converting the first SQL query into HQL:

Code:
from AccountAsset asset
right join (
  select (asset2.accountId, max(asset2.reportDate) as reportDate)
  from AccountAsset asset2
  group by asset2.accountId) as latestUpdate
with asset.reportDate = latestUpdate.reportDate and asset.accountId = latestUpdate.accountId


It complains about the first paranthesis directly after join. So how to actually do a self join (join on the same table)?

Thanks in advance,
Joerg


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 04, 2009 12:50 pm 
Newbie

Joined: Thu Jan 29, 2009 3:57 pm
Posts: 5
Hi,

I know its been long but did you find a solution to your problem. I am having a similar query and am unable to use HQL, or Criteria or DetachedCriteria.

Would appreciate if you could post your solution.

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 12, 2009 8:10 pm 
Newbie

Joined: Thu Jun 14, 2007 6:12 pm
Posts: 3
I found this to be immensely helpful.

http://forum.hibernate.org/viewtopic.php?t=986953


Top
 Profile  
 
 Post subject: Thank you
PostPosted: Fri Feb 13, 2009 1:24 am 
Newbie

Joined: Thu Jan 29, 2009 3:57 pm
Posts: 5
Thank you for your reply.

I eventually found the 'where in' clause in hibernate documentation and was able to resolve my query.

Thanks so much for replying though


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.