-->
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: Incorrect SQL generated from HQL
PostPosted: Fri Mar 23, 2007 2:04 pm 
Newbie

Joined: Tue Feb 28, 2006 5:03 pm
Posts: 15
Location: North Carolina
Hibernate version: 3.2.2

I have written a HQL query that retrieves a set of objects that are mapped from a parent object. The SQL that is generated from it is incorrect. I have discovered that the ORDER BY clause is causing the problem. I will include the HQL that generates the incorrect join in SQL and the HQL that generates the correct join in SQL (without an ORDER BY). The first query results in a query that returns each object 3 times. My question is am I defining the ORDER BY incorrectly or is there a bug in Hibernate?

HQL that generates a bad join
Code:
SELECT b.loans FROM Batch AS b WHERE b.id = :id ORDER BY b.loans.loanNumber DESC

Resulting SQL
Code:
    select
        loan2_.id as id2_,
        loan2_.name as name2_
    from
        dbo.TestBatch batch0_
    inner join
        TestMapping loans1_
            on batch0_.id=loans1_.batchId
    inner join
        dbo.TestLoan loan2_
            on loans1_.loanId=loan2_.id,
        TestMapping loans3_,
        dbo.TestLoan loan4_
    where
        batch0_.id=loans3_.batchId
        and loans3_.loanId=loan4_.id
        and batch0_.id=?
    order by
        loan4_.name DESC


If I just remove the ORDER BY clause the Resulting SQL includes a valid join
Code:
    select
        loan2_.id as id2_,
        loan2_.name as name2_
    from
        dbo.TestBatch batch0_
    inner join
        TestMapping loans1_
            on batch0_.id=loans1_.batchId
    inner join
        dbo.TestLoan loan2_
            on loans1_.loanId=loan2_.id
    where
        batch0_.id=?


Mapping documents:
Code:
    <class name="testHibernate.Batch"
           table="TestBatch"
           schema="dbo"
           dynamic-insert="true"
           dynamic-update="true">
           
        <id name="id" type="java.lang.Integer" column="id" >
            <generator class="increment" />
        </id>
        <property name="name" type="java.lang.String" column="name" length="80" not-null="true" />

       
        <set name="loans" table="TestMapping">
            <key column="batchId"/>
            <many-to-many column="loanId" unique="true" class="testHibernate.Loan" lazy="proxy"/>
        </set>
    </class>

    <class name="testHibernate.Loan"
           table="TestLoan"
           schema="dbo"
           dynamic-insert="true"
           dynamic-update="true">
           
        <id name="id" type="java.lang.Integer" column="id" >
            <generator class="increment" />
        </id>
        <property name="loanNumber" type="java.lang.String" column="name" length="80" not-null="true" />
    </class>


By the way, if you think this problem looks familiar, I have already posted it. I don't really know how this forum works, so I thought I may have "flagged" my original post as "answered" when I replied to it to provide more information.


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.