You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-user@db.apache.org by Brendan Richards <br...@designuk.com> on 2004/02/03 16:32:56 UTC
Broker Critera Negation weirdness
Hi,
I've discovered some odd behavior exhibited by the broker criteria API
and I was wondering if anyone had come across this.
My system is dynamically building complex queries by building criteria
objects one at a time and then pulling them together with addAndCriteria
and addOr Criteria.
So my code looks something like this pseudocode:
Criteria criteria = new Criteria(); // create criteria object
while (myQuery.hasMoreElements()) {
Criteria c1 = new Criteria(); // new criteria
newCrit.addEqualTo("myProperty", myValue);
if (myNegation) {
c1.setNegative(true);
}
criteria.addAndCriteria(c1);
}
So this code loops through some objects that I've created and adds a
number of criteria to a central criteria object. Each criteria may be
negated.
My problem is that on the first call to addAndCriteria, a Criteria
object is not added to my main criteria, only a ValueCriteria.
So say I want to do:
WHERE (NOT forename=brendan) AND (NOT surname IS NULL)
My code would loop twice setting values and setNegative(true) for each
sub-criteria.
Looking at my final criteria object via getElements and printing the
types
Using this code (with Logger being a simple class that writes output to
a file):
Enumeration critElements = criteria.getElements();
while (critElements.hasMoreElements()) {
Object testCrit = critElements.nextElement();
Logger.debugLog("type "+testCrit.getClass().getName());
if (testCrit instanceof Criteria) {
Criteria tc = (Criteria) testCrit;
if (tc.isNegative()) {
Logger.debugLog("Criteria is negative");
}
}
}
I get the following result in my logfile:
type org.apache.ojb.broker.query.ValueCriteria
type org.apache.ojb.broker.query.Criteria
Criteria is negative
Using criteria.toString() I get:
[forename = brendan, [surname IS NULL ]]
What this means is that I have completely LOST the isNegative value for
the first criteria I add. The containing criteria object is not negative
(I don't want it to be either I want to control the negativity of each
criteria I add).
Why does criteria.addAndCriteria convert a Criteria to ValueCriteria
when adding the first criteria losing the negative switch?
What I'd expect to see from criteria.toString() after the above code is:
[[forername = brendan], [surname IS NULL ]] - with both elements being
instances of Criteria.
Subsequent calls to addAndCriteria() after the first add Criteria
objects as you would expect.
[ forname=brendan, [surname IS NULL], [email IS NULL] ]
There is a simple dodgy workaround I could do where the first criteria I
add always evaluates to TRUE but this is far from ideal.
Any Ideas?
Cheers,
Brendan.
---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org