mirror of
https://github.com/hibernate/hibernate-orm
synced 2025-02-10 05:04:52 +00:00
HHH-8914 double negation broken on JPA criteria
This commit is contained in:
parent
535659d61f
commit
fdc3b3b009
@ -40,14 +40,13 @@
|
|||||||
public abstract class AbstractPredicateImpl
|
public abstract class AbstractPredicateImpl
|
||||||
extends ExpressionImpl<Boolean>
|
extends ExpressionImpl<Boolean>
|
||||||
implements PredicateImplementor, Serializable {
|
implements PredicateImplementor, Serializable {
|
||||||
private boolean negated;
|
|
||||||
|
|
||||||
protected AbstractPredicateImpl(CriteriaBuilderImpl criteriaBuilder) {
|
protected AbstractPredicateImpl(CriteriaBuilderImpl criteriaBuilder) {
|
||||||
super( criteriaBuilder, Boolean.class );
|
super( criteriaBuilder, Boolean.class );
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isNegated() {
|
public boolean isNegated() {
|
||||||
return negated;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Predicate not() {
|
public Predicate not() {
|
||||||
|
@ -107,19 +107,19 @@ public void registerParameters(ParameterRegistry registry) {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String render(boolean isNegated, RenderingContext renderingContext) {
|
public String render(boolean isNegated, RenderingContext renderingContext) {
|
||||||
return render( renderingContext );
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String render(RenderingContext renderingContext) {
|
|
||||||
if ( isJunction() ) {
|
if ( isJunction() ) {
|
||||||
return CompoundPredicate.render( this, renderingContext );
|
return CompoundPredicate.render( this, renderingContext );
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return predicate.render( isNegated(), renderingContext );
|
return predicate.render( isNegated, renderingContext );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String render(RenderingContext renderingContext) {
|
||||||
|
return render( isNegated(), renderingContext );
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String renderProjection(RenderingContext renderingContext) {
|
public String renderProjection(RenderingContext renderingContext) {
|
||||||
return render( renderingContext );
|
return render( renderingContext );
|
||||||
|
@ -126,4 +126,59 @@ public void testFunctionCriteria() {
|
|||||||
em.getTransaction().commit();
|
em.getTransaction().commit();
|
||||||
em.close();
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user