-->
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.  [ 9 posts ] 
Author Message
 Post subject: How can add paranthesis in Criterion object or Criteria?
PostPosted: Wed Nov 19, 2008 10:31 am 
Newbie

Joined: Wed Nov 19, 2008 10:16 am
Posts: 7
hi everyone,

is there any way to append paranthesis "(" in criterion or criteria object. i am using logical operators

for example i need to achieve this scenario

title="hiber%" AND (priority=p0 OR priority=p1)


but now i am getting like this

title="hiber%" AND priority=p0 OR priority=p1


so is there anyway to append ( and ) in criterion .


help me out of this problem


thank u all

regards,
javasyed.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 19, 2008 10:51 am 
Newbie

Joined: Wed Nov 19, 2008 10:16 am
Posts: 7
anyone is there to help me out from this situation??

please help me


regards,
javasyed.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 19, 2008 3:42 pm 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
Seems to work for me on 3.2.6 when I add an OR restriction to my query (e.g. from docs):
Code:
List cats = sess.createCriteria(Cat.class)
    .add( Restrictions.like("name", "Fritz%") )
    .add( Restrictions.or(
        Restrictions.eq( "age", new Integer(0) ),
        Restrictions.isNull("age")
    ) )
    .list();


produces: name like "Fritz%" and (age = 0 or age is null)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 20, 2008 12:23 am 
Newbie

Joined: Wed Nov 19, 2008 10:16 am
Posts: 7
hi williams thaks for ur reply

u told that u r getting name like "Fritz%" and (age = 0 or age is null)

have u system.out(cats)?


but for me i am getting name like "Fritz%" and age = 0 or age is null when i system.out(cats);


there is no parenthesis included in that criteria. so i think that might give some wrong result.


any comments please reply


thanks


regards,
javasyed.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 20, 2008 1:26 am 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
I don't understand what you mean by "system.out(cats)". I'm looking at the actual SQL generated by hibernate, printed to the console when you set show_sql=true.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 20, 2008 1:46 am 
Newbie

Joined: Wed Nov 19, 2008 10:16 am
Posts: 7
sysout is just mentioned to print it in console... k its right u r using show_sql.


i have achieved that by some crappy coding





the code is:



if (null != values && values.length > 0) {
for (int index = 0; index < values.length; index++) {
if (null == criterion) {
if(values.length > 1) {
criterion = Expression.eq("("+key, values[index]);
} else {
criterion = Expression.eq(key, values[index]);
}
} else {
if(values.length > 1) {
criterion = Expression.or(criterion, Expression.eq(key, (index + 1 == values.length)
? values[index]+ ")": values[index]));
} else {
criterion = Expression.or(criterion, Expression.eq(key, values[index]));
}
}
}
}

thank u

regards,
javasyed


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 20, 2008 1:52 am 
Newbie

Joined: Wed Nov 19, 2008 10:16 am
Posts: 7
org.hibernate.QueryException: could not resolve property

is comming..... in the aboe code i included '(' in property name..


still headache is continuing...


shall i send u the code... can u look into that and tell me how can i achieve that...


thanks



regards,

javasyed


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 20, 2008 2:03 am 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
You can't include brackets with the property name, they will be considered as part of the name and cause the exception you're seeing.

What version of hibernate are you using?

The OR criteria in 3.2.6 works correctly. I haven't checked other versions but it would be a pretty serious bug it it existed - likely to get fixed quickly.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 20, 2008 2:09 am 
Newbie

Joined: Wed Nov 19, 2008 10:16 am
Posts: 7
This is my code williams

if (null != logical_OR_ParamValues) {
Criterion logicalOperatorCriterion = null;
for (Iterator iter = logical_OR_ParamValues.keySet().iterator(); iter.hasNext();) {
String key = (String) iter.next();
String[] values = (String[]) logical_OR_ParamValues.get(key);
Criterion criterion = null;

if (null != values && values.length > 0) {
for (int index = 0; index < values.length; index++) {
if (null == criterion) {
if(values.length > 1) {
criterion = Expression.sql("("+key+"="+values[index]);
} else {
criterion = Expression.eq(key, values[index]);
}
} else {
if(values.length > 1) {
criterion = Expression.or(criterion, Expression.eq(key, (index + 1 == values.length)
? values[index]+ ")": values[index]));
} else {
criterion = Expression.or(criterion, Expression.eq(key, values[index]));
}
}
}
}
if(!StringUtil.isEmptyString(logicalOperator)) {
if(null == logicalOperatorCriterion) {
logicalOperatorCriterion = criterion;
} else {
if(logicalOperator.equalsIgnoreCase(Constants.OR)) {
logicalOperatorCriterion = Expression
.or(logicalOperatorCriterion, criterion);
}
if(logicalOperator.equalsIgnoreCase(Constants.AND)) {
logicalOperatorCriterion = Expression
.and(logicalOperatorCriterion, criterion);
}
}
} else {
qry.add(criterion);
}
}
if(null != logicalOperatorCriterion) {
qry.add(logicalOperatorCriterion);
}
}


if u give the input

title='title1','title2' i used to OR the values if the user gives multiple value for a single property which will be stored in criterion object.

suppose if u want to AND or OR different property that time

for example
title='title1','title2' and description='desc1','desc2'

this time i used to store this in logicalOperatorCriterion and finally wil add into criteria object


how can i achieve this


please help

thanks

regards,
javasyed


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