-->
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: Criteria Where Clause - Help Please!
PostPosted: Thu Oct 04, 2007 5:07 pm 
Newbie

Joined: Sun Sep 30, 2007 6:50 pm
Posts: 8
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.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 09, 2007 6:54 am 
Regular
Regular

Joined: Mon Jan 22, 2007 10:32 am
Posts: 101
I am not sure how to get the sql mentioned by you but one to get the desired results will be to add an additional isNull restriction on history status.

Something like:

Criterion exp1 = Restrictions.isNull("status");
Criterion exp2 = Restrictions.eq("status","CLOSED");
Criterion finalCriterion = Restrictions.or(exp1,exp2);

add finalCriterion to criteria and the list().


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.