-->
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.  [ 1 post ] 
Author Message
 Post subject: Using MAX in the subquery of a LEFT JOIN
PostPosted: Wed Jun 09, 2010 5:40 pm 
Newbie

Joined: Wed Jun 09, 2010 5:29 pm
Posts: 1
I have a one to many relation between table A and table B, and I need to get the row with latest date <= now() from table B from within a LEFT JOIN's subquery. The hql query that I am using is returning all the dates and ignore my subquery in the WHERE clause, but when i translate the same query into sql and run it returns the correct result. Could someone help .. I tried everything I could think of....

Here are the Mappings:

<class name="Store" table="merchants" schema="public">
<id name="id" type="int" column="merchant_id">
<generator class="sequence">
<param name="sequence">merchants_seq</param>
</generator>
</id>
<set name="currRebate" table="merchant_rebates" schema="rebate" lazy="false" cascade="none" inverse="true">
<key column="mr_merchant_id"/>
<one-to-many class="MerchantRebate"/>
</set>
</class>

<class name="MerchantRebate" table="merchant_rebates" schema="rebate">
<id name="id" type="int" column="mr_id">
<generator class="sequence">
<param name="sequence">merchant_rebates_seq</param>
</generator>
</id>
<property name="merchantId" column="mr_merchant_id" type="int"/>
<property name="startDate" column="mr_start_date" type="timestamp"/>
</class>

And here is the query I came up with:


<query name="getStoreById">
<![CDATA[
SELECT p FROM Store as p LEFT JOIN p.currRebate AS curRebate
WHERE p.id = id AND curRebate.startDate = (SELECT MAX(mr.startDate) FROM MerchantRebate AS mr
WHERE mr.startDate <= now())
]]>
</query>


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.