Skip to content

Commit

Permalink
HHH-6643 Criteria doesn't support a chaining of 2 not restrictions (sql
Browse files Browse the repository at this point in the history
= not not criterion)
  • Loading branch information
Brett Meyer committed Jan 3, 2013
1 parent 510f876 commit cd76f86
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 9 deletions.
Expand Up @@ -25,12 +25,12 @@
package org.hibernate.criterion;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.dialect.MySQLDialect;
import org.hibernate.engine.spi.TypedValue;

/**
* Negates another criterion
* @author Gavin King
* @author Brett Meyer
*/
public class NotExpression implements Criterion {

Expand All @@ -40,14 +40,9 @@ protected NotExpression(Criterion criterion) {
this.criterion = criterion;
}

public String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery)
throws HibernateException {
if ( criteriaQuery.getFactory().getDialect() instanceof MySQLDialect ) {
return "not (" + criterion.toSqlString(criteria, criteriaQuery) + ')';
}
else {
return "not " + criterion.toSqlString(criteria, criteriaQuery);
}
public String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery) throws HibernateException {
return criteriaQuery.getFactory().getDialect().getNotExpression(
criterion.toSqlString( criteria, criteriaQuery ) );
}

public TypedValue[] getTypedValues(
Expand Down
Expand Up @@ -2390,4 +2390,8 @@ public boolean useFollowOnLocking() {
public UniqueDelegate getUniqueDelegate() {
return uniqueDelegate;
}

public String getNotExpression( String expression ) {
return "not " + expression;
}
}
Expand Up @@ -396,4 +396,9 @@ public JDBCException convert(SQLException sqlException, String message, String s
}
};
}

@Override
public String getNotExpression( String expression ) {
return "not (" + expression + ")";
}
}
Expand Up @@ -581,4 +581,9 @@ public boolean forceLobAsLastValue() {
public boolean useFollowOnLocking() {
return true;
}

@Override
public String getNotExpression( String expression ) {
return "not (" + expression + ")";
}
}
Expand Up @@ -377,4 +377,9 @@ public boolean supportsExistsInSelect() {
public int getInExpressionCountLimit() {
return PARAM_LIST_SIZE_LIMIT;
}

@Override
public String getNotExpression( String expression ) {
return "not (" + expression + ")";
}
}
Expand Up @@ -2011,6 +2011,41 @@ public void testCriteriaFetchMode() {
session.close();

}

@Test
@TestForIssue( jiraKey = "HHH-6643" )
public void testNotNot() {
Student student1 = new Student();
student1.setName("Foo1 Foo1");
student1.setStudentNumber(1);
Student student2 = new Student();
student2.setName("Foo2 Foo2");
student2.setStudentNumber(2);

Session s = openSession();
Transaction t = s.beginTransaction();

s.persist( student1 );
s.persist( student2 );
s.flush();
s.clear();

// Although this example is simplified and the "not not" is pointless,
// double negatives can occur in some dynamic applications (regardless
// if it results from bad design or not). Test to ensure the dialect
// handles them as expected.
List<Student> students = s.createCriteria( Student.class ).add(
Restrictions.not(
Restrictions.not(
Restrictions.eq( "studentNumber", 1l ) ) )
).list();

assertEquals( students.size(), 1 );
assertEquals( students.get( 0 ).getStudentNumber(), 1 );

t.commit();
session.close();
}

}

0 comments on commit cd76f86

Please sign in to comment.