Merge branch 'master' of github.com:hibernate/hibernate-core

This commit is contained in:
Gail Badner 2010-12-17 10:42:17 -08:00
commit 4fa467a8a4
3 changed files with 150 additions and 35 deletions

View File

@ -252,7 +252,7 @@ public class CriteriaBuilderImpl implements CriteriaBuilder, Serializable {
public Predicate wrap(Expression<Boolean> expression) {
if ( Predicate.class.isInstance( expression ) ) {
return ( ( Predicate ) expression );
return ( (Predicate) expression );
}
else if ( PathImplementor.class.isInstance( expression ) ) {
return new BooleanAssertionPredicate( this, expression, Boolean.TRUE );
@ -340,7 +340,7 @@ public class CriteriaBuilderImpl implements CriteriaBuilder, Serializable {
if ( predicate.getExpressions().size() == 0 ) {
return new BooleanStaticAssertionPredicate(
this,
predicate.getOperator() == Predicate.BooleanOperator.OR
predicate.getOperator() == Predicate.BooleanOperator.OR
);
}
predicate.not();
@ -1363,7 +1363,7 @@ public class CriteriaBuilderImpl implements CriteriaBuilder, Serializable {
}
return new MemberOfPredicate<E, C>(
this,
e,
e,
(PluralAttributePath<C>)collectionExpression
);
}

View File

@ -24,15 +24,15 @@
package org.hibernate.ejb.criteria.predicate;
import java.io.Serializable;
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
import javax.persistence.criteria.Predicate;
import java.util.List;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import org.hibernate.ejb.criteria.ParameterRegistry;
import org.hibernate.ejb.criteria.CriteriaBuilderImpl;
import org.hibernate.ejb.criteria.CriteriaQueryCompiler;
import org.hibernate.ejb.criteria.ParameterRegistry;
import org.hibernate.ejb.criteria.Renderable;
/**
@ -44,15 +44,15 @@ import org.hibernate.ejb.criteria.Renderable;
public class CompoundPredicate
extends AbstractPredicateImpl
implements Serializable {
private final BooleanOperator operator;
private BooleanOperator operator;
private final List<Expression<Boolean>> expressions = new ArrayList<Expression<Boolean>>();
/**
* Constructs an empty conjunction or disjunction.
*
* @param criteriaBuilder The query builder from whcih this originates.
* @param operator Indicates whether this predicate will funtion
* as a conjunction or disjuntion.
* @param criteriaBuilder The query builder from which this originates.
* @param operator Indicates whether this predicate will function
* as a conjunction or disjunction.
*/
public CompoundPredicate(CriteriaBuilderImpl criteriaBuilder, BooleanOperator operator) {
super( criteriaBuilder );
@ -63,31 +63,31 @@ public class CompoundPredicate
* Constructs a conjunction or disjunction over the given expressions.
*
* @param criteriaBuilder The query builder from which this originates.
* @param operator Indicates whether this predicate will funtion
* as a conjunction or disjuntion.
* @param operator Indicates whether this predicate will function
* as a conjunction or disjunction.
* @param expressions The expressions to be grouped.
*/
public CompoundPredicate(
CriteriaBuilderImpl criteriaBuilder,
BooleanOperator operator,
Expression<Boolean>... expressions) {
this( criteriaBuilder, operator );
this( criteriaBuilder, operator );
applyExpressions( expressions );
}
/**
* Constructs a conjunction or disjunction over the given expressions.
*
* @param criteriaBuilder The query builder from whcih this originates.
* @param operator Indicates whether this predicate will funtion
* as a conjunction or disjuntion.
* @param criteriaBuilder The query builder from which this originates.
* @param operator Indicates whether this predicate will function
* as a conjunction or disjunction.
* @param expressions The expressions to be grouped.
*/
public CompoundPredicate(
CriteriaBuilderImpl criteriaBuilder,
BooleanOperator operator,
List<Expression<Boolean>> expressions) {
this( criteriaBuilder, operator );
this( criteriaBuilder, operator );
applyExpressions( expressions );
}
@ -110,7 +110,7 @@ public class CompoundPredicate
public void registerParameters(ParameterRegistry registry) {
for ( Expression expression : getExpressions() ) {
Helper.possibleParameter(expression, registry);
Helper.possibleParameter( expression, registry );
}
}
@ -125,7 +125,7 @@ public class CompoundPredicate
: "0=1"; // false
}
if ( getExpressions().size() == 1 ) {
return ( (Renderable) getExpressions().get(0) ).render( renderingContext );
return ( (Renderable) getExpressions().get( 0 ) ).render( renderingContext );
}
final StringBuilder buffer = new StringBuilder();
String sep = "";
@ -148,4 +148,29 @@ public class CompoundPredicate
public String renderProjection(CriteriaQueryCompiler.RenderingContext renderingContext) {
return render( renderingContext );
}
/**
* Create negation of compound predicate by using logic rules:
* 1. not (x || y) is (not x && not y)
* 2. not (x && y) is (not x || not y)
*/
@Override
public Predicate not() {
toggleOperator();
for ( Expression expr : this.getExpressions() ) {
if ( Predicate.class.isInstance( expr ) ) {
( (Predicate) expr ).not();
}
}
return this;
}
private void toggleOperator() {
if ( this.operator == BooleanOperator.AND ) {
this.operator = BooleanOperator.OR;
}
else {
this.operator = BooleanOperator.AND;
}
}
}

View File

@ -23,9 +23,11 @@
*/
package org.hibernate.ejb.criteria.basic;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import org.hibernate.ejb.metamodel.AbstractMetamodelSpecificTest;
@ -35,33 +37,121 @@ import org.hibernate.ejb.metamodel.Order;
* Test the various predicates.
*
* @author Steve Ebersole
* @author Hardy Ferentschik
*/
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();
private EntityManager em;
private CriteriaBuilder builder;
public void setUp() throws Exception {
super.setUp();
builder = factory.getCriteriaBuilder();
em = getOrCreateEntityManager();
createTestOrders();
em.getTransaction().begin();
CriteriaQuery<Order> orderCriteria = builder.createQuery(Order.class);
Root<Order> orderRoot = orderCriteria.from(Order.class);
orderCriteria.select(orderRoot);
orderCriteria.where( builder.isTrue( builder.conjunction() ) );
em.createQuery( orderCriteria ).getResultList();
}
public void tearDown() throws Exception {
em.getTransaction().commit();
em.close();
super.tearDown();
}
public void testEmptyConjunction() {
// yes this is a retarded case, but explicitly allowed in the JPA spec
CriteriaQuery<Order> orderCriteria = builder.createQuery( Order.class );
Root<Order> orderRoot = orderCriteria.from( Order.class );
orderCriteria.select( orderRoot );
orderCriteria.where( builder.isTrue( builder.conjunction() ) );
em.createQuery( orderCriteria ).getResultList();
List<Order> orders = em.createQuery( orderCriteria ).getResultList();
assertTrue( orders.size() == 3 );
}
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<Order> orderCriteria = builder.createQuery(Order.class);
Root<Order> orderRoot = orderCriteria.from(Order.class);
orderCriteria.select(orderRoot);
CriteriaQuery<Order> orderCriteria = builder.createQuery( Order.class );
Root<Order> orderRoot = orderCriteria.from( Order.class );
orderCriteria.select( orderRoot );
orderCriteria.where( builder.isFalse( builder.disjunction() ) );
em.createQuery( orderCriteria ).getResultList();
List<Order> orders = em.createQuery( orderCriteria ).getResultList();
assertTrue( orders.size() == 3 );
}
/**
* Check simple not.
*/
public void testSimpleNot() {
CriteriaQuery<Order> orderCriteria = builder.createQuery( Order.class );
Root<Order> orderRoot = orderCriteria.from( Order.class );
orderCriteria.select( orderRoot );
orderCriteria.where( builder.not( builder.equal( orderRoot.get( "id" ), "order-1" ) ) );
List<Order> orders = em.createQuery( orderCriteria ).getResultList();
assertTrue( orders.size() == 2 );
}
/**
* Check complicated not.
*/
public void testComplicatedNotOr() {
CriteriaQuery<Order> orderCriteria = builder.createQuery( Order.class );
Root<Order> orderRoot = orderCriteria.from( Order.class );
orderCriteria.select( orderRoot );
Predicate p1 = builder.equal( orderRoot.get( "id" ), "order-1" );
Predicate p2 = builder.equal( orderRoot.get( "id" ), "order-2" );
orderCriteria.where( builder.not( builder.or( p1, p2 ) ) );
List<Order> orders = em.createQuery( orderCriteria ).getResultList();
assertTrue( orders.size() == 1 );
Order order = orders.get( 0 );
assertEquals( "order-3", order.getId() );
}
/**
* Check complicated not.
*/
public void testNotMultipleOr() {
CriteriaQuery<Order> orderCriteria = builder.createQuery( Order.class );
Root<Order> orderRoot = orderCriteria.from( Order.class );
orderCriteria.select( orderRoot );
Predicate p1 = builder.equal( orderRoot.get( "id" ), "order-1" );
Predicate p2 = builder.equal( orderRoot.get( "id" ), "order-2" );
Predicate p3 = builder.equal( orderRoot.get( "id" ), "order-3" );
orderCriteria.where( builder.not( builder.or( p1, p2, p3 ) ) );
List<Order> orders = em.createQuery( orderCriteria ).getResultList();
assertTrue( orders.size() == 0 );
}
/**
* Check complicated not.
*/
public void testComplicatedNotAnd() {
CriteriaQuery<Order> orderCriteria = builder.createQuery( Order.class );
Root<Order> orderRoot = orderCriteria.from( Order.class );
orderCriteria.select( orderRoot );
Predicate p1 = builder.equal( orderRoot.get( "id" ), "order-1" );
Predicate p2 = builder.equal( orderRoot.get( "id" ), "order-2" );
orderCriteria.where( builder.not( builder.and( p1, p2 ) ) );
List<Order> orders = em.createQuery( orderCriteria ).getResultList();
assertTrue( orders.size() == 3 );
}
private void createTestOrders() {
em.getTransaction().begin();
em.persist( new Order( "order-1", 1.0d ) );
em.persist( new Order( "order-2", 10.0d ) );
em.persist( new Order( "order-3", 100.0d ) );
em.getTransaction().commit();
em.close();
}
}