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

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Sat Jan 9 11:24:44 EST 2010


Author: steve.ebersole at jboss.com
Date: 2010-01-09 11:24:44 -0500 (Sat, 09 Jan 2010)
New Revision: 18481

Added:
   core/trunk/entitymanager/src/test/java/org/hibernate/ejb/criteria/subquery/CorrelatedSubqueryTest.java
Modified:
   core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/CriteriaSubqueryImpl.java
   core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/FromImplementor.java
   core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/QueryStructure.java
   core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/path/AbstractFromImpl.java
Log:
HHH-4768 - Bug in how Criteria Subquery correlations are handled


Modified: core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/CriteriaSubqueryImpl.java
===================================================================
--- core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/CriteriaSubqueryImpl.java	2010-01-09 15:49:30 UTC (rev 18480)
+++ core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/CriteriaSubqueryImpl.java	2010-01-09 16:24:44 UTC (rev 18481)
@@ -26,11 +26,9 @@
 import java.io.Serializable;
 import java.util.List;
 import java.util.Set;
-import java.util.HashSet;
 import javax.persistence.criteria.AbstractQuery;
 import javax.persistence.criteria.CollectionJoin;
 import javax.persistence.criteria.Expression;
-import javax.persistence.criteria.Fetch;
 import javax.persistence.criteria.Join;
 import javax.persistence.criteria.ListJoin;
 import javax.persistence.criteria.MapJoin;
@@ -53,18 +51,6 @@
 	private final AbstractQuery<?> parent;
 	private final QueryStructure<T> queryStructure;
 
