-->
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: Prbloem with simple criteria query
PostPosted: Thu Jul 12, 2007 6:24 am 
Newbie

Joined: Tue May 22, 2007 10:02 am
Posts: 7
Hi All

I have a query that looks like the below:

select * from table where column1 like 'xxx' or column2 b like 'xxx' or column1 like 'yyy' or column2 like 'yyy' or column3 like 'xxxyyy' or column4 like 'xxxyyy'



Below is how i am implementing it in nhibernate but it is not working:
This query reads from the query string. It splits it into words and searches word by word.
When i send one word only in the querystring the below works fine but when i send more than one it returns nothing.


ICriteria crit = session.CreateCriteria(typeof(Cat));

string[] q = search.Replace(" ", "|").Split('|');
for (int i = 0; i < q.Length; i++)
{
crit.Add(Expression.Or(Expression.Like("culumn1", "%" + q[i].ToString() + "%"), Expression.Like("column2", "%" + q[i].ToString() + "%")));
}
crit.Add(Expression.Or(Expression.Like("column3", search.ToString()), Expression.Like("column4", search.ToString())));

Thanks in advance


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 13, 2007 4:58 am 
Beginner
Beginner

Joined: Sat Jan 29, 2005 8:49 pm
Posts: 20
is this a typo? 'culumn1'

turn on show_sql and I guess it will show you that you generate sql like

Code:
select * from table where column1 like 'xxx' or column2 b like 'xxx' and column1 like 'yyy' or column2 like 'yyy' and column3 like 'xxxyyy' or column4 like 'xxxyyy'


try using

Code:
ICriteria crit = session.CreateCriteria(typeof(Cat));

string[] q = search.Split(' ');
Criterion c;
for (int i = 0; i < q.Length; i++)
{
Criterion c1 = Expression.or(c, Expression.Or(Expression.Like("culumn1", "%" + q[i].ToString() + "%"), Expression.Like("column2", "%" + q[i].ToString() + "%")));
c = c1;
}
Criterion c1 = Expression.or(Expression.Or(Expression.Like("column3", search.ToString()), Expression.Like("column4", search.ToString())));
c = c1;
crit.Add(c);


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.