EJB-447 - Implement JPA 2.0 criteria apis (building)

git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@17340 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
Steve Ebersole 2009-08-17 19:09:17 +00:00
parent 3e22c213de
commit bd10b18c44
2 changed files with 40 additions and 7 deletions

View File

@ -27,6 +27,7 @@ import java.util.HashSet;
import java.util.List;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.ArrayList;
import javax.persistence.criteria.AbstractQuery;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Predicate;
@ -78,14 +79,18 @@ public class QueryStructure<T> {
ParameterContainer.Helper.possibleParameter(selection, registry);
ParameterContainer.Helper.possibleParameter(restriction, registry);
ParameterContainer.Helper.possibleParameter(having, registry);
for ( Subquery subquery : getSubqueries() ) {
ParameterContainer.Helper.possibleParameter(subquery, registry);
if ( subqueries != null ) {
for ( Subquery subquery : subqueries ) {
ParameterContainer.Helper.possibleParameter(subquery, registry);
}
}
// both group-by and having expressions can (though unlikely) contain parameters...
ParameterContainer.Helper.possibleParameter(having, registry);
for ( Expression<?> grouping : groupings ) {
ParameterContainer.Helper.possibleParameter(grouping, registry);
if ( groupings != null ) {
for ( Expression<?> grouping : groupings ) {
ParameterContainer.Helper.possibleParameter(grouping, registry);
}
}
return parameters;
@ -128,7 +133,7 @@ public class QueryStructure<T> {
}
public <X> Root<X> from(EntityType<X> entityType) {
RootImpl<X> root = new RootImpl( queryBuilder, entityType );
RootImpl<X> root = new RootImpl<X>( queryBuilder, entityType );
roots.add( root );
return root;
}
@ -179,9 +184,16 @@ public class QueryStructure<T> {
return subqueries;
}
public List<Subquery<?>> internalGetSubqueries() {
if ( subqueries == null ) {
subqueries = new ArrayList<Subquery<?>>();
}
return subqueries;
}
public <U> Subquery<U> subquery(Class<U> subqueryType) {
CriteriaSubqueryImpl<U> subquery = new CriteriaSubqueryImpl<U>( queryBuilder, subqueryType, owner );
subqueries.add( subquery );
internalGetSubqueries().add( subquery );
return subquery;
}
}

View File

@ -22,7 +22,11 @@
package org.hibernate.ejb.criteria.basic;
import javax.persistence.EntityManager;
import javax.persistence.metamodel.SingularAttribute;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.ParameterExpression;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Predicate;
import org.hibernate.ejb.test.TestCase;
@ -37,7 +41,7 @@ public class BasicCriteriaUsageTest extends TestCase {
return new Class[] { Wall.class };
}
public void testSimpliestCriterias() {
public void testSimplestCriterias() {
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
CriteriaQuery criteria = em.getQueryBuilder().createQuery();
@ -45,4 +49,21 @@ public class BasicCriteriaUsageTest extends TestCase {
em.getTransaction().commit();
em.close();
}
public void testParameterCollection() {
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
CriteriaQuery criteria = em.getQueryBuilder().createQuery();
Root<Wall> from = criteria.from( Wall.class );
ParameterExpression param = em.getQueryBuilder().parameter( String.class );
SingularAttribute<? super Wall,?> colorAttribute = em.getMetamodel()
.entity( Wall.class )
.getDeclaredSingularAttribute( "color" );
assertNotNull( "metamodel returned null singular attribute", colorAttribute );
Predicate predicate = em.getQueryBuilder().equal( from.get( colorAttribute ), param );
criteria.where( predicate );
assertEquals( 1, criteria.getParameters().size() );
em.getTransaction().commit();
em.close();
}
}