-->
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: Restriction.or Restriction.and used in a loop
PostPosted: Thu Apr 07, 2005 2:49 pm 
Beginner
Beginner

Joined: Wed Nov 24, 2004 10:54 am
Posts: 48
Hibernate version:3

In my current app, I have a loop that builds a where clause in HQL. Here it is.


Code:
         for (int i = 0;i<hmembpList.size();i++){
            HQL_STMT = HQL_STMT + " tdocumtBean.xdact =? AND tdocumtBean.xdpat = ? OR ";
            HmembpBean hmembpBean = (HmembpBean) hmembpList.get(i);
            findObject[x] = hmembpBean.getAcct();
            x++;
            findObject[x] = hmembpBean.getPat();
            x++;
            findType[y] = Hibernate.INTEGER;
            y++;
            findType[y] = Hibernate.INTEGER;
            y++;
         }


Now, I am trying to move this code from Hibernate 2.1.7 useing a session.find to Hibernate 3 using a session.createCriteria. As you can see, I have both "and" and "or" (disjunction) in this HQL. How would I build this using the createCreiteria dynamically? I do not know how many Restrictions I may have. From the looks of the doc, I need to know this in order to code it correctly. Am I missing something here?


Top
 Profile  
 
 Post subject: Restriction.or Restriction.and used in a loop
PostPosted: Wed Jul 27, 2005 8:13 pm 
Newbie

Joined: Wed Jul 27, 2005 8:04 pm
Posts: 1
Location: San Francisco
The solution I came up with was to create a new Criterion within the loop, add it to somekind of datastructure, then reassemble a new Criterion using the Expression.or method. Code snippet below:

ArrayList expressions = new ArrayList();
for loop{
Criterion c = Expression.like("example", example);
expressions.add(c);
} // for

Iterator ci = expressions.iterator();
Criterion searchAllCriteria = null;
while(ci.hasNext()) {
if(searchAllCriteria == null)
searchAllCriteria = Expression.or((Criterion)ci.next(), (Criterion)ci.next());
else
searchAllCriteria = Expression.or(searchAllCriteria, (Criterion)ci.next());
} // while

criteria.add(searchAllCriteria);

Hope this helps,
Catherine


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.