HHH-4583 - Incorrect handling of empty conjunction and disjunction
git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@18839 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
parent
6ca968f0e9
commit
894665481c
|
@ -73,6 +73,7 @@ import org.hibernate.ejb.criteria.expression.function.UpperFunction;
|
|||
import org.hibernate.ejb.criteria.path.PluralAttributePath;
|
||||
import org.hibernate.ejb.criteria.predicate.BooleanAssertionPredicate;
|
||||
import org.hibernate.ejb.criteria.predicate.BooleanExpressionPredicate;
|
||||
import org.hibernate.ejb.criteria.predicate.BooleanStaticAssertionPredicate;
|
||||
import org.hibernate.ejb.criteria.predicate.NullnessPredicate;
|
||||
import org.hibernate.ejb.criteria.predicate.CompoundPredicate;
|
||||
import org.hibernate.ejb.criteria.predicate.ComparisonPredicate;
|
||||
|
@ -316,9 +317,11 @@ public class CriteriaBuilderImpl implements CriteriaBuilder, Serializable {
|
|||
public Predicate isTrue(Expression<Boolean> expression) {
|
||||
if ( CompoundPredicate.class.isInstance( expression ) ) {
|
||||
final CompoundPredicate predicate = (CompoundPredicate) expression;
|
||||
if ( predicate.getOperator() == Predicate.BooleanOperator.OR
|
||||
&& predicate.getExpressions().size() == 0 ) {
|
||||
predicate.not();
|
||||
if ( predicate.getExpressions().size() == 0 ) {
|
||||
return new BooleanStaticAssertionPredicate(
|
||||
this,
|
||||
predicate.getOperator() == Predicate.BooleanOperator.AND
|
||||
);
|
||||
}
|
||||
return predicate;
|
||||
}
|
||||
|
@ -334,13 +337,13 @@ public class CriteriaBuilderImpl implements CriteriaBuilder, Serializable {
|
|||
public Predicate isFalse(Expression<Boolean> expression) {
|
||||
if ( CompoundPredicate.class.isInstance( expression ) ) {
|
||||
final CompoundPredicate predicate = (CompoundPredicate) expression;
|
||||
if ( predicate.getOperator() == Predicate.BooleanOperator.OR
|
||||
&& predicate.getExpressions().size() == 0 ) {
|
||||
// nothing to do
|
||||
}
|
||||
else {
|
||||
predicate.not();
|
||||
if ( predicate.getExpressions().size() == 0 ) {
|
||||
return new BooleanStaticAssertionPredicate(
|
||||
this,
|
||||
predicate.getOperator() == Predicate.BooleanOperator.OR
|
||||
);
|
||||
}
|
||||
predicate.not();
|
||||
return predicate;
|
||||
}
|
||||
else if ( Predicate.class.isInstance( expression ) ) {
|
||||
|
@ -351,7 +354,7 @@ public class CriteriaBuilderImpl implements CriteriaBuilder, Serializable {
|
|||
return new BooleanAssertionPredicate( this, expression, Boolean.FALSE );
|
||||
}
|
||||
|
||||
/**
|
||||
/**s
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public Predicate isNull(Expression<?> x) {
|
||||
|
|
|
@ -0,0 +1,81 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* Copyright (c) 2010, Red Hat Inc. or third-party contributors as
|
||||
* indicated by the @author tags or express copyright attribution
|
||||
* statements applied by the authors. All third-party contributions are
|
||||
* distributed under license by Red Hat Inc.
|
||||
*
|
||||
* This copyrighted material is made available to anyone wishing to use, modify,
|
||||
* copy, or redistribute it subject to the terms and conditions of the GNU
|
||||
* Lesser General Public License, as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this distribution; if not, write to:
|
||||
* Free Software Foundation, Inc.
|
||||
* 51 Franklin Street, Fifth Floor
|
||||
* Boston, MA 02110-1301 USA
|
||||
*/
|
||||
package org.hibernate.ejb.criteria.predicate;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import org.hibernate.ejb.criteria.CriteriaBuilderImpl;
|
||||
import org.hibernate.ejb.criteria.CriteriaQueryCompiler;
|
||||
import org.hibernate.ejb.criteria.ParameterRegistry;
|
||||
|
||||
/**
|
||||
* Predicate used to assert a static boolean condition.
|
||||
*
|
||||
* @author Steve Ebersole
|
||||
*/
|
||||
public class BooleanStaticAssertionPredicate
|
||||
extends AbstractSimplePredicate
|
||||
implements Serializable {
|
||||
private final Boolean assertedValue;
|
||||
|
||||
public BooleanStaticAssertionPredicate(
|
||||
CriteriaBuilderImpl criteriaBuilder,
|
||||
Boolean assertedValue) {
|
||||
super( criteriaBuilder );
|
||||
this.assertedValue = assertedValue;
|
||||
}
|
||||
|
||||
public Boolean getAssertedValue() {
|
||||
return assertedValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public void registerParameters(ParameterRegistry registry) {
|
||||
// nada
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public String render(CriteriaQueryCompiler.RenderingContext renderingContext) {
|
||||
boolean isTrue = getAssertedValue();
|
||||
if ( isNegated() ) {
|
||||
isTrue = !isTrue;
|
||||
}
|
||||
|
||||
return isTrue
|
||||
? "1=1"
|
||||
: "0=1";
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public String renderProjection(CriteriaQueryCompiler.RenderingContext renderingContext) {
|
||||
return render( renderingContext );
|
||||
}
|
||||
|
||||
}
|
|
@ -82,6 +82,66 @@ public class ExpressionsTest extends AbstractMetamodelSpecificTest {
|
|||
em.close();
|
||||
}
|
||||
|
||||
public void testEmptyConjunctionIsTrue() {
|
||||
EntityManager em = getOrCreateEntityManager();
|
||||
em.getTransaction().begin();
|
||||
CriteriaQuery<Product> criteria = builder.createQuery( Product.class );
|
||||
criteria.from( Product.class );
|
||||
criteria.where( builder.isTrue( builder.and() ) );
|
||||
List<Product> result = em.createQuery( criteria ).getResultList();
|
||||
assertEquals( 1, result.size() );
|
||||
em.getTransaction().commit();
|
||||
em.close();
|
||||
}
|
||||
|
||||
public void testEmptyConjunctionIsFalse() {
|
||||
EntityManager em = getOrCreateEntityManager();
|
||||
em.getTransaction().begin();
|
||||
CriteriaQuery<Product> criteria = builder.createQuery( Product.class );
|
||||
criteria.from( Product.class );
|
||||
criteria.where( builder.isFalse( builder.and() ) );
|
||||
List<Product> result = em.createQuery( criteria ).getResultList();
|
||||
assertEquals( 0, result.size() );
|
||||
em.getTransaction().commit();
|
||||
em.close();
|
||||
}
|
||||
|
||||
public void testEmptyDisjunction() {
|
||||
EntityManager em = getOrCreateEntityManager();
|
||||
em.getTransaction().begin();
|
||||
CriteriaQuery<Product> criteria = builder.createQuery( Product.class );
|
||||
criteria.from( Product.class );
|
||||
criteria.where( builder.disjunction() );
|
||||
List<Product> result = em.createQuery( criteria ).getResultList();
|
||||
assertEquals( 0, result.size() );
|
||||
em.getTransaction().commit();
|
||||
em.close();
|
||||
}
|
||||
|
||||
public void testEmptyDisjunctionIsTrue() {
|
||||
EntityManager em = getOrCreateEntityManager();
|
||||
em.getTransaction().begin();
|
||||
CriteriaQuery<Product> criteria = builder.createQuery( Product.class );
|
||||
criteria.from( Product.class );
|
||||
criteria.where( builder.isTrue( builder.disjunction() ) );
|
||||
List<Product> result = em.createQuery( criteria ).getResultList();
|
||||
assertEquals( 0, result.size() );
|
||||
em.getTransaction().commit();
|
||||
em.close();
|
||||
}
|
||||
|
||||
public void testEmptyDisjunctionIsFalse() {
|
||||
EntityManager em = getOrCreateEntityManager();
|
||||
em.getTransaction().begin();
|
||||
CriteriaQuery<Product> criteria = builder.createQuery( Product.class );
|
||||
criteria.from( Product.class );
|
||||
criteria.where( builder.isFalse( builder.disjunction() ) );
|
||||
List<Product> result = em.createQuery( criteria ).getResultList();
|
||||
assertEquals( 1, result.size() );
|
||||
em.getTransaction().commit();
|
||||
em.close();
|
||||
}
|
||||
|
||||
public void testDiff() {
|
||||
EntityManager em = getOrCreateEntityManager();
|
||||
em.getTransaction().begin();
|
||||
|
|
Loading…
Reference in New Issue