Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to code the NOT IN query in Hibernate criteria?

This is the query that I am trying to write in Criteria:

    SELECT * FROM abc 
    WHERE NOT PartType IN ('0','4','5','6','7','a','b','c') 

The above is in iBatis.

So this is the hbm.xml for the table

  <class name="Parts" table="SomeDb..Parts">
        <id name="recordNumber" column="Recnum" />
        <property name="partNumber" column="Partnum" />
        <property name="sectionNumber" column="Secnum" />
        <property name="articleNumber" column="Articlenum"/>
        <property name="headerNumber" column="Headernum"/>
        <property name="partType" column="PartType"/>
        <property name="code" column="Code"/>
  </class>

The partType is nvarchar with the length of 1 in a SQL Server db. I am trying to select records that do not have a part type of any of this '0','4','5','6','7','a','b','c'. Hope I have answered your question. Thanks

like image 865
user1860447 Avatar asked Feb 27 '13 18:02

user1860447


People also ask

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.

What is criteria uniqueResult ()?

uniqueResult() Convenience method to return a single instance that matches the query, or null if the query returns no results. Method Detail.

How use like criteria query?

Double-click the Last Name and City fields to add them to the query design grid. In the City field, add the “Like B*” criteria, and click Run.


1 Answers

Criteria criteria = ...;
criteria.add(
  Restrictions.not(
    Restrictions.in("partType", new String[] {"0","4","5","6","7","a","b","c"})
  )
);

Hope it helps.

like image 50
danny.lesnik Avatar answered Sep 30 '22 08:09

danny.lesnik