【问题标题】:Spring JPA query involving COUNT() and LOWER()涉及 COUNT() 和 LOWER() 的 Spring JPA 查询
【发布时间】:2020-04-05 02:31:18
【问题描述】:

我对 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 成功实现这样的事情的例子?...在​​此先感谢。

【问题讨论】:

    标签: mysql spring spring-data-jpa jpql


    【解决方案1】:

    您是否考虑过将nativeQuery = true 添加到您的@Query 注释中?

    【讨论】:

      猜你喜欢
      • 2016-07-26
      • 1970-01-01
      • 2023-02-21
      • 2011-01-04
      • 2016-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多