-->
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.  [ 7 posts ] 
Author Message
 Post subject: Criteria "or"ing in a loop
PostPosted: Mon Aug 18, 2008 8:52 am 
Newbie

Joined: Mon Aug 18, 2008 8:40 am
Posts: 6
Hi,

I couldn't find how to convert the java-sql code below into a hibernate findByCriteria method in a DAO class.


Code:
public String searchPatient(String patient){
  String sql = "select * from PATIENT where ";
  String names[] = patient.split(" ");
  for(int i = 0;i < names.length ;i++){
    sql += "upper(PATIENT_NAME) like '%" +  names[i].toUpperCase() + "%' OR upper(PATIENT_SURNAME) like '%" + names[i].toUpperCase() + "%'";
    if(i+1 !=names.length) sql += " OR ";
  }
  return sql;
}


uppercase etc. are not important...
Yes,there is "or"ing two criterias but how can I use it in a loop like this?

Thanks


Last edited by natnan on Mon Aug 18, 2008 10:09 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 18, 2008 9:32 am 
Expert
Expert

Joined: Thu Jul 05, 2007 9:38 am
Posts: 287
Code:
Criterion sofar = something;
for (whatever : stuff){
sofar = Restrictions.or(sofar, whatever);
}

_________________
Please rate useful posts.


Schauderhaft: Softwaredevelopment, Projectmanagement, Qualitymanagement and all things "schauderhaft"


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 18, 2008 10:07 am 
Newbie

Joined: Mon Aug 18, 2008 8:40 am
Posts: 6
I'm using DetachedCriteria in all findByCriteria methods(although I don't know why :)) and it seems eclipse won't accept that :

Code:
public List findByCriteria(Class clazz, String hekim) {
       DetachedCriteria criteria = DetachedCriteria.forEntityName(clazz.getName());
       String names[] = patient.split(" ");
       for(int i = 0;i < names.length;i++){
          criteria = Restrictions.or(criteria, Expression.like("Patient_Name", names[i]));
          criteria = Restrictions.or(criteria, Expression.like("patient_surname", names[i]));
       }
        List list = super.getHibernateTemplate().findByCriteria(criteria);
        return list;
}


Code:
The method or(Criterion, Criterion) in the type Restrictions is not applicable for the arguments (DetachedCriteria, SimpleExpression)


Should i change DetachedCriteria to Criterion?is it absolutely necessary?
How should I use Expression.like(asd,asd) in this "or" method?

Thanks for the quick reply btw.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 18, 2008 11:39 am 
Expert
Expert

Joined: Thu Jul 05, 2007 9:38 am
Posts: 287
You are mixing up Criteria and Criterion. Criteria is the Query, Criterion is a where clause (very roughly at least)

So you'd do something like this:

Code:
public List findByCriteria(Class clazz, String hekim) {
       DetachedCriteria criteria = DetachedCriteria.forEntityName(clazz.getName());
        Criterion criterion = null;
       String names[] = patient.split(" ");
       for(int i = 0;i < names.length;i++){
if (criterion == null) criterion = Expression.like("Patient_Name", names[i]);
else
          criterion = Restrictions.or(criterion, Expression.like("Patient_Name", names[i]));
          criterion = Restrictions.or(criterion, Expression.like("patient_surname", names[i]));
       }
        criteria.add(criterion);
        List list = super.getHibernateTemplate().findByCriteria(criteria);
        return list;
}

_________________
Please rate useful posts.


Schauderhaft: Softwaredevelopment, Projectmanagement, Qualitymanagement and all things "schauderhaft"


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 19, 2008 3:31 am 
Newbie

Joined: Mon Aug 18, 2008 8:40 am
Posts: 6
That did the "or" trick,thank you very much.

But still there is one problem:

How does hibernate ignore case sensitivity?I was using "upper(field_name),String.touppercase()".Should I use it now too?

I'm looking through the reference documentation but I couldn't find that part(if there is).I'm still looking,if i find the solution I'll take my question back.

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 19, 2008 4:51 am 
Expert
Expert

Joined: Thu Jul 05, 2007 9:38 am
Posts: 287
http://www.hibernate.org/hib_docs/v3/ap ... ang.Object)

_________________
Please rate useful posts.


Schauderhaft: Softwaredevelopment, Projectmanagement, Qualitymanagement and all things "schauderhaft"


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 19, 2008 4:56 am 
Newbie

Joined: Mon Aug 18, 2008 8:40 am
Posts: 6
wow! great.

thanks.


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