我在存储库中有一个jpql查询,相当于下面的mysql查询:
SELECT DISTINCT ji.* FROM tracker_job_item AS ji
JOIN tracker_work AS w ON ji.id=w.tracker_job_item_id
JOIN tracker_work_quantity AS wq on w.id=wq.tracker_work_id
WHERE w.work_type = 'CUTTING' AND ji.is_finished=0
GROUP BY wq.tracker_work_id
HAVING ji.quantity != SUM(wq.received_quantity)
mysql版本运行得很好,但jpql等效版本给出了一个例外: Unknown column 'jobitem0_.quantity' in 'having clause'
jpql查询如下:
@Query("select distinct ji from JobItem ji" +
" join Work w on ji.id=w.jobItem.id" +
" join WorkQuantity wq on w.id=wq.work.id" +
" where w.workType='CUTTING' and ji.isFinished=false and ji.jobItemName like %:search%" +
" group by ji.id" +
" having ji.quantity != sum(wq.receivedQuantity)")
Page<JobItem> findAllActiveCuttingJobs(Pageable pageable, @Param("search") String search);
请帮助我,为什么我得到的错误,即使该领域 quantity
存在于 JobItem
.
1条答案
按热度按时间vhipe2zx1#
不能引用having子句中不在groupby子句中的列,在jpa和sql中(通常至少是这样)。看起来mysql让你逍遥法外,但jpa却没有。
请看这里:
http://learningviacode.blogspot.co.uk/2012/12/group-by-and-having-clauses-in-hql.html