-	private Set<Join<?, ?>> correlatedJoins = new HashSet<Join<?,?>>();
-
-	private final FromImplementor.JoinScope joinScope = new FromImplementor.JoinScope() {
-		public void addJoin(Join join) {
-			correlatedJoins.add( join );
-		}
-
-		public void addFetch(Fetch fetch) {
-			throw new UnsupportedOperationException( "Cannot define fetch from a subquery correlation" );
-		}
-	};
-
 	public CriteriaSubqueryImpl(
 			CriteriaBuilderImpl criteriaBuilder,
 			Class<T> javaType,
@@ -75,15 +61,6 @@
 	}
 
 	/**
-	 * Get the scope used to scope joins to this subquery.
-	 *
-	 * @return The subquery's join scope.
-	 */
-	public FromImplementor.JoinScope getJoinScope() {
-		return joinScope;
-	}
-
-	/**
 	 * {@inheritDoc}
 	 */
 	public AbstractQuery<?> getParent() {
@@ -235,49 +212,61 @@
 	 * {@inheritDoc}
 	 */
     public Set<Join<?, ?>> getCorrelatedJoins() {
-		return correlatedJoins;
+		return queryStructure.collectCorrelatedJoins();
 	}
 
 	/**
 	 * {@inheritDoc}
 	 */
 	public <Y> Root<Y> correlate(Root<Y> source) {
-		return ( ( RootImpl<Y> ) source ).correlateTo( this );
+		final RootImpl<Y> correlation = ( ( RootImpl<Y> ) source ).correlateTo( this );
+		queryStructure.addCorrelationRoot( correlation );
+		return correlation;
 	}
 
 	/**
 	 * {@inheritDoc}
 	 */
 	public <X, Y> Join<X, Y> correlate(Join<X, Y> source) {
-		return ( (JoinImplementor<X,Y>) source ).correlateTo( this );
+		final JoinImplementor<X,Y> correlation = ( (JoinImplementor<X,Y>) source ).correlateTo( this );
+		queryStructure.addCorrelationRoot( correlation );
+		return correlation;
 	}
 
 	/**
 	 * {@inheritDoc}
 	 */
 	public <X, Y> CollectionJoin<X, Y> correlate(CollectionJoin<X, Y> source) {
-		return ( (CollectionJoinImplementor<X,Y>) source ).correlateTo( this );
+		final CollectionJoinImplementor<X,Y> correlation = ( (CollectionJoinImplementor<X,Y>) source ).correlateTo( this );
+		queryStructure.addCorrelationRoot( correlation );
+		return correlation;
 	}
 
 	/**
 	 * {@inheritDoc}
 	 */
 	public <X, Y> SetJoin<X, Y> correlate(SetJoin<X, Y> source) {
-		return ( (SetJoinImplementor<X,Y>) source ).correlateTo( this );
+		final SetJoinImplementor<X,Y> correlation = ( (SetJoinImplementor<X,Y>) source ).correlateTo( this );
+		queryStructure.addCorrelationRoot( correlation );
+		return correlation;
 	}
 
 	/**
 	 * {@inheritDoc}
 	 */
 	public <X, Y> ListJoin<X, Y> correlate(ListJoin<X, Y> source) {
-		return ( (ListJoinImplementor<X,Y>) source ).correlateTo( this );
+		final ListJoinImplementor<X,Y> correlation = ( (ListJoinImplementor<X,Y>) source ).correlateTo( this );
+		queryStructure.addCorrelationRoot( correlation );
+		return correlation;
 	}
 
 	/**
 	 * {@inheritDoc}
 	 */
 	public <X, K, V> MapJoin<X, K, V> correlate(MapJoin<X, K, V> source) {
-		return ( (MapJoinImplementor<X, K, V>) source ).correlateTo( this );
+		final MapJoinImplementor<X, K, V> correlation = ( (MapJoinImplementor<X, K, V>) source ).correlateTo( this );
+		queryStructure.addCorrelationRoot( correlation );
+		return correlation;
 	}
 
 	/**

Modified: core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/FromImplementor.java
===================================================================
--- core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/FromImplementor.java	2010-01-09 15:49:30 UTC (rev 18480)
+++ core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/FromImplementor.java	2010-01-09 16:24:44 UTC (rev 18481)
@@ -37,14 +37,8 @@
 	public void prepareAlias(CriteriaQueryCompiler.RenderingContext renderingContext);
 	public String renderTableExpression(CriteriaQueryCompiler.RenderingContext renderingContext);
 
-	/**
-	 * Helper contract used to define who/what keeps track of joins and fetches made from this <tt>FROM</tt>.
-	 */
-	public static interface JoinScope<X> extends Serializable {
-		public void addJoin(Join<X, ?> join);
-		public void addFetch(Fetch<X,?> fetch);
-	}
 
 	public FromImplementor<Z,X> correlateTo(CriteriaSubqueryImpl subquery);
-	public void prepareCorrelationDelegate(JoinScope<X> joinScope, FromImplementor<Z,X> parent);
+	public void prepareCorrelationDelegate(FromImplementor<Z,X> parent);
+	public FromImplementor<Z, X> getCorrelationParent();
 }

Modified: core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/QueryStructure.java
===================================================================
--- core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/QueryStructure.java	2010-01-09 15:49:30 UTC (rev 18480)
+++ core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/QueryStructure.java	2010-01-09 16:24:44 UTC (rev 18481)
@@ -60,15 +60,18 @@
 public class QueryStructure<T> implements Serializable {
 	private final AbstractQuery<T> owner;
 	private final CriteriaBuilderImpl criteriaBuilder;
+	private final boolean isSubQuery;
 
 	public QueryStructure(AbstractQuery<T> owner, CriteriaBuilderImpl criteriaBuilder) {
 		this.owner = owner;
 		this.criteriaBuilder = criteriaBuilder;
+		this.isSubQuery = Subquery.class.isInstance( owner );
 	}
 
 	private boolean distinct;
 	private Selection<? extends T> selection;
 	private Set<Root<?>> roots = new HashSet<Root<?>>();
+	private Set<FromImplementor> correlationRoots;
 	private Predicate restriction;
 	private List<Expression<?>> groupings = Collections.emptyList();
 	private Predicate having;
@@ -148,6 +151,36 @@
 	}
 
 
+	// CORRELATION ROOTS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+	public void addCorrelationRoot(FromImplementor fromImplementor) {
+		if ( !isSubQuery ) {
+			throw new IllegalStateException( "Query is not identified as sub-query" );
+		}
+		if ( correlationRoots == null ) {
+			correlationRoots = new HashSet<FromImplementor>();
+		}
+		correlationRoots.add( fromImplementor );
+	}
+
+	public Set<Join<?, ?>> collectCorrelatedJoins() {
+		if ( !isSubQuery ) {
+			throw new IllegalStateException( "Query is not identified as sub-query" );
+		}
+		final Set<Join<?, ?>> correlatedJoins;
+		if ( correlationRoots != null ) {
+			correlatedJoins = new HashSet<Join<?,?>>();
+			for ( FromImplementor<?,?> correlationRoot : correlationRoots ) {
+				correlatedJoins.addAll( correlationRoot.getJoins() );
+			}
+		}
+		else {
+			correlatedJoins = Collections.emptySet();
+		}
+		return correlatedJoins;
+	}
+
+
 	// RESTRICTIONS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
 	public Predicate getRestriction() {
@@ -213,28 +246,14 @@
 			jpaqlQuery.append( "distinct " );
 		}
 		if ( getSelection() == null ) {
-			// we should have only a single root (query validation should have checked this...)
-			final Root root = getRoots().iterator().next();
-			jpaqlQuery.append( ( (Renderable) root ).renderProjection( renderingContext) );
+			jpaqlQuery.append( locateImplicitSelection().renderProjection( renderingContext ) );
 		}
 		else {
 			jpaqlQuery.append( ( (Renderable) getSelection() ).renderProjection( renderingContext ) );
 		}
 
-		jpaqlQuery.append( " from " );
-		String sep = "";
-		for ( Root root : getRoots() ) {
-			( (FromImplementor) root ).prepareAlias( renderingContext );
-			jpaqlQuery.append( sep );
-			jpaqlQuery.append( ( (FromImplementor) root ).renderTableExpression( renderingContext ) );
-			sep = ", ";
-		}
+		renderFromClause( jpaqlQuery, renderingContext );
 
