簡體   English   中英

使用orderBy()時,Hibernate Criteria API會生成帶有聚合的無效SQL

[英]Hibernate Criteria API produces invalid SQL with aggregates when using orderBy()

我在一個使用Spring Data和Hibernate的Criteria API的項目中使用JPA。 使用JpaSpecificationExecutor我可以通過調用Page<EventPost> findAll(Specification<EventPost> specification, Pageable pageable);來創建查詢,使我可以使用Specification在存儲庫中同時使用過濾和Page<EventPost> findAll(Specification<EventPost> specification, Pageable pageable); 順利。

我現在遇到的問題是,如果沒有休眠生成像這樣的無效查詢,我將無法對結果進行排序:

select count(eventpost0_.event_id) as col_0_0_ from event_post eventpost0_ where eventpost0_.category_event_category_id=? order by eventpost0_.createDate desc

顯然,Hibernate必須在發出實際finder查詢之前對行進行計數,然后將我的Criteria中的order by子句錯誤地添加到select count(*)語句中。

這是我在日志中看到的逐字記錄:

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

這就是我創建查詢的方式

@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()]));
}

當我刪除query.orderBy(cb.desc(root.get("createDate"))); ,一切正常。 任何想法在這里可能出什么問題嗎?

版本如下:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM