开发者社区 问答 正文

涉及COUNT()和LOWER()的Spring JPA查询

已解决

我是Spring JPA / JPQL的新手,但是尝试复制以下MySQL查询,但没有成功。我们无法克服看起来像语法错误的问题,该语法错误似乎是试图将COUNT()和LOWER()的使用组合在一起的功能。

MySQL(有效)是:

select lower(sdetail_cvalue) as stringValue, count(lower(sdetail_cvalue)) as stringValueCount from where sdetail_cfield not like and sdetail_cfield not like and sdetail_cfield not like group by stringValue order by stringValueCount desc

而我正在尝试的相应JPQL是

SELECT new (lower(sd.searchText) as searchText, COUNT(lower(sd.searchText)) as searchTextOccurrenceCount) FROM sd WHERE sd.searchType not like AND sd.searchType not like AND sd.searchType not like 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 (lower(sd.searchText) as searchText, COUNT(lower(sd.searchText)) as searchTextOccurrenceCount) FROM sd WHERE sd.searchType not like AND sd.searchType not like AND sd.searchType not like GROUP BY searchText ORDER BY searchTextOccurrenceCount DESC]

我已对上面的部分进行了编辑,但是列号(即128)是指“(”在“ lower”和“ sd”之间。

我们尝试了各种方法来解构查询以查明问题,这似乎是我们使用了复合COUNT(LOWER())构造。有没有人有使用JPQL成功实现这样的事情的示例?...预先感谢。

展开
收起
保持可爱mmm 2019-12-12 15:41:34 553 分享 版权
1 条回答
写回答
取消 提交回答
  • 采纳回答

    你有没有想过将nativeQuery = true你的@Query注释?

    问题来源于stack overflow

    2019-12-12 15:41:53
    赞同 展开评论