[hibernate-commits] Hibernate SVN: r18839 - in core/trunk/entitymanager/src: main/java/org/hibernate/ejb/criteria/predicate and 1 other directories.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Thu Feb 18 12:00:48 EST 2010


Author: steve.ebersole at jboss.com
Date: 2010-02-18 12:00:47 -0500 (Thu, 18 Feb 2010)
New Revision: 18839

Added:
   core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/predicate/BooleanStaticAssertionPredicate.java
Modified:
   core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/CriteriaBuilderImpl.java
   core/trunk/entitymanager/src/test/java/org/hibernate/ejb/criteria/basic/ExpressionsTest.java
Log:
HHH-4583 - Incorrect handling of empty conjunction and disjunction


Modified: core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/CriteriaBuilderImpl.java
===================================================================
--- core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/CriteriaBuilderImpl.java	2010-02-18 16:19:05 UTC (rev 18838)
+++ core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/CriteriaBuilderImpl.java	2010-02-18 17:00:47 UTC (rev 18839)
@@ -73,6 +73,7 @@
 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 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 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
+			if ( predicate.getExpressions().size() == 0 ) {
+				return new BooleanStaticAssertionPredicate(
+						this,
+						predicate.getOperator() == Predicate.BooleanOperator.OR 
+				);
 			}
-			else {
-				predicate.not();
-			}
+			predicate.not();
 			return predicate;
 		}
 		else if ( Predicate.class.isInstance( expression ) ) {
@@ -351,7 +354,7 @@
 		return new BooleanAssertionPredicate( this, expression, Boolean.FALSE );
 	}
 
-	/**
+	/**s
 	 * {@inheritDoc}
 	 */
 	public Predicate isNull(Expression<?> x) {

Copied: core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/predicate/BooleanStaticAssertionPredicate.java (from rev 18810, core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/predicate/BooleanAssertionPredicate.java)
===================================================================
--- core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/predicate/BooleanStaticAssertionPredicate.java	                        (rev 0)
+++ core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/predicate/BooleanStaticAssertionPredicate.java	2010-02-18 17:00:47 UTC (rev 18839)
@@ -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 );
+	}
+
+}
\ No newline at end of file

Modified: core/trunk/entitymanager/src/test/java/org/hibernate/ejb/criteria/basic/ExpressionsTest.java
===================================================================
--- core/trunk/entitymanager/src/test/java/org/hibernate/ejb/criteria/basic/ExpressionsTest.java	2010-02-18 16:19:05 UTC (rev 18838)
+++ core/trunk/entitymanager/src/test/java/org/hibernate/ejb/criteria/basic/ExpressionsTest.java	2010-02-18 17:00:47 UTC (rev 18839)
@@ -82,6 +82,66 @@
 		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();



More information about the hibernate-commits mailing list