Search code examples
hibernatehibernate-criteria

IN operation in hibernate criteria


I have a collection with all the ids. I want to create a hibernate criteria where I pass the collection and get the results matching all the values in the list with a joined tables.

Similar to a WHERE IN operation in sql.

select * from Employee e, dept d where d.id in (1,2,3,4,5)

Cheers!!


Solution

  • Use Restriction for your purpose:

    Criteria c = ... // get Employee criteria here
    c.createAlias("dept", "d");
    c.add(Restrictions.in("d.id", ids));