-->
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.  [ 4 posts ] 
Author Message
 Post subject: Criteria and Subcriteria or in where clause
PostPosted: Wed Sep 28, 2005 11:31 am 
Regular
Regular

Joined: Fri Sep 09, 2005 11:35 am
Posts: 101
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:
3.05
Mapping documents:
Not Necessary
Code between sessionFactory.openSession() and session.close():
Criteria criteria = sess.createCriteria ( Permission.class);
criteria.add ( Restrictions.eq ( "user", user));
Criteria subCriteria = criteria.createCriteria ( "accountGroup").createCriteria ( "groupedAccounts").createCriteria ( "account").createCriteria ( "cashMovements");
subCriteria.add ( Restrictions.eq ( "id", new Integer(1)));

Full stack trace of any exception that occurs:
No Exceptions.
Name and version of the database you are using:
Sybase ASE 12.5
The generated SQL (show_sql=true):
select ...... where this_.USER_ID=2 and cashmoveme4_.ID=1;
Debug level Hibernate log excerpt:
Not needed.

I could not find a way to get the "and" to change to an "or".
I went through the docs and then looked into the code also and found this in class CriteriaQueryTranslator
<code>
public String getWhereCondition() {
StringBuffer condition = new StringBuffer(30);
Iterator criterionIterator = rootCriteria.iterateExpressionEntries();
while ( criterionIterator.hasNext() ) {
CriteriaImpl.CriterionEntry entry = (CriteriaImpl.CriterionEntry) criterionIterator.next();
String sqlString = entry.getCriterion().toSqlString( entry.getCriteria(), this );
condition.append(sqlString);
if ( criterionIterator.hasNext() ) condition.append(" and ");
}
return condition.toString();
}
</code>

Looks like if you add 2 or more Criterion to a Criteria they are always "anded".

Is there some way around this ?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 28, 2005 11:45 am 
Newbie

Joined: Sat Jul 23, 2005 8:31 am
Posts: 18
there is a or-restriction:

Code:
Criteria criteria = sess.createCriteria ( Permission.class);


Criteria subCriteria = criteria.createCriteria ( "accountGroup").createCriteria ( "groupedAccounts").createCriteria ( "account").createCriteria ( "cashMovements");

criteria.add ( Restrictions.or(
    Restrictions.eq ( "user", user),
    Restrictions.eq ( "id", new Integer(1)));


if you have more then 2 criterias use a Disjunction.[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 28, 2005 11:48 am 
Regular
Regular

Joined: Fri Sep 09, 2005 11:35 am
Posts: 101
This will not work this will add the restriction on the root criteria which does not have the "id" property.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 29, 2005 4:49 am 
Newbie

Joined: Sat Jul 23, 2005 8:31 am
Posts: 18
for createCriteria you can set an alias as second parameter
Code:
.createCriteria ( "cashMovements", "cm");


so you should be able to use the restriction like
Code:
Restrictions.eq ( "cm.id", new Integer(1))


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