How to code the NOT IN query in Hibernate criteria?
Asked Answered
G

1

7

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

Gudrin answered 27/2, 2013 at 18:53 Comment(1)
Could you explain what you are trying? How does the table look like which datatype has the field PartType?Squireen
I
19
Criteria criteria = ...;
criteria.add(
  Restrictions.not(
    Restrictions.in("partType", new String[] {"0","4","5","6","7","a","b","c"})
  )
);
Innutrition answered 27/2, 2013 at 20:25 Comment(2)
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 !! Thanks !!Gudrin
Expression is deprecated now, you should use Restrictions instead as specified in the answer.Colossian

© 2022 - 2024 — McMap. All rights reserved.