我有一个像这样的查询:
select name, trunc( date, 'MM' ), sum( number )
from t$mytable
group by name, trunc( date, 'MM' );
它运行完美,并给出了预期的结果。我在 JPA 标准 API 中实现了完全相同的查询:
CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
CriteriaQuery criteriaQuery = criteriaBuilder.createQuery( pClass );
Metamodel metamodel = em.getMetamodel();
EntityType entityType_ = metamodel.entity( pClass );
Root<?> root = criteriaQuery.from( pClass );
Expression pathName = root.get( entityType_.getSingularAttribute( "name" ));
Path pathDate = root.get( entityType_.getSingularAttribute( "date" ) );
Expression<Date> pathTruncatedDate = criteriaBuilder.function("trunc", Date.class , pathDate, 'MM');
Expression pathNumber = criteriaBuilder.sum( root.get( entityType_.getSingularAttribute("number") ));
criteriaQuery.select( criteriaBuilder.array( pathName, pathTruncatedDate, pathNumber) );
List<Expression> groupBy = new ArrayList<Expression>();
groupBy.add( pathName);
groupBy.add( pathTruncatedDate);
criteriaQuery.groupBy( groupBy );
TypedQuery<Object[]> q = em.createQuery( criteriaQuery );
<-- 令人惊讶的是,我通过运行它得到了 ORA-00979: not a GROUP BY 表达式。是什么引起了这个问题,我猜一切都在正确的地方。非常感谢您提前!