如何调试NoSuchMethod错误异常?org.hibernate.cfg.Configuration.addAnno



当我尝试在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

没有人有想法,可能有什么问题?有人可以提供一般建议,如何处理,调试这些晦涩的错误吗?

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

也可能是实现的同一个类是多个依赖项,所以谷歌它找出哪些jar包含它。 http://www.jarfinder.com/index.php/java/info/org.hibernate.cfg.Configuration

相关内容

最新更新