Author: steve.ebersole(a)jboss.com
Date: 2009-08-17 15:09:17 -0400 (Mon, 17 Aug 2009)
New Revision: 17340
Modified:
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/QueryStructure.java
core/trunk/entitymanager/src/test/java/org/hibernate/ejb/criteria/basic/BasicCriteriaUsageTest.java
Log:
EJB-447 - Implement JPA 2.0 criteria apis (building)
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 2009-08-17
19:08:40 UTC (rev 17339)
+++
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/QueryStructure.java 2009-08-17
19:09:17 UTC (rev 17340)
@@ -27,6 +27,7 @@
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 @@
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 <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 @@
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;
}
}
Modified:
core/trunk/entitymanager/src/test/java/org/hibernate/ejb/criteria/basic/BasicCriteriaUsageTest.java
===================================================================
---
core/trunk/entitymanager/src/test/java/org/hibernate/ejb/criteria/basic/BasicCriteriaUsageTest.java 2009-08-17
19:08:40 UTC (rev 17339)
+++
core/trunk/entitymanager/src/test/java/org/hibernate/ejb/criteria/basic/BasicCriteriaUsageTest.java 2009-08-17
19:09:17 UTC (rev 17340)
@@ -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 @@
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 @@
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();
+ }
}
Show replies by date