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]