Hm, this may be still lacking from the Criteria API. You can however add this easily by defining your own Criterion object. It should look like this:
Code:
public class NotInExpression extends AbstractCriterion {
private final String propertyName;
private final Object[] values;
NotInExpression(String propertyName, Object[] values) {
this.propertyName = propertyName;
this.values = values;
}
public String toSqlString(SessionFactoryImplementor sessionFactory, Class persistentClass, String alias, Map aliasClasses)
throws HibernateException {
String params;
if ( values.length>0 ) {
params = StringHelper.repeat( "?, ", values.length-1 );
params += "?";
}
else {
params = StringHelper.EMPTY_STRING;
}
String condition = " not in (" + params + ')';
return StringHelper.join(
" and ",
StringHelper.suffix(
getColumns(sessionFactory, persistentClass, propertyName, alias, aliasClasses),
condition
)
);
}
public TypedValue[] getTypedValues(SessionFactoryImplementor sessionFactory, Class persistentClass, Map aliasClasses) throws HibernateException {
TypedValue[] tvs = new TypedValue[ values.length ];
for ( int i=0; i<tvs.length; i++ ) {
tvs[i] = getTypedValue( sessionFactory, persistentClass, propertyName, values[i], aliasClasses );
}
return tvs;
}
public String toString() {
return propertyName + " not in (" + StringHelper.toString(values) + ')';
}
}
Then pass an instance of it to criteria.add