db-torque-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Greg Monroe <Greg.Mon...@dukece.com>
Subject RE: Criteria.or not working?
Date Fri, 05 Feb 2010 15:16:46 GMT
Whoops, looked at the code and I think my answer was wrong.  The 
code is actually behaving as documented.  E.g. the docs say:

"This method adds a new criterion to the list of criterias. If a
 criterion for the requested column already exists, it is
 "OR"ed to the existing criterion...."

You are using two different columns in the or statements.  Therefore,
they are being treated as an add and not an or condition for each
column.

Not intuitive, but correctly documented. Sigh. Use the criterion 
method to create mix column or statements.

FWIW, the underlying cause is that criteria object was initially
created under Java 1.2 or 1.3 and all the Collections interfaces
where not defined.  So it is based on Hashtable.  This has caused 
some long term issues like this.  One of the goals of the 4.0 
version is to replace this with a HashList.  This will allow 
looking at the last added criterion and applying conditionals to 
it.  

> -----Original Message-----
> From: Greg Monroe
> Sent: Friday, February 05, 2010 9:35 AM
> To: 'Apache Torque Users List'
> Subject: RE: Criteria.or not working?
> 
> I think the proper syntax would be to "add" the left hand operator
> and then or the conditional with this.  Rather than 2 ors which
> are right hand conditionals. E.g., the proper syntax would be:
> 
> Criteria criteria = new Criteria();
> criteria.add("user.first_name", "John");
> criteria.or("user.last_name", "Doe");
> UserPeer.doSelect(criteria);
> 
> The syntax using two the .or methods is like trying to write an SQL
> statement like:
> 
> Select * from user where or user.first='John' or user.last='Doe';
> 
> The SQL parser would choke on this.  I suspect the code is assuming
> that since there is no right hand conditional the or should be treated
> as an and statement. E.g. "Null or X" is translated to just X.   It
> probably should throw an invalid syntax error here.
> 
> FWIW, using Criterion is really only "required" if you are using the
> same column or need parenthetical nesting.
> 
> 
> > -----Original Message-----
> > From: Ivano Luberti [mailto:luberti@archicoop.it]
> > Sent: Friday, February 05, 2010 9:08 AM
> > To: Apache Torque Users List
> > Subject: Re: Criteria.or not working?
> >
> > Sorry never tried that, I only now that is not the way the Criteria
> > class is meant to be used.
> > So I let developers talk about that.
> > Alternatively you can dig into the peer classes source code: I have
> done
> > a few times to understand Torque behaviour and is quite well written
> and
> > readable
> >
> > Ludwig Magnusson ha scritto:
> > > Yes that works, but that was not what I asked.
> > > My question was, how can the methods and(), add() and or() all
> generate
> > the
> > > same result?
> > >
> > > -----Original Message-----
> > > From: Ivano Luberti [mailto:luberti@archicoop.it]
> > > Sent: den 5 februari 2010 14:10
> > > To: Apache Torque Users List
> > > Subject: Re: Criteria.or not working?
> > >
> > > If you want to combine clauses mixinn and and or operators you should
> > > use Criterion.
> > >
> > > Look here for an introduction to how build queries using Criteria and
> > > Criterion
> > >
> > > http://db.apache.org/torque/releases/torque-
> 3.3/runtime/reference/read-
> > from-
> > > db.html
> > >
> > > Ludwig Magnusson ha scritto:
> > >
> > >> Hi!
> > >>
> > >> I have done some testing during development of a project here and it
> > seems
> > >> that criteria.and([parameters]), criteria.add([parameters]) and
> > >> criteria.or([parameters]) all generate the same query to the
> database.
> > >>
> > >>
> > >>
> > >> E.g these three code snippets:
> > >>
> > >> Criteria criteria = new Criteria();
> > >>
> > >> criteria.and("user.first_name", "John");
> > >>
> > >> criteria.and("user.last_name", "Doe");
> > >>
> > >> UserPeer.doSelect(criteria);
> > >>
> > >>
> > >>
> > >> Criteria criteria = new Criteria();
> > >>
> > >> criteria.add("user.first_name", "John");
> > >>
> > >> criteria.add("user.last_name", "Doe");
> > >>
> > >> UserPeer.doSelect(criteria);
> > >>
> > >>
> > >>
> > >> Criteria criteria = new Criteria();
> > >>
> > >> criteria.or("user.first_name", "John");
> > >>
> > >> criteria.or("user.last_name", "Doe");
> > >>
> > >> UserPeer.doSelect(criteria);
> > >>
> > >>
> > >>
> > >> . would all generate the Sql query
> > >>
> > >> SELECT * FROM USER WHERE first_name = "John" AND last_name = "Doe";
> > >>
> > >>
> > >>
> > >> How can this be?
> > >>
> > >> /Ludwig
> > >>
> > >>
> > >>
> > >>
> > >
> > >
> >
> > --
> > ==================================================
> > dott. Ivano Mario Luberti
> > Archimede Informatica societa' cooperativa a r. l.
> > Sede Operativa
> > Via Gereschi 36 - 56126- Pisa
> > tel.: +39-050- 580959
> > tel/fax: +39-050-9711344
> > web: www.archicoop.it
> > ==================================================
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: torque-user-unsubscribe@db.apache.org
> > For additional commands, e-mail: torque-user-help@db.apache.org
> 
> DukeCE Privacy Statement:
> Please be advised that this e-mail and any files transmitted with
> it are confidential communication or may otherwise be privileged or
> confidential and are intended solely for the individual or entity
> to whom they are addressed. If you are not the intended recipient
> you may not rely on the contents of this email or any attachments,
> and we ask that you please not read, copy or retransmit this
> communication, but reply to the sender and destroy the email, its
> contents, and all copies thereof immediately. Any unauthorized
> dissemination, distribution or copying of this communication is
> strictly prohibited.
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: torque-user-unsubscribe@db.apache.org
> For additional commands, e-mail: torque-user-help@db.apache.org

DukeCE Privacy Statement:
Please be advised that this e-mail and any files transmitted with
it are confidential communication or may otherwise be privileged or
confidential and are intended solely for the individual or entity
to whom they are addressed. If you are not the intended recipient
you may not rely on the contents of this email or any attachments,
and we ask that you please not read, copy or retransmit this
communication, but reply to the sender and destroy the email, its
contents, and all copies thereof immediately. Any unauthorized
dissemination, distribution or copying of this communication is
strictly prohibited.

---------------------------------------------------------------------
To unsubscribe, e-mail: torque-user-unsubscribe@db.apache.org
For additional commands, e-mail: torque-user-help@db.apache.org


Mime
View raw message