java spring jpa @Query 错误,期待关闭,发现 '(' 附近

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36475872/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 01:30:20  来源:igfitidea点击:

spring jpa @Query error, expecting CLOSE, found '(' near

javaspringspring-dataspring-data-jpa

提问by levi

I have following JPQL:

我有以下 JPQL:

@Query("SELECT su.id, su.nameCn, count(b.id), avg(s.rate), count(concat(b.id, '@', s.user.id)) "
            + "FROM S su, B b, S s where b.st.id = su.id and s.bd.id = b.id and su.mt.id = ?1 group by su.id")

When I add concat(b.id, '@', s.user.id), it shows me:

当我添加时concat(b.id, '@', s.user.id),它向我显示:

org.hibernate.hql.internal.ast.QuerySyntaxException: expecting CLOSE, found '(' near line 1, at org.hibernate.hql.internal.ast.QuerySyntaxException.convert(QuerySyntaxException.java:74) ~[hibernate-core-5.1.0.Final.jar:5.1.0.Final] at org.hibernate.hql.internal.ast.ErrorCounter.throwQueryException(ErrorCounter.java:91) ~[hibernate-core-5.1.0.Final.jar:5.1.0.Final] at org.hibernate.hql.internal.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:288) ~[hibernate-core-5.1.0.Final.jar:5.1.0.Final] at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:187) ~[hibernate-core-5.1.0.Final.jar:5.1.0.Final] at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:142) ~[hibernate-core-5.1.0.Final.jar:5.1.0.Final] at org.hibernate.engine.query.spi.HQLQueryPlan.(HQLQueryPlan.java:115) ~[hibernate-core-5.1.0.Final.jar:5.1.0.Final] at org.hibernate.engine.query.spi.HQLQueryPlan.(HQLQueryPlan.java:76) ~[hibernate-core-5.1.0.Final.jar:5.1.0.Final] at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:150) ~[hibernate-core-5.1.0.Final.jar:5.1.0.Final] at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:302) ~[hibernate-core-5.1.0.Final.jar:5.1.0.Final] at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:240) ~[hibernate-core-5.1.0.Final.jar:5.1.0.Final] at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1907) ~[hibernate-core-5.1.0.Final.jar:5.1.0.Final] at org.hibernate.jpa.spi.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:291) ~[hibernate-entitymanager-5.1.0.Final.jar:5.1.0.Final]

org.hibernate.hql.internal.ast.QuerySyntaxException:期待关闭,在 org.hibernate.hql.internal.ast.QuerySyntaxException.convert(QuerySyntaxException.java:74) ~[hibernate-core- 5.1.0.Final.jar:5.1.0.Final] 在 org.hibernate.hql.internal.ast.ErrorCounter.throwQueryException(ErrorCounter.java:91) ~[hibernate-core-5.1.0.Final.jar:5.1 .0.Final] 在 org.hibernate.hql.internal.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:288) ~[hibernate-core-5.1.0.Final.jar:5.1.0.Final] 在 org.hibernate .hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:187) ~[hibernate-core-5.1.0.Final.jar:5.1.0.Final] 在 org.hibernate.hql.internal.ast.QueryTranslatorImpl。编译(QueryTranslatorImpl.java:142)~[hibernate-core-5.1.0.Final.jar:5.1.0.Final] 在 org.hibernate.engine.query.spi。HQLQueryPlan.(HQLQueryPlan.java:115) ~[hibernate-core-5.1.0.Final.jar:5.1.0.Final] 在 org.hibernate.engine.query.spi.HQLQueryPlan.(HQLQueryPlan.java:76) ~ [hibernate-core-5.1.0.Final.jar:5.1.0.Final] 在 org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:150) ~[hibernate-core-5.1.0. Final.jar:5.1.0.Final] 在 org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:302) ~[hibernate-core-5.1.0.Final.jar:5.1.0.Final] 在 org. hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:240) ~[hibernate-core-5.1.0.Final.jar:5.1.0.Final] 在 org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1907 ) ~[hibernate-core-5.1.0.Final.jar:5.1.0.Final] 在 org.hibernate.jpa.spi.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:291)~[休眠实体管理器-5.1.0.Final.jar:5.1.0.Final]

回答by OrangeDog

expecting CLOSE, found (

期待关闭,找到 (

The term CLOSE here refers to a closing bracket, ).

这里的术语 CLOSE 指的是一个右括号,)

It would appear that the query parser does not support calling another function inside the count(...).

查询解析器似乎不支持在count(...).

In other words, the syntax error is here:

换句话说,语法错误在这里:

SELECT su.id, su.nameCn, count(b.id), avg(s.rate), count(concat(b.id, '@', s.user.id))
                                                               ^

Perhaps you mean to be doing some kind of join, and counting the rows there?

也许您的意思是进行某种连接,并计算那里的行数?

回答by Shaan

Problem is using both count and concatenate function together.

问题是同时使用计数和连接函数。

If you write your query without count() like below, should work. This is just for testing to know who is causing the issue exactly.

如果你像下面这样在没有 count() 的情况下编写查询,应该可以工作。这仅用于测试以了解究竟是谁导致了问题。

Try writing query different way using count and concatenate seperatly.

尝试使用计数和连接单独编写查询。

...concat(b.id, '@', s.user.id)....