-->
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.  [ 5 posts ] 
Author Message
 Post subject: Criteria results
PostPosted: Wed Oct 25, 2006 6:49 am 
Newbie

Joined: Mon Sep 04, 2006 1:27 pm
Posts: 9
I 'm testing :
..........

if (a!=null){
criteria.add(Restrictions.eq("a",a);
}
if (b!=null){
criteria.add(Restrictions.eq("b",b);
}
if (c!=null){
criteria.add(Restrictions.eq("c",c);
}
But the result it's very strict ;becouse only return an existent object only if you fill all fields of the object.
eg if I let in blank the form in the fields b,c just resturn an existent object with b,c null.
How can I do in the restrictions for several conditions??
I want make search of several conditions ,a could be null,or b ,If I put a and c I want to get a list of every object with a and c properties.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 25, 2006 8:25 am 
Regular
Regular

Joined: Tue May 16, 2006 3:32 am
Posts: 117
Check out Example Queries, Hibernate reference doc 15.6.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 26, 2006 6:58 am 
Newbie

Joined: Mon Sep 04, 2006 1:27 pm
Posts: 9
Jayehs,I tried with :
x po=new x();
Criteria crit=hibernateTemplate.createCriteria(x.class);
crit.add((Example.create(po)).excludeNone());
if (a!=null){
criteria.add(Restrictions.eq("a",a);
}
if (b!=null){
criteria.add(Restrictions.eq("b",b);
}
if (c!=null){
criteria.add(Restrictions.eq("c",c);
}

but now I get an empty list ,could you please put an example of using criteria with several conditions and how to use the restrictions under Example class.
Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 26, 2006 9:03 am 
Regular
Regular

Joined: Tue May 16, 2006 3:32 am
Posts: 117
I was thinking more along the following lines:

Code:
x po=new x();

po.setA(a); // 'a' maybe null or with some valid value
po.setB(b);
po.setC(c);

Criteria crit=hibernateTemplate.createCriteria(x.class);
crit.add(Example.create(po));


By default null values set in po will not be considered as part of the criteria.

So e.g. if the values for a, b, c are null, b, c respectively.

Then only x(s) which match b and c would be returned. Since 'a' is null (could be equivalent to not filling in) it would be ignored and not included as part of the criteria.


The result would be similar to your code with null checks.

Quote:
But the result it's very strict ;becouse only return an existent object only if you fill all fields of the object. eg if I let in blank the form in the fields b,c just resturn an existent object with b,c null.


I do not agree with your above comments. It seems to me that your code (first post) should also have worked just fine. If a particular property is null then it is not added to the criteria. So the query should not include the property as part of the search.

It could be that when you do not fill in the field it may not be null but for e.g. an empty string "" or " ".

Check the query that is being generated and the value for the fields being left empty.


Top
 Profile  
 
 Post subject: thank Jayeshj
PostPosted: Fri Oct 27, 2006 8:18 am 
Newbie

Joined: Mon Sep 04, 2006 1:27 pm
Posts: 9
thanks Jayeshj the problem was:
"It could be that when you do not fill in the field it may not be null but for e.g. an empty string "" or " ". "
like you well say the field not filled had an empty string "".
I put in the logic also !(a.length()<1);
now work perfectly.
Thank you very much for your help.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.