HHH-7216 'java.lang.IllegalStateException: No supertype found' thrown on query with COUNT aggregate function

This commit is contained in:
Strong Liu 2013-08-14 00:39:40 +08:00
parent 858c68364a
commit 4f2088856f
2 changed files with 33 additions and 1 deletions

View File

@ -115,7 +115,7 @@ public class AggregationFunction<T>
Expression argExpr = argExprs.get(0);
if (argExpr instanceof Root<?>) {
Root<?> root = (Root<?>)argExpr;
if (root.getModel().getIdType() != null) {
if (!root.getModel().hasSingleIdAttribute()) {
buffer.append('*');
return;
}

View File

@ -28,6 +28,7 @@ import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
@ -143,6 +144,37 @@ public class IdClassPredicateTest extends AbstractMetamodelSpecificTest {
em.close();
}
@Test
public void testCountIdClassAttributes(){
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Long> cq = cb.createQuery(Long.class);
Root<Widget> path = cq.from(Widget.class);
Expression<Long> countSelection = cb.count(path);
cq.select(countSelection);
Long count = em.createQuery(cq).getSingleResult();
// // Packaging arguments for use in query.
// List<String> divisions = new ArrayList<String>( );
// divisions.add( "NA" );
// divisions.add( "EU" );
//
// // Building the query.
// CriteriaBuilder criteria = em.getCriteriaBuilder( );
// CriteriaQuery<Widget> query = criteria.createQuery( Widget.class );
// Root<Widget> root = query.from( Widget.class );
//
// Predicate predicate = root.get( "division" ).in( divisions );
// query.where( predicate );
//
// // Retrieving query.;
// List<Widget> widgets = em.createQuery( query ).getResultList( );
// Assert.assertEquals( 4, widgets.size() );
em.getTransaction().commit();
em.close();
}
@Test
public void testDeclaredIdClassAttributes( ) {