Hibernate Criteria API

Discussion in 'Java' started by comp.lang.java.programmer, Mar 9, 2011.

  1. 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.
     
    comp.lang.java.programmer, Mar 9, 2011
    #1
    1. Advertising

  2. comp.lang.java.programmer

    David L Guest

    On 8 mar, 19:21, "comp.lang.java.programmer" <>
    wrote:
    > 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().

    --
    DLL
     
    David L, Mar 9, 2011
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ilias Lazaridis
    Replies:
    0
    Views:
    1,723
    Ilias Lazaridis
    Dec 27, 2004
  2. LuckyBoy
    Replies:
    0
    Views:
    597
    LuckyBoy
    Aug 25, 2007
  3. LuckyBoy
    Replies:
    0
    Views:
    385
    LuckyBoy
    Sep 4, 2007
  4. anisha

    hibernate criteria query

    anisha, Oct 7, 2008, in forum: Java
    Replies:
    0
    Views:
    419
    anisha
    Oct 7, 2008
  5. stunaz

    Hibernate Criteria Update

    stunaz, Mar 11, 2009, in forum: Java
    Replies:
    0
    Views:
    2,715
    stunaz
    Mar 11, 2009
Loading...

Share This Page