Hibernate Criteria API

  • Thread starter comp.lang.java.programmer
  • Start date
C

comp.lang.java.programmer

Hi

I'm looking for a way to implement a query using Hibernate Criteria/Restrictions

The query would be along the lines of

(ConditionA) and (ConditionB and ConditionC and ConditionD)

But I couldn't see how the Criteria API allows logical combinations/grouping?
In other words
where (colA = 'aa') and (colb != 'bb' AND colC != 'cc' AND colD != 'dd')

Actually I couldn't even figure out how to do the second part because
criteria.add(Restrictions.ne("colB", "bb");
criteria.add(Restriction.ne("colC", "cc");
criteria.add(Restriction.ne("colD", dd");

seems to equate to
where colb != 'bb' AND colC != 'cc' AND cold != 'dd'

In other words there's no parenthesis around the three conditions
and so it returns all records because the three conditions must be applied together to eliminate one single record.

I thought maybe something like Restrict.allEq
might have an equivalent Restrict.allNe

Or that maybe the Example would have an inverse operation to return all records "not matching" the example.

Regards.
 
D

David L

I'm looking for a way to implement a query using Hibernate Criteria/Restrictions

The query would be along the lines of

(ConditionA) and (ConditionB and ConditionC and ConditionD)

But I couldn't see how the Criteria API allows logical combinations/grouping?

The boolean "and" operation is associative. That is, "(A and B) and
C" has exactly the same value as "A and (B and C)". I'm pretty sure
that Criteria.add() joins the new terms to the existing ones in a
conjuction; that is, as if the total criterion is "(old terms) and
(new term)".

Another way to construct an AND term is with
Restrictions.conjunction().

If you need to construct an OR term, use Restrictions.disjunction().
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top