I am trying to use the disjunction operator on a query.
Here is the code within the try block
//---------------------------//
Criteria crit = session.createCriteria(SecurityUserDO.class);
//while we have some criteria, add it to the criteria clause
while (simpleSearchString.indexOf(" ") != -1)
{
String tempCriteria = simpleSearchString.substring(0, simpleSearchString.indexOf(" ")).trim();
simpleSearchString = simpleSearchString.substring(simpleSearchString.indexOf(" ")+1, simpleSearchString.length()).trim();
crit.add(Expression.disjunction().add(Expression.like("simpleSearchString",tempCriteria,MatchMode.ANYWHERE)));
}
crit.add(Expression.disjunction().add(Expression.like("simpleSearchString",simpleSearchString,MatchMode.ANYWHERE)));
return crit.list();
//---------------------------//
I have a string with spaces, each word in the string is a possible search criteria.
For each word, I want to see if it's in the simpleSearch field in the DB ( a list of keywords)
I would like to use the OR logic and I am using the disjunction function.. here is the Hibernate sql.show
//--------------------------------//
Hibernate: select this.user_id as user_id0_, this.user_name as user_name0_, this.user_password as user_pas3_0_, this.first_name as first_name0_, this.last_name as last_name0_, this.email as email0_, this.inactive as inactive0_, this.system as system0_, this.simple_search as simple_s9_0_, this.created_by as created_by0_, this.created_stamp as created11_0_, this.updated_by as updated_by0_, this.updated_stamp as updated13_0_ from security_user this where (this.simple_search like ?) and (this.simple_search like ?) and (this.simple_search like ?) and (this.simple_search like ?) and (this.simple_search like ?) and (this.simple_search like ?)
//-------------------------------//
and her is my debug output
//--------------------------------//
14:05:23,921 DEBUG BatcherImpl:253 - preparing statement
14:05:23,921 DEBUG StringType:46 - binding '%this%' to parameter: 1
14:05:23,921 DEBUG StringType:46 - binding '%is%' to parameter: 2
14:05:23,921 DEBUG StringType:46 - binding '%a%' to parameter: 3
14:05:23,981 DEBUG StringType:46 - binding '%test%' to parameter: 4
14:05:23,981 DEBUG StringType:46 - binding '%for%' to parameter: 5
14:05:23,981 DEBUG StringType:46 - binding '%user%' to parameter: 6
14:05:24,011 DEBUG Loader:281 - processing result set
14:05:24,011 DEBUG Loader:298 - done processing result set (0 rows)
//----------------------------------//
The main goal: to find the records that contain any of the above keywords in the field called "searchstring"
but it's doing an AND operator instead of OR. can anyone point out why this is occuring?.
I've gone through Chpt 12 in the API, and page 256 in the Hibernate in action book.
Thanks in avance.
|