-->
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.  [ 3 posts ] 
Author Message
 Post subject: same HQL gives different SQL + results in Hibernate 3.3/3.6
PostPosted: Sun Mar 27, 2011 9:50 pm 
Newbie

Joined: Sun Mar 27, 2011 9:31 pm
Posts: 2
Location: Christchurch, New Zealand
Hi,

We have just upgraded from Hibernate 3.3 to Hibernate 3.6 (actually as part of upgrading JBoss AS but I don't think this matters in this case), and at least one of our HQL queries are now generating different SQL - which is unfortunately returning different results. I've not had much luck trying to figure out what the problem is, or if this is expected and perhaps we have a problem with our HQL or mapping.

The relevant (to me, please let me know if I've left something out that may be significant) two entities are mapped as a unidirectional one-to-one relationship (using a <many-to-one> element), but I'm including the mapping for TradeApprovals as a reference only, we set the Trade id in it manually.

This is the Trade side mapping:

Code:
<hibernate-mapping>
    <class name="foo.bar.Trade"
           table="TRADES"
           lazy="true"
           batch-size="128">

        <many-to-one lazy="false"
                     name="latestApproval"
                     class="foo.bar.TradeApproval"
                     cascade="none"
                     column="LATESTAPPROVALID"
                     outer-join="true"
                     fetch="select"/>

        <!-- all other stuff removed for clarity -->
    </class>
</hibernate-mapping>


And the TradeApprovals side:

Code:
<hibernate-mapping>
    <class name="foo.bar.TradeApproval"
           table="TRADEAPPROVALS">

        <property name="tradeId"
                  type="long"
                  update="true"
                  insert="true"
                  column="TRADEID"
                  not-null="true"/>

        <!-- all other stuff removed for clarity -->
    </class>
</hibernate-mapping>


The HQL query we run is:

Code:
from com.kiodex.model.trade.Trade t left join fetch t.latestApproval where (t.latestApproval is null or t.latestApproval.status <> 7) and t.trader.organization.id = :organizationId and t.deleted = :deleted


In Hibernate 3.3 the HQL query above causes this SQL query to be run:

Code:
select ...
from TRADES trade0_
left outer join TRADEAPPROVALS tradeappro1_ on trade0_.LATESTAPPROVALID=tradeappro1_.ID, PRINCIPALS user3_
where trade0_.TRADERID=user3_.ID
and (trade0_.LATESTAPPROVALID is null or tradeappro1_.STATUS<>7)
and user3_.ORGANIZATIONID=?
and trade0_.TRADEDATE>=?
and trade0_.DELETED=?


Unfortunately, using Hibernate 3.6 this SQL is generated:

Code:
select ...
from TRADES trade0_
left outer join TRADEAPPROVALS tradeappro1_ on trade0_.LATESTAPPROVALID=tradeappro1_.ID, TRADEAPPROVALS tradeappro2_, PRINCIPALS user3_
where trade0_.LATESTAPPROVALID=tradeappro2_.ID
and trade0_.TRADERID=user3_.ID
and (trade0_.LATESTAPPROVALID is null or tradeappro2_.STATUS<>7)
and user3_.ORGANIZATIONID=?
and trade0_.TRADEDATE>=?
and trade0_.DELETED=?


As you can see there is now a second reference to the TRADEAPPROVALS table, and the extra "trade0_.LATESTAPPROVALID=tradeappro2_.ID" has been added to the "where" clause, effectively turning this "left outer join" statement into an "inner join" statement.

Does anybody have any thoughts on if this is a new Hibernate bug, or if it is expected considering my mapping and HQL used, and in that case how I can change them to ensure that I get back to the old intent and results?

Regards,


Top
 Profile  
 
 Post subject: Re: same HQL gives different SQL + results in Hibernate 3.3/3.6
PostPosted: Mon Mar 28, 2011 5:58 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
The problem is that you have multiple references to 't.latestApproval' that appears both in a join clause and in the where clause. There was a change in Hibernate 3.5 (see http://opensource.atlassian.com/project ... e/HHH-4091) that affects this type of queries. To make the query work as before you need to assign an alias when you join the associated entity and then use the alias in the where clause. Something like this:

Code:
... left join fetch t.latestApproval la where (la is null or la.status <> 7) ....


Top
 Profile  
 
 Post subject: Re: same HQL gives different SQL + results in Hibernate 3.3/3.6
PostPosted: Mon Mar 28, 2011 7:05 pm 
Newbie

Joined: Sun Mar 27, 2011 9:31 pm
Posts: 2
Location: Christchurch, New Zealand
nordborg, thank you so much, it works! :)

If I ever move back to Sweden I owe you a beer.


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