Search code examples
hibernatecriteria

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


Solution

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