我对Spring JPA / JPQL还是很陌生,但是尝试复制以下MySQL查询,但没有成功。我们无法克服看起来像语法错误的问题,它似乎是试图将COUNT()和LOWER()的使用组合起来的功能。
MySQL(有效)是:
select lower(sdetail_cvalue) as stringValue,
count(lower(sdetail_cvalue)) as stringValueCount
from <someTable>
where sdetail_cfield not like <someValue>
and sdetail_cfield not like <someOtherValue>
and sdetail_cfield not like <someOtherOtherValue>
group by stringValue
order by stringValueCount desc
和我正在尝试的相应JPQL是
SELECT new <searchResult>
(lower(sd.searchText) as searchText,
COUNT(lower(sd.searchText)) as searchTextOccurrenceCount)
FROM <someTable> sd
WHERE sd.searchType not like <someValue>
AND sd.searchType not like <someOtherValue>
AND sd.searchType not like <someOtherOtherValue>
GROUP BY searchText
ORDER BY searchTextOccurrenceCount DESC
但是在执行时收到以下错误消息
Caused by: java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: expecting CLOSE, found '(' near line 1, column 128 [SELECT new <REDACTED> (lower(sd.searchText) as searchText, COUNT(lower(sd.searchText)) as searchTextOccurrenceCount) FROM <someTable> sd WHERE sd.searchType not like <someValue> AND sd.searchType not like <someOtherValue> AND sd.searchType not like <someOtherOtherValue> GROUP BY searchText ORDER BY searchTextOccurrenceCount DESC]
我已经编辑了上面的部分内容,但是列号(即128)是指“(”在“ lower”和“ sd”之间。
我们已经尝试了各种方法来解构查询以查明问题,这似乎是我们使用了复合COUNT(LOWER())构造。有没有人有使用JPQL成功实现这样的事情的示例?...预先感谢。
您是否考虑过将nativeQuery = true
添加到@Query
注释中?