I'm building Criteria within Criteria using following code:
My code looks like:
************************************************
Criteria hCriteriaUser = hbmSession.createCriteria(User.class);
//add Restrictions on User.name like "John"
Criteria hCriteriaHistory = hCriteriaUser.createCriteria("histories".LEFT_JOIN);
//add Restrictions on History, status="active"
List userList = hCriteriaUser.list();
*************************************************
User mapping file looks like:
************************************************
<class name="User" table="users" lazy="false">
.............................................
<set name="histories" lazy="true" cascade="save-update,lock" inverse="true">
<key column="USERID" not-null="true"/>
<one-to-many class="History"/>
</set>
.......................................
****************************************************
Issues:
We are using LEFT_JOIN in the above scenario, it's not retrieving all matching Users if there is no corresponding matching History. For example, if there 5 users with name "John" and for 3 users with matching History, I'm expecting userList to have all 5 users with HISTORY as NULL for the remaining 2 users.
Generated SQL:
*************************
select this_.ID as userID, hist.ID as histID from User this_
left outer join History hist on this_.ID=hist.userID
where this_.type='LOAN' and hist.Code='CLOSED';
*****************************
How to change above SQL to following SQL, which gives correct output:
**************************
select this_.ID as userID, hist.ID as histID from User this_
left outer join History hist on this_.ID=hist.userID and
hist.Code='CLOSED' where this_.type='LOAN';
**********************
I would appreciate any inputs on resolving this problem.
Thanks,R.
|