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.  [ 3 posts ] 
Author Message
 Post subject: Issue with disjunction
PostPosted: Thu Mar 17, 2005 6:39 pm 
Newbie

Joined: Fri Mar 11, 2005 7:22 pm
Posts: 10
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.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 22, 2005 2:43 pm 
Newbie

Joined: Fri Mar 11, 2005 7:22 pm
Posts: 10
bump for love.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 23, 2005 3:31 pm 
Newbie

Joined: Wed Feb 23, 2005 11:57 am
Posts: 15
I think you need to add the like expressions to the same disjunction.

Disjunction disjunction = Expression.disjunction();
disjunction.add(Restriction.like("simpleSearchString",tempCriteria,MatchMode.ANYWHERE));
disjunction.add.add(Restriction.like("simpleSearchString",simpleSearchString,MatchMode.ANYWHERE));
crit.add(disjunction);

Basically, you were doing (A) AND (B) instead of (A OR B)

I'm sort of a noob though so take it with a grain of salt.


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