[英]Hibernate Criteria API produces invalid SQL with aggregates when using orderBy()
I am using JPA in a project that uses Spring Data with Hibernate's Criteria API underneath. 我在一个使用Spring Data和Hibernate的Criteria API的项目中使用JPA。 Using
JpaSpecificationExecutor
I was able to create queries that would let me use filtering and paging at the same time in my repository using a Specification
, by just calling Page<EventPost> findAll(Specification<EventPost> specification, Pageable pageable);
使用
JpaSpecificationExecutor
我可以通过调用Page<EventPost> findAll(Specification<EventPost> specification, Pageable pageable);
来创建查询,使我可以使用Specification
在存储库中同时使用过滤和Page<EventPost> findAll(Specification<EventPost> specification, Pageable pageable);
without a hitch. 顺利。
The Problem that I have now, is that I can't order the results without hibernate generating invalid queries like this one: 我现在遇到的问题是,如果没有休眠生成像这样的无效查询,我将无法对结果进行排序:
select count(eventpost0_.event_id) as col_0_0_ from event_post eventpost0_ where eventpost0_.category_event_category_id=? order by eventpost0_.createDate desc
Apparently, Hibernate has to count the rows before it issues the real finder query, and erroneously adds the order by clause from my Criteria to the select count(*)
statement. 显然,Hibernate必须在发出实际finder查询之前对行进行计数,然后将我的Criteria中的order by子句错误地添加到
select count(*)
语句中。
This is verbatim what I see in the logs: 这是我在日志中看到的逐字记录:
2016-09-05 09:22:36.987 [http-bio-8080-exec-4] DEBUG org.hibernate.SQL - select count(eventpost0_.event_id) as col_0_0_ from event_post eventpost0_ where eventpost0_.category_event_category_id=? order by eventpost0_.createDate desc
2016-09-05 09:22:36.991 [http-bio-8080-exec-4] WARN o.h.e.j.spi.SqlExceptionHelper - SQL Error: 0, SQLState: 42803
2016-09-05 09:22:36.992 [http-bio-8080-exec-4] ERROR o.h.e.j.spi.SqlExceptionHelper - ERROR: column "eventpost0_.createdate" must appear in the GROUP BY clause or be used in an aggregate function
Position: 133
And this is how I create my query 这就是我创建查询的方式
@Override
public Predicate toPredicate(Root<EventPost> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
Path<EventPost> category = root.get("category");
Path<Long> participants = root.get("participants");
Path<EventPostType> eventPostType = root.get("eventPostType");
final List<Predicate> predicates = new ArrayList<Predicate>();
if(criteria.getEventCategory()!=null){
predicates.add(cb.equal(category,criteria.getEventCategory()));
}
if(criteria.getParticipantsFrom()!=null){
predicates.add(cb.ge(participants,criteria.getParticipantsFrom()));
}else if(criteria.getParticipantsTo()!=null){
predicates.add(cb.lt(participants,criteria.getParticipantsTo()));
}
if(criteria.getEventPostType()!=null){
predicates.add(cb.equal(eventPostType,criteria.getEventPostType()));
}
query.orderBy(cb.desc(root.get("createDate")));
return cb.and(predicates.toArray(new Predicate[predicates.size()]));
}
When I remove query.orderBy(cb.desc(root.get("createDate")));
当我删除
query.orderBy(cb.desc(root.get("createDate")));
, everything works fine. ,一切正常。 Any Ideas what might be wrong here?
任何想法在这里可能出什么问题吗?
Versions are as follows: 版本如下:
PostgreSQL Database 9.5.3.0 with PostGIS
spring-orm:jar:4.2.5.RELEASE
spring-data-jpa:jar:1.9.4.RELEASE
hibernate-spatial:jar:4.3:compile
hibernate-core:jar:4.3.11.Final:compile
postgresql:jar:8.4-701.jdbc4:compile
postgis-jdbc:jar:1.5.2:compile
I have the following code which worked for me
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<T> cq = cb.createQuery(entity);
Root<T> root = cq.from(entity);
cq.orderBy(cb.desc(cb.sum(root.get(orderByString))));
// orderByString is string entity field that is being aggregated and which we want to put in orderby clause as well.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.