【问题标题】:How to debug NoSuchMethodError exception ? org.hibernate.cfg.Configuration.addAnnotatedClass(Ljava/lang/Class;)如何调试 NoSuchMethodError 异常? org.hibernate.cfg.Configuration.addAnnotatedClass(Ljava/lang/Class;)
【发布时间】:2013-10-13 01:21:13
【问题描述】:

当我尝试在 servlet 中初始化 Hibernate 4 SessionFactory 时:

Configuration config;
ServiceRegistry registry;
SessionFactory factory;
config = new Configuration();
config = config.addAnnotatedClass(Star.class); // <-- Exception here.

我得到一个例外:

SEVERE: Servlet.service() for servlet [hu.adamsan.store.TestHibernate] in context with path [/TestProject] threw exception [Servlet execution threw an exception] with root cause
java.lang.NoSuchMethodError: org.hibernate.cfg.Configuration.addAnnotatedClass(Ljava/lang/Class;)Lorg/hibernate/cfg/Configuration;
at hu.adamsan.store.TestHibernate.doGet(TestHibernate.java:75)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at 

当我在谷歌中查找时,我找不到解决方案,但是当与休眠版本不匹配时,会出现类似的错误。我做了一个类似的项目,具有完全相同的 maven 依赖项,使用相同的代码在 doGet 方法中初始化休眠,并且它有效。

maven 依赖:

mysql-connector-java 5.1.26
hibernate-core 4.2.6.Final
hibernate-validator 5.0.1.Final
commons-beanutils 1.8.3
commons-collections 3.2.1
log4j 1.2.17
slf4j-api 1.7.5

有谁知道,有什么问题吗? 有人可以提供一般性建议,如何处理和调试这些晦涩难懂的错误吗?

【问题讨论】:

    标签: eclipse hibernate maven exception servlets


    【解决方案1】:

    当您在外部库中获得NoSuchMethodError 时,通常是由于类路径上有相同依赖项的多个版本。这可能是由于传递依赖,因此仅通过查看项目的pom.xml 中的依赖来检测它是不够的。如果您执行mvn dependency:tree,它也会列出所有传递依赖项,因此请查找同一依赖项的多个版本。

    也有可能是同一个类实现了多个依赖,所以google一下看看哪些jar包包含它。 http://www.jarfinder.com/index.php/java/info/org.hibernate.cfg.Configuration

    【讨论】:

    • 谢谢。与此同时,我已经想通了。早些时候我尝试过hibernate 3,我已经将pom文件更改为hibernate 4,我认为它会处理jar文件。但它没有。当我查看 getServletContext().getRealPath() 给出的文件夹时,在 WEB-INF\lib 中有旧版本的 jars。
    • 即使在清理/重建项目之后,maven 不这样做是否正常?我真的必须从那里手动删除罐子吗?我认为 Maven 的重点是使这些事情自动化。我正在使用内置 eclipse jee 版本的 maven。我假设 dependency:tree 应该为 eclipse pom.xml/dependency 层次结构选项卡提供类似的输出。在这里我无法看到旧版本的罐子。那么,基本上,我怎样才能使这些依赖 jar 的处理自动化?
    • 我不确定我是否理解 100%,但 IDE 具有向类路径添加内容的机制(然后它在 maven 之外)。因此,如果从 IDE 运行时发生这种情况,则可能是 maven 依赖项与仅在 Eclipse 中添加到类路径的依赖项之间存在依赖冲突..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-01
    • 2020-09-06
    • 1970-01-01
    • 2014-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多