Search code examples
hibernatehibernate-criteriarestriction

Hibernate Criteria Restrictions AND combination With in IN Clause


I am using Hibernate 4.3.8

How would I achieve this using Hibernate Restrictions?

SELECT * FROM t1 WHERE ( col_1, col_2 ) IN (( 'a', 'b' ), ( 'c', 'd' ));

Solution

  • If col_1 and col_2 are part of an embeddable, you can use it like select e from Entity e where e.embeddable in (:embeddable1, :embeddable2) and pass embeddables accordingly. Note though, that this version of Hibernate does not yet support emulating row value constructors, so it depends on the DBMS you are using, if this works. You can emulate this though by doing:

    select e
    from Entity e
    where e.col1 = 'a' and e.col2 = 'b'
       or e.col1 = 'c' and e.col2 = 'd'