-->
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.  [ 2 posts ] 
Author Message
 Post subject: Where clause in classes and left joins
PostPosted: Mon Oct 02, 2006 5:43 pm 
Newbie

Joined: Tue Dec 27, 2005 8:10 am
Posts: 7
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 1.2.0 beta1

Mapping documents:
Code:
<class name="class1" .... table="table1" >
    ....
    <many-to-one name="class2" column="class2ID" /> <!--nullable-->
    ....
</class>

<class name="class2" table="table2" where="<condition_from_class2>">
    ....
</class>

Name and version of the database you are using: MS SQL Server 2000

The generated SQL (show_sql=true):
Code:
SELECT ... FROM table1 t1 LEFT JOIN t2 table2 on t1.class2ID = t2.Id WHERE <condition_from_class2>


Hi. I have a problem with the where clause in class mapping that is closely related to http://forum.hibernate.org/viewtopic.php?t=952606, http://forum.hibernate.org/viewtopic.php?t=956298 and in jira NH-514.

(the mapping class and SQL querys shown here are simplified for easier reading)

The problem is that if I specify a where clause in the class and then I indirectly query that class by a left join, NHibernate puts the where clause of the class in the where clause of the generated SQL, instead of putting it in the left join's ON clause.

That is, if I do:

Code:
FROM Table1 t1 LEFT JOIN FETCH t1.class2 where <somecondition>


in HQL, NHibernate generates:

Code:
SELECT ... FROM table1 t1 LEFT JOIN t2 table2 on t1.class2ID = t2.Id WHERE <condition_from_class2> and <somecondition>


which produces incorrect results if t1.class2ID is null.

The correct SQL should be:
Code:
SELECT ... FROM table1 t1 LEFT JOIN t2 table2 on t1.class2ID = t2.Id and <condition_from_class2> WHERE <somecondition>


A workaround would be to check for null results in the where clause of the class (eg, if where="field > 3", one would instead write where="(field IS NULL or field > 3)"), but this is not exactly right.

Is this a bug, or is it the expected behavior?[/url]


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 20, 2006 8:28 pm 
Newbie

Joined: Fri Oct 20, 2006 8:22 pm
Posts: 1
What you want is the 'with' keyword used by hibernate:

Code:
FROM Table1 t1 LEFT JOIN FETCH t1.class2 with <somecondition>


Look at this example:

Code:
from Cat as cat
    left join cat.kittens as kitten
        with kitten.bodyWeight > 10.0


Taken from here:
http://www.hibernate.org/hib_docs/v3/re ... ryhql.html


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