我需要有关此错误的帮助:java.lang.NoSuchMethodError

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/308555/
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-10-29 11:52:57  来源:igfitidea点击:

I need help with this error: java.lang.NoSuchMethodError

javahibernatereflectionnosuchmethoderror

提问by Ionut

I have this Java code (JPA):

我有这个 Java 代码(JPA):

String queryString = "SELECT b  , sum(v.votedPoints) as votedPoint " +
                       " FROM Bookmarks b  " +
                         " LEFT OUTER JOIN Votes v " +
                           " on (v.organizationId = b.organizationId) " + 
                         "WHERE b.userId = 101  " + 
                         "GROUP BY b.organizationId " +
                         "ORDER BY votedPoint ascending ";
EntityManager em = getEntityManager();
Query query = em.createQuery(queryString);
query.setFirstResult(start);
query.setMaxResults(numRecords);
List results = query.getResultList();

I don't know what is wrong with my query because it gives me this error:

我不知道我的查询有什么问题,因为它给了我这个错误:

java.lang.NoSuchMethodError: org.hibernate.hql.antlr.HqlBaseParser.recover(Lantlr/RecognitionException;Lantlr/collections/impl/BitSet;)V
        at org.hibernate.hql.antlr.HqlBaseParser.fromJoin(HqlBaseParser.java:1802)
        at org.hibernate.hql.antlr.HqlBaseParser.fromClause(HqlBaseParser.java:1420)
        at org.hibernate.hql.antlr.HqlBaseParser.selectFrom(HqlBaseParser.java:1130)
        at org.hibernate.hql.antlr.HqlBaseParser.queryRule(HqlBaseParser.java:702)
        at org.hibernate.hql.antlr.HqlBaseParser.selectStatement(HqlBaseParser.java:296)
        at org.hibernate.hql.antlr.HqlBaseParser.statement(HqlBaseParser.java:159)
        at org.hibernate.hql.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:271)
        at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:180)
        at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:134)
        at org.hibernate.engine.query.HQLQueryPlan.(HQLQueryPlan.java:101)
        at org.hibernate.engine.query.HQLQueryPlan.(HQLQueryPlan.java:80)
        at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:94)
        at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:156)
        at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:135)
        at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1650)

Thanks.

谢谢。

回答by jabley

You definitely have an issue with the version of hibernate and ANTLR jars that you are using. The recover method wasn't present in the ANTLR Parser class until version 2.7.6? If you are using an earlier version of ANTLR, such as 2.7.2, then you will see this problem.

您使用的 hibernate 和 ANTLR jar 版本肯定有问题。在 2.7.6 版之前,ANTLR Parser 类中不存在恢复方法?如果您使用的是早期版本的 ANTLR,例如 2.7.2,那么您会看到这个问题。

Using maven can cause this sort of situation, where you depend on Hibernate and its transitive dependencies, but something 'closer'; e.g. Struts; providers a different, earlier version of ANTLR and that earlier version gets resolved in your application.

使用 maven 会导致这种情况,在这种情况下,您依赖 Hibernate 及其可传递的依赖项,但“更接近”;例如支柱;提供一个不同的、早期版本的 ANTLR,该早期版本在您的应用程序中得到解决。

If you can provide the version of jars involved, we would be able to help some more. Once you have fixed the issue with the jar versions, you should get a more revealing error message which shows what is wrong with your HQL expression.

如果您能提供所涉及的 jar 版本,我们将能够提供更多帮助。一旦您修复了 jar 版本的问题,您应该会收到一条更能说明问题的错误消息,其中显示了您的 HQL 表达式出了什么问题。

回答by Chris Kimpton

Stab in the dark - Are you sure you have a consistent set of jars - perhaps you need to get the antlr jar that comes with the hibernate distribution you are using...

在黑暗中刺伤 - 你确定你有一套一致的罐子 - 也许你需要得到你正在使用的休眠发行版附带的 antlr 罐子......

回答by Ionut

I've found the problem: because this is a native query, java classes for this 2 tables must have some special attributes:
in Bookmarks.java class

我发现了问题:因为这是一个本地查询,所以这 2 个表的 java 类必须有一些特殊的属性:
在 Bookmarks.java 类中

@OneToMany(mappedBy = "bookmarkId")
private Collection votesCollection;
并在 Votes.java 类中

@JoinColumn(name = "bookmark_id", referencedColumnName = "bookmark_id")
@ManyToOne
[private Bookmarks bookmarkId;

and i have also changed the query to work

我也改变了查询工作


tring queryString = "SELECT b, sum(v.votedPoints) " +
                          "FROM Bookmarks b  " +
                          "LEFT OUTER JOIN b.votesCollection v " + 
                          "WHERE b.userId = 101  " + 
                          "GROUP BY b.organizationId " +
                          "ORDER BY sum(v.votedPoints) asc ";

thanks for the help

谢谢您的帮助

回答by nicerobot

The query seems to be invalid unless it's an artifact of formatting.

该查询似乎无效,除非它是格式化的产物。

I think you meant this:

我想你的意思是:

Select b, ...

Select b, ...

to be:

成为:

Select b.organizationId, ...

Select b.organizationId, ...

??

??

回答by VonC

May be you have some double-quotes "missing or which should be doubled in your HQL.

可能是您"缺少一些双引号,或者应该在您的 HQL 中加倍。

Illustration here.

插图在这里

Or you miss some simple quotes as illustrated there

或者你会错过一些简单的行情如图所示

回答by Ionut

I have the consistent set of jars because simple queries like this one

我有一组一致的罐子,因为像这样的简单查询

"SELECT b  FROM table_name b  WHERE b.userId = 102 " 

are working. I have verified all double quotes and everything is alright.

正在工作。我已经验证了所有双引号,一切正常。

My database is: mysql, and I use jpa to connect to it. I don't know what is causing the problem. Maybe this type of join, i don't know

我的数据库是:mysql,我用jpa连接。我不知道是什么导致了问题。也许这种类型的连接,我不知道

回答by Gowri

Er, isnt your query trying to select b which is a table alias and thats not allowed as far as I know.

呃,你的查询不是试图选择 b 这是一个表别名,据我所知这是不允许的。

回答by JesperE

I'd probably guess that something is going wrong with your query, because the method HqlBaseParser fails to lookup is called recover(RecognitionException, Bitset). Perhaps this query fails for some reason the other simpler queries don't (and the NoSuchMethod exception is thrown when attempting to recover from that error).

我可能猜测您的查询出了问题,因为 HqlBaseParser 无法查找的方法被称为recover(RecognitionException, Bitset)。也许这个查询由于某种原因而失败,其他更简单的查询没有(并且在尝试从该错误中恢复时抛出 NoSuchMethod 异常)。

java.lang.NoSuchMethodError: org.hibernate.hql.antlr.HqlBaseParser.recover(Lantlr/RecognitionException;Lantlr/collections/impl/BitSet;)V

回答by nicerobot

Your query is still wrong. Maybe it works with your driver/db but it isn't standard SQL. You should be selecting b.*or b.organizationId.

您的查询仍然是错误的。也许它适用于您的驱动程序/数据库,但它不是标准的 SQL。您应该选择b.*b.organizationId