-		for ( Root root : getRoots() ) {
-			renderJoins( jpaqlQuery, renderingContext, root.getJoins() );
-			renderFetches( jpaqlQuery, renderingContext, root.getFetches() );
-		}
-
 		if ( getRestriction() != null) {
 			jpaqlQuery.append( " where " )
 					.append( ( (Renderable) getRestriction() ).render( renderingContext ) );
@@ -242,7 +261,7 @@
 
 		if ( ! getGroupings().isEmpty() ) {
 			jpaqlQuery.append( " group by " );
-			sep = "";
+			String sep = "";
 			for ( Expression grouping : getGroupings() ) {
 				jpaqlQuery.append( sep )
 						.append( ( (Renderable) grouping ).render( renderingContext ) );
@@ -256,7 +275,71 @@
 		}
 	}
 
+	private FromImplementor locateImplicitSelection() {
+		FromImplementor implicitSelection = null;
+
+		if ( ! isSubQuery ) {
+			// we should have only a single root (query validation should have checked this...)
+			implicitSelection = (FromImplementor) getRoots().iterator().next();
+		}
+		else {
+			// we should only have a single "root" which can act as the implicit selection
+			final Set<Join<?, ?>> correlatedJoins = collectCorrelatedJoins();
+			if ( correlatedJoins != null ) {
+				if ( correlatedJoins.size() == 1 ) {
+					implicitSelection = (FromImplementor) correlatedJoins.iterator().next();
+				}
+			}
+		}
+
+		if ( implicitSelection == null ) {
+			throw new IllegalStateException( "No explicit selection and an implicit one cold not be determined" );
+		}
+
+		return implicitSelection;
+	}
+
 	@SuppressWarnings({ "unchecked" })
+	private void renderFromClause(StringBuilder jpaqlQuery, CriteriaQueryCompiler.RenderingContext renderingContext) {
+		jpaqlQuery.append( " from " );
+		String sep = "";
+		for ( Root root : getRoots() ) {
+			( (FromImplementor) root ).prepareAlias( renderingContext );
+			jpaqlQuery.append( sep );
+			jpaqlQuery.append( ( (FromImplementor) root ).renderTableExpression( renderingContext ) );
+			sep = ", ";
+		}
+
+		for ( Root root : getRoots() ) {
+			renderJoins( jpaqlQuery, renderingContext, root.getJoins() );
+			renderFetches( jpaqlQuery, renderingContext, root.getFetches() );
+		}
+
+		if ( isSubQuery ) {
+			if ( correlationRoots != null ) {
+				for ( FromImplementor<?,?> correlationRoot : correlationRoots ) {
+					final FromImplementor correlationParent = correlationRoot.getCorrelationParent();
+					correlationParent.prepareAlias( renderingContext );
+					final String correlationRootAlias = correlationParent.getAlias();
+					for ( Join<?,?> correlationJoin : correlationRoot.getJoins() ) {
+						final JoinImplementor correlationJoinImpl = (JoinImplementor) correlationJoin;
+						// IMPL NOTE: reuse the sep from above!
+						jpaqlQuery.append( sep );
+						correlationJoinImpl.prepareAlias( renderingContext );
+						jpaqlQuery.append( correlationRootAlias )
+								.append( '.' )
+								.append( correlationJoinImpl.getAttribute().getName() )
+								.append( " as " )
+								.append( correlationJoinImpl.getAlias() );
+						sep = ", ";
+						renderJoins( jpaqlQuery, renderingContext, correlationJoinImpl.getJoins() );
+					}
+				}
+			}
+		}
+	}
+
+	@SuppressWarnings({ "unchecked" })
 	private void renderJoins(
 			StringBuilder jpaqlQuery,
 			CriteriaQueryCompiler.RenderingContext renderingContext,

Modified: core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/path/AbstractFromImpl.java
===================================================================
--- core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/path/AbstractFromImpl.java	2010-01-09 15:49:30 UTC (rev 18480)
+++ core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/path/AbstractFromImpl.java	2010-01-09 16:24:44 UTC (rev 18481)
@@ -102,10 +102,21 @@
 	 */
 	public void prepareAlias(CriteriaQueryCompiler.RenderingContext renderingContext) {
 		if ( getAlias() == null ) {
-			setAlias( renderingContext.generateAlias() );
+			if ( isCorrelated() ) {
+				setAlias( getCorrelationParent().getAlias() );
+			}
+			else {
+				setAlias( renderingContext.generateAlias() );
+			}
 		}
 	}
 
+	@Override
+	public String renderProjection(CriteriaQueryCompiler.RenderingContext renderingContext) {
+		prepareAlias( renderingContext );
+		return getAlias();
+	}
+
 	/**
 	 * {@inheritDoc}
 	 */
@@ -137,8 +148,24 @@
 
 	// CORRELATION ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
-	private From<Z,X> correlationParent;
-	private JoinScope<X> joinScope = new JoinScope<X>() {
+	// IMPL NOTE : another means from handling correlations is to create a series of
+	//		specialized From implementations that represent the correlation roots.  While
+	//		that may be cleaner code-wise, it is certainly means creating a lot of "extra"
+	//		classes since we'd need one for each Subquery#correlate method
+
+	private FromImplementor<Z,X> correlationParent;
+
+	private JoinScope<X> joinScope = new BasicJoinScope();
+
+	/**
+	 * Helper contract used to define who/what keeps track of joins and fetches made from this <tt>FROM</tt>.
+	 */
+	public static interface JoinScope<X> extends Serializable {
+		public void addJoin(Join<X, ?> join);
+		public void addFetch(Fetch<X,?> fetch);
+	}
+
+	protected class BasicJoinScope implements JoinScope<X> {
 		public void addJoin(Join<X, ?> join) {
 			if ( joins == null ) {
 				joins = new LinkedHashSet<Join<X,?>>();
@@ -152,8 +179,21 @@
 			}
 			fetches.add( fetch );
 		}
-	};
+	}
 
+	protected class CorrelationJoinScope implements JoinScope<X> {
+		public void addJoin(Join<X, ?> join) {
+			if ( joins == null ) {
+				joins = new LinkedHashSet<Join<X,?>>();
+			}
+			joins.add( join );
+		}
+
+		public void addFetch(Fetch<X, ?> fetch) {
+			throw new UnsupportedOperationException( "Cannot define fetch from a subquery correlation" );
+		}
+	}
+
 	/**
 	 * {@inheritDoc}
 	 */
@@ -164,7 +204,7 @@
 	/**
 	 * {@inheritDoc}
 	 */
-	public From<Z,X> getCorrelationParent() {
+	public FromImplementor<Z,X> getCorrelationParent() {
 		return correlationParent;
 	}
 
@@ -174,17 +214,21 @@
 	@SuppressWarnings({ "unchecked" })
 	public FromImplementor<Z, X> correlateTo(CriteriaSubqueryImpl subquery) {
 		final FromImplementor<Z, X> correlationDelegate = createCorrelationDelegate();
-		correlationDelegate.prepareCorrelationDelegate( subquery.getJoinScope(), this );
+		correlationDelegate.prepareCorrelationDelegate( this );
 		return correlationDelegate;
 	}
 
 	protected abstract FromImplementor<Z, X> createCorrelationDelegate();
 
-	public void prepareCorrelationDelegate(JoinScope<X> joinScope, FromImplementor<Z, X> parent) {
-		this.joinScope = joinScope;
+	public void prepareCorrelationDelegate(FromImplementor<Z, X> parent) {
+		this.joinScope = new CorrelationJoinScope();
 		this.correlationParent = parent;
 	}
 
+	@Override
+	public String getAlias() {
+		return isCorrelated() ? getCorrelationParent().getAlias() : super.getAlias();
+	}
 
 	// JOINS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 

Added: core/trunk/entitymanager/src/test/java/org/hibernate/ejb/criteria/subquery/CorrelatedSubqueryTest.java
===================================================================
--- core/trunk/entitymanager/src/test/java/org/hibernate/ejb/criteria/subquery/CorrelatedSubqueryTest.java	                        (rev 0)
+++ core/trunk/entitymanager/src/test/java/org/hibernate/ejb/criteria/subquery/CorrelatedSubqueryTest.java	2010-01-09 16:24:44 UTC (rev 18481)
@@ -0,0 +1,130 @@
+/*
+ * 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.subquery;
+
+import java.util.List;
+import javax.persistence.EntityManager;
+import javax.persistence.TypedQuery;
+import javax.persistence.criteria.CriteriaBuilder;
+import javax.persistence.criteria.CriteriaQuery;
+import javax.persistence.criteria.Join;
+import javax.persistence.criteria.Root;
+import javax.persistence.criteria.Subquery;
+
+import org.hibernate.ejb.metamodel.AbstractMetamodelSpecificTest;
+import org.hibernate.ejb.metamodel.Customer;
+import org.hibernate.ejb.metamodel.Customer_;
+import org.hibernate.ejb.metamodel.LineItem;
+import org.hibernate.ejb.metamodel.LineItem_;
+import org.hibernate.ejb.metamodel.Order;
+import org.hibernate.ejb.metamodel.Order_;
+
+/**
+ * TODO : javadoc
+ *
+ * @author Steve Ebersole
+ */
+public class CorrelatedSubqueryTest extends AbstractMetamodelSpecificTest {
+	public void testBasicCorrelation() {
+		CriteriaBuilder builder = factory.getCriteriaBuilder();
+		EntityManager em = getOrCreateEntityManager();
+		em.getTransaction().begin();
+
+		CriteriaQuery<Customer> criteria = builder.createQuery( Customer.class );
+		Root<Customer> customer = criteria.from( Customer.class );
+		criteria.select( customer );
+		Subquery<Order> orderSubquery = criteria.subquery( Order.class );
+		Root<Customer> customerCorrelationRoot = orderSubquery.correlate( customer );
+		Join<Customer, Order> customerOrderCorrelationJoin = customerCorrelationRoot.join( Customer_.orders );
+		orderSubquery.select( customerOrderCorrelationJoin );
+		criteria.where( builder.not( builder.exists( orderSubquery ) ) );
+		em.createQuery( criteria ).getResultList();
+
+		em.getTransaction().commit();
+		em.close();
+	}
+
+	public void testRestrictedCorrelation() {
+		CriteriaBuilder builder = factory.getCriteriaBuilder();
+		EntityManager em = getOrCreateEntityManager();
+		em.getTransaction().begin();
+
+		CriteriaQuery<Order> criteria = builder.createQuery( Order.class );
+		Root<Order> orderRoot = criteria.from( Order.class );
+		criteria.select( orderRoot );
+		// create correlated subquery
+		Subquery<Customer> customerSubquery = criteria.subquery( Customer.class );
+		Root<Order> orderRootCorrelation = customerSubquery.correlate( orderRoot );
+		Join<Order, Customer> orderCustomerJoin = orderRootCorrelation.join( Order_.customer );
+		customerSubquery.where( builder.like( orderCustomerJoin.get( Customer_.name ), "%Caruso" ) )
+				.select( orderCustomerJoin );
+		criteria.where( builder.exists( customerSubquery ) );
+		em.createQuery( criteria ).getResultList();
+
+		em.getTransaction().commit();
+		em.close();
+	}
+
+	public void testCorrelationExplicitSelectionCorrelation() {
+		CriteriaBuilder builder = factory.getCriteriaBuilder();
+		EntityManager em = getOrCreateEntityManager();
+		em.getTransaction().begin();
+
+		CriteriaQuery<Customer> customerCriteria = builder.createQuery( Customer.class );
+		Root<Customer> customer = customerCriteria.from( Customer.class );
+		Join<Customer, Order> o = customer.join( Customer_.orders );
+		Subquery<Order> sq = customerCriteria.subquery(Order.class);
+		Join<Customer, Order> sqo = sq.correlate(o);
+		Join<Order, LineItem> sql = sqo.join(Order_.lineItems);
+		sq.where( builder.gt(sql.get( LineItem_.quantity), 3) );
+		// use the correlation itself as the subquery selection (initially caused problems wrt aliases)
+		sq.select(sqo);
+		customerCriteria.select(customer).distinct(true);
+		customerCriteria.where(builder.exists(sq));
+		em.createQuery( customerCriteria ).getResultList();
+
+		em.getTransaction().commit();
+		em.close();
+	}
+
+	public void testRestrictedCorrelationNoExplicitSelection() {
+		CriteriaBuilder builder = factory.getCriteriaBuilder();
+		EntityManager em = getOrCreateEntityManager();
+		em.getTransaction().begin();
+
+		CriteriaQuery<Order> criteria = builder.createQuery( Order.class );
+		Root<Order> orderRoot = criteria.from( Order.class );
+		criteria.select( orderRoot );
+		// create correlated subquery
+		Subquery<Customer> customerSubquery = criteria.subquery( Customer.class );
+		Root<Order> orderRootCorrelation = customerSubquery.correlate( orderRoot );
+		Join<Order, Customer> orderCustomerJoin = orderRootCorrelation.join( "customer" );
+		customerSubquery.where( builder.like( orderCustomerJoin.<String>get( "name" ), "%Caruso" ) );
+		criteria.where( builder.exists( customerSubquery ) );
+		em.createQuery( criteria ).getResultList();
+
+		em.getTransaction().commit();
+		em.close();
+	}
+}



More information about the hibernate-commits mailing list