Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to achieve "not in" by using Restrictions and criteria in Hibernate?

I have list of category. I need a list of category by excluding 2,3 row. Can we achieve through hibernate by using Criteria and Restriction?

like image 920
Shashi Avatar asked Aug 03 '09 06:08

Shashi


People also ask

How do you use not in in criteria query hibernate?

Hope it helps. I got it to work with this : \n criteria. add(Expression. not(Expression.in("paraType", new String[]{"0", "4", "5", "6", "7", "a", "b", "c"}))); \n I will try your solution too !!

What is restriction in hibernate criteria?

The Criteria interface makes it easy to selectively fetch the data on the basis of conditions in the select query. The Restriction class in hibernate provide several methods that can be used as conditions (also known as Criterion). These conditions are added to a criteria object with the add() method.

How do you put restrictions in criteria query?

Restrictions with CriteriaCriteria cr = session. createCriteria(Employee. class); Criterion salary = Restrictions.gt("salary", 2000); Criterion name = Restrictions. ilike("firstNname","zara%"); // To get records matching with OR conditions LogicalExpression orExp = Restrictions.or(salary, name); cr.


1 Answers

Your question is somewhat unclear. Assuming "Category" is a root entity and "2,3" are ids (or values of some property of the category") you can exclude them using the following:

Criteria criteria = ...; // obtain criteria from somewhere, like session.createCriteria()  criteria.add(   Restrictions.not(      // replace "id" below with property name, depending on what you're filtering against     Restrictions.in("id", new long[] {2, 3})   ) ); 

Same can be done with DetachedCriteria.

like image 118
ChssPly76 Avatar answered Sep 29 '22 13:09

ChssPly76