java 休眠:“ClassNotFoundException:javax.persistence.spi.ProviderUtil”

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

Hibernate: "ClassNotFoundException: javax.persistence.spi.ProviderUtil"

javahibernateormjpaannotations

提问by Steve

My application is trying to use Hibernate annotations. I've gotten the 3.5.0 version of Hibernate installed, but when I try to load my app I get the following exception:

我的应用程序正在尝试使用 Hibernate 注释。我已经安装了 3.5.0 版本的 Hibernate,但是当我尝试加载我的应用程序时,出现以下异常:

junit.framework.AssertionFailedError: Exception in constructor: testSubscriber (java.lang.NoClassDefFoundError: javax/persistence/spi/ProviderUtil
    at java.lang.Class.getDeclaredConstructors0(Native Method)
    at java.lang.Class.privateGetDeclaredConstructors(Class.java:2389)
    at java.lang.Class.getConstructor0(Class.java:2699)
    at java.lang.Class.newInstance0(Class.java:326)
    at java.lang.Class.newInstance(Class.java:308)
    at javax.persistence.Persistence.findAllProviders(Persistence.java:79)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:48)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:33)
    at com.upod.dao.SubscriberDAOTest.<init>(SubscriberDAOTest.java:32)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at junit.framework.TestSuite.createTest(TestSuite.java:58)
    at junit.framework.TestSuite.addTestMethod(TestSuite.java:280)
    at junit.framework.TestSuite.<init>(TestSuite.java:140)
    at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestLoader.getTest(JUnit3TestLoader.java:102)
    at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestLoader.loadTests(JUnit3TestLoader.java:59)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:445)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Caused by: java.lang.ClassNotFoundException: javax.persistence.spi.ProviderUtil
    at java.net.URLClassLoader.run(URLClassLoader.java:200)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:319)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:330)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:254)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:399)
    ... 22 more
)
    at junit.framework.Assert.fail(Assert.java:47)
    at junit.framework.TestSuite.runTest(TestSuite.java:90)
    at junit.framework.TestCase.runBare(TestCase.java:130)
    at junit.framework.TestResult.protect(TestResult.java:106)
    at junit.framework.TestResult.runProtected(TestResult.java:124)
    at junit.framework.TestResult.run(TestResult.java:109)
    at junit.framework.TestCase.run(TestCase.java:120)
    at junit.framework.TestSuite.runTest(TestSuite.java:230)
    at junit.framework.TestSuite.run(TestSuite.java:225)
    at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)

Apprently, I'm missing something, or I have incompatible jars again, but for the life of me I can't figure out where the problem is. I've got the hibernate3.jar, ejb3-persistence.jar, and all of the dependencies specified in the Hibernate docs, yet I still get this.

显然,我遗漏了一些东西,或者我的罐子又不兼容,但对于我的一生,我无法弄清楚问题出在哪里。我已经得到了 hibernate3.jar、ejb3-persistence.jar 和 Hibernate 文档中指定的所有依赖项,但我仍然得到了这个。

采纳答案by dustmachine

Do you have the correct JPA jar in your classpath? since youre error is: java.lang.NoClassDefFoundError: javax/persistence/spi/ProviderUtil

您的类路径中是否有正确的 JPA jar?因为你的错误是:java.lang.NoClassDefFoundError: javax/persistence/spi/ProviderUtil

EDIT

编辑

Y'know, before we go further trying to figure this out, we should ask if it's really necessary to be using 3.5.0 (since at the time of this writing, it's 3.5.0-Beta-2). If all you're looking for is Annotations, using Hibernate Core 3.3.2 is enough to allow you to use Hibernate Annotations 3.4.0.

你知道,在我们进一步尝试解决这个问题之前,我们应该问一下是否真的有必要使用 3.5.0(因为在撰写本文时,它是 3.5.0- Beta-2)。如果您只需要注解,那么使用 Hibernate Core 3.3.2 就足以让您使用 Hibernate Annotations 3.4.0。

IMHO, you might want to just back up a version to something "safe"

恕我直言,您可能只想将版本备份到“安全”

回答by Srini

If you are using Hibernate 3.5.x, you also need the following jar file.

如果您使用的是 Hibernate 3.5.x,则还需要以下 jar 文件。

hibernate-jpa-2.0-api-1.0.0-CR-1.jar

hibernate-jpa-2.0-api-1.0.0-CR-1.jar

I had the same problem and adding this fixed the issue.

我遇到了同样的问题,添加这个解决了这个问题。

回答by Pascal Thivent

I don't know what documentation you are referring to as you didn't post any link so I didn't check what it is saying but I think that you you are missing the jpa-apidependency that I can see in the pom.xmlof hibernate-parent-3.5.0-Beta-2(it provides javax.persistence.spi.ProviderUtil):

我不知道你指的是,你没有张贴任何联系,所以我没有检查它是什么说什么文档,但我认为你你缺少的jpa-api依赖关系,我可以在看的pom.xmlhibernate-parent-3.5.0-Beta-2(它提供javax.persistence.spi.ProviderUtil):

<dependency>
  <groupId>org.hibernate.java-persistence</groupId>
  <artifactId>jpa-api</artifactId>
  <version>2.0-cr-1</version>
</dependency>

You can get it from the JBoss Mavenrepository (it doesn't seem to be included in the bundle released on SourceForge). Time to start using maven :)

您可以从JBoss Maven存储库中获取它(它似乎没有包含在SourceForge 上发布的包中)。是时候开始使用 maven 了 :)