247 words

Help with JPA2 Criteria and conditional operator grouping

2011-07-13 13:51


  I'm getting a little trouble performing a query as per the way I had it set up on eclipselink. Essentially, the query I'm trying to generate is:

(($1.companyId=:p1) AND ($1.conditionOne=true)) AND
(($1.conditionTwo=false) OR ($1.conditionThree=false))


From the JPA2 criteria:

CriteriaBuilder builder = em.getCriteriaBuilder();
  CriteriaQuery<Company> c = builder.createQuery(Company.class);
  Root<Company> root = c.from(Company.class);
  ParameterExpression<String> nameParam = builder.parameter(String.class);
  Path<String> namePath = root.get("companyId");
  Path<Boolean> conditionOnePath = root.get("conditionOne");
  Path<Boolean> conditionTwoPath = root.get("conditionTwo");
  Path<Boolean> conditionThreePath = root.get("conditionThree");
    builder.and(builder.equal(namePath, nameParam),
               builder.equal(conditionOnePath, true)),
            builder.equal(conditionTwoPath, false),
            builder.equal(conditionThreePath, false)
  System.out.println("Query: " + c);

But I can can never get the grouping to occur around the 'or'. Hence, my logic conditions is failing. The same EJB running against my eclipselink persistence.xml shows the grouping happening. I've had to dumb down the domain model enough to convey the issue I'm getting, so the actual query will probably not make much sense. The generated objectdb query looks like this:

SELECT $1 FROM Company $1 WHERE ($1.companyId=:p1) AND ($1.conditionOne=true) AND ($1.conditionTwo=false) OR ($1.conditionThree=false)

Even though the the JPA2 criteria 'wraps' the conditionals around an AND/OR block, all the conditionals are being generated individually. Is this correct or have I missed something again?


Attached is the test case I'm using to generate this.




ObjectDB Version: 2.2.8_02



Willks's picture
Joined on 2011-04-04
User Post #25
2011-07-13 17:33

This is a bug. Thank you for the report and for the test case.

Please try build 2.2.8_04 that should fix the bug.

ObjectDB Support
ObjectDB - Fast Object Database for Java (JPA/JDO)
support's picture
Joined on 2010-05-03
User Post #430
2011-07-15 20:58

That fixed the conditional grouping issue. Thanks.


There is a new problem now however, I'll create a new post

Willks's picture
Joined on 2011-04-04
User Post #26

Post Reply

Please read carefully the posting instructions - before posting to the ObjectDB website.

  • You may have to disable pop up blocking in order to use the toolbar (e.g. in Chrome).
  • Use ctrl + right click to open the browser context menu in the editing area (e.g. for using a browser spell checker).
  • To insert formatted lines (e.g. Java code, stack trace) - select a style in the toolbar and then insert the text in the new created block.
  • Avoid overflow of published source code examples by breaking long lines.
  • You may mark in paragraph code words (e.g. class names) with the code style (can be applied by ctrl + D).
  • Long stack traces (> 50 lines) and complex source examples (> 100 lines) should be posted as attachments.
Maximum file size: 32 MB