HHH-8914 double negation broken on JPA criteria

This commit is contained in:
Brett Meyer 2014-01-30 13:15:50 -05:00
parent 535659d61f
commit fdc3b3b009
3 changed files with 62 additions and 8 deletions

View File

@ -40,14 +40,13 @@ import org.hibernate.jpa.criteria.expression.ExpressionImpl;
public abstract class AbstractPredicateImpl
extends ExpressionImpl<Boolean>
implements PredicateImplementor, Serializable {
private boolean negated;
protected AbstractPredicateImpl(CriteriaBuilderImpl criteriaBuilder) {
super( criteriaBuilder, Boolean.class );
}
public boolean isNegated() {
return negated;
return false;
}
public Predicate not() {

View File

@ -107,19 +107,19 @@ public class NegatedPredicateWrapper extends ExpressionImpl<Boolean> implements
@Override
public String render(boolean isNegated, RenderingContext renderingContext) {
return render( renderingContext );
}
@Override
public String render(RenderingContext renderingContext) {
if ( isJunction() ) {
return CompoundPredicate.render( this, renderingContext );
}
else {
return predicate.render( isNegated(), renderingContext );
return predicate.render( isNegated, renderingContext );
}
}
@Override
public String render(RenderingContext renderingContext) {
return render( isNegated(), renderingContext );
}
@Override
public String renderProjection(RenderingContext renderingContext) {
return render( renderingContext );

View File

@ -126,4 +126,59 @@ public class BasicCriteriaUsageTest extends BaseEntityManagerFunctionalTestCase
em.getTransaction().commit();
em.close();
}
@Test
@TestForIssue( jiraKey = "HHH-8914" )
public void testDoubleNegation() {
Wall wall1 = new Wall();
wall1.setColor( "yellow" );
Wall wall2 = new Wall();
wall2.setColor( null );
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
em.persist( wall1 );
em.persist( wall2 );
em.getTransaction().commit();
em.clear();
em.getTransaction().begin();
// Although the examples are simplified and the usages appear pointless,
// double negatives can occur in some dynamic applications (regardless
// if it results from bad design or not). Ensure we handle them as expected.
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Wall> query = cb.createQuery( Wall.class );
Root<Wall> root = query.from( Wall.class );
query.select( root ).where(
cb.not(
cb.isNotNull( root.get( "color" ) ) ) );
Wall result = em.createQuery( query ).getSingleResult();
assertNotNull( result );
assertEquals( null, result.getColor() );
query = cb.createQuery( Wall.class );
root = query.from( Wall.class );
query.select( root ).where(
cb.not(
cb.not(
cb.isNull( root.get( "color" ) ) ) ) );
result = em.createQuery( query ).getSingleResult();
assertNotNull( result );
assertEquals( null, result.getColor() );
query = cb.createQuery( Wall.class );
root = query.from( Wall.class );
query.select( root ).where(
cb.not(
cb.not(
cb.isNotNull( root.get( "color" ) ) ) ) );
result = em.createQuery( query ).getSingleResult();
assertNotNull( result );
assertEquals( "yellow", result.getColor() );
em.getTransaction().commit();
em.close();
}
}