From 20b78f45a783ca7812de3c5d56e8ef72fd87e232 Mon Sep 17 00:00:00 2001 From: Steve Ebersole Date: Sat, 9 Jan 2010 17:04:55 +0000 Subject: [PATCH] HHH-4772 - Empty conjunction/disjunction in criteria does not follow spec rules git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@18483 1b8cb986-b30d-0410-93ca-fae66ebed9b2 --- .../criteria/predicate/CompoundPredicate.java | 5 ++ .../ejb/criteria/basic/PredicateTest.java | 67 +++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 entitymanager/src/test/java/org/hibernate/ejb/criteria/basic/PredicateTest.java diff --git a/entitymanager/src/main/java/org/hibernate/ejb/criteria/predicate/CompoundPredicate.java b/entitymanager/src/main/java/org/hibernate/ejb/criteria/predicate/CompoundPredicate.java index 6b9c9605bf..516a275997 100644 --- a/entitymanager/src/main/java/org/hibernate/ejb/criteria/predicate/CompoundPredicate.java +++ b/entitymanager/src/main/java/org/hibernate/ejb/criteria/predicate/CompoundPredicate.java @@ -115,6 +115,11 @@ public class CompoundPredicate } public String render(CriteriaQueryCompiler.RenderingContext renderingContext) { + if ( getExpressions().size() == 0 ) { + return getOperator() == Predicate.BooleanOperator.AND + ? "true" + : "false"; + } if ( getExpressions().size() == 1 ) { return ( (Renderable) getExpressions().get(0) ).render( renderingContext ); } diff --git a/entitymanager/src/test/java/org/hibernate/ejb/criteria/basic/PredicateTest.java b/entitymanager/src/test/java/org/hibernate/ejb/criteria/basic/PredicateTest.java new file mode 100644 index 0000000000..2d2d12df5a --- /dev/null +++ b/entitymanager/src/test/java/org/hibernate/ejb/criteria/basic/PredicateTest.java @@ -0,0 +1,67 @@ +/* + * 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.basic; + +import javax.persistence.EntityManager; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.CriteriaQuery; +import javax.persistence.criteria.Root; + +import org.hibernate.ejb.metamodel.AbstractMetamodelSpecificTest; +import org.hibernate.ejb.metamodel.Order; + +/** + * Test the various predicates. + * + * @author Steve Ebersole + */ +public class PredicateTest extends AbstractMetamodelSpecificTest { + public void testEmptyConjunction() { + // yes this is a retarded case, but explicitly allowed in the JPA spec + CriteriaBuilder builder = factory.getCriteriaBuilder(); + EntityManager em = getOrCreateEntityManager(); + em.getTransaction().begin(); + CriteriaQuery orderCriteria = builder.createQuery(Order.class); + Root orderRoot = orderCriteria.from(Order.class); + orderCriteria.select(orderRoot); + orderCriteria.where( builder.isTrue( builder.conjunction() ) ); + em.createQuery( orderCriteria ).getResultList(); + em.getTransaction().commit(); + em.close(); + } + + public void testEmptyDisjunction() { + // yes this is a retarded case, but explicitly allowed in the JPA spec + CriteriaBuilder builder = factory.getCriteriaBuilder(); + EntityManager em = getOrCreateEntityManager(); + em.getTransaction().begin(); + CriteriaQuery orderCriteria = builder.createQuery(Order.class); + Root orderRoot = orderCriteria.from(Order.class); + orderCriteria.select(orderRoot); + orderCriteria.where( builder.isFalse( builder.disjunction() ) ); + em.createQuery( orderCriteria ).getResultList(); + em.getTransaction().commit(); + em.close(); + } +}