Hibernate 3.5.x: NoSuchMethodError: javax.persistence.OneToMany.orphanRemoval

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

Hibernate 3.5.x: NoSuchMethodError: javax.persistence.OneToMany.orphanRemoval

javahibernate

提问by Marty Pitt

I'm trying to upgrade to Hibernate 3.5.3-FINAL.

我正在尝试升级到 Hibernate 3.5.3-FINAL。

When running my unit tests, I now receive the following exception:

在运行我的单元测试时,我现在收到以下异常:

java.lang.NoSuchMethodError: javax.persistence.OneToMany.orphanRemoval()Z   
at org.hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.java:1837)

My classpath contains the following JAR's:

我的类路径包含以下 JAR:

From the hibernate dist:

从休眠区:

antlr-2.7.6.jar
commons-collections-3.1.jar
dom4j-1.6.1.jar
javassist-3.9.0.GA.jar
jta-1.1.jar
slf4j-api-1.5.8.jar

cglib-2.2.jar
hibernate-jpa-2.0-api-1.0.0.Final.jar
hibernate3.jar

Other jars:

其他罐子:

blazeds-common-3.2.0.3978.jar
blazeds-core-3.2.0.3978.jar
blazeds-opt-3.2.0.3978.jar
blazeds-proxy-3.2.0.3978.jar
blazeds-remoting-3.2.0.3978.jar
commons-lang-2.3.jar
dbunit-2.4.7.jar
ejb3-persistence.jar // Note, I've tried excluding this, but I get different errors
guava-r05.jar
hsqldb-1.8.0.7.jar
junit-4.1.jar
lambdaj-2.0-with-dependencies.jar
log4j-1.2.14.jar
mockito-all-1.8.0.jar
persistence-api-1.0.jar
spring-security-core-2.0.0.jar
spring.jar
sqljdbc.jar

I've researched this, and I find answersthat state my webserver must be JPA2 Compliant:

我对此进行了研究,我发现答案表明我的网络服务器必须符合 JPA2:

Unfortunately if your app server is not JPA 2 compliant, you are likely to be out of luck

不幸的是,如果您的应用服务器不符合 JPA 2 标准,您可能会走运

This project is a library, not a webserver project. (Although it is eventually deployed to a webserver, I'm simply running unit tests here)

这个项目是一个库,而不是一个网络服务器项目。(虽然它最终被部署到一个网络服务器,我只是在这里运行单元测试)

What am I missing?

我错过了什么?

<indulgent_rant>

<indulgent_rant>

As a sidenote, it's very frustrating that every time I upgrade Hibernate, I find myself spending hours researching conflicting jars for MethodNotFoundExceptionor ClassNotFoundException's. There must be a simpler way?

作为旁注,每次升级 Hibernate 时,我都会发现自己花费数小时研究MethodNotFoundExceptionorClassNotFoundException的冲突 jar,这非常令人沮丧。一定有更简单的方法吗?

I thought for sure that with the announcement they were merging the modules back to a single core project, that these conflicts would go away?

我确信,随着他们宣布将模块合并回单个核心项目,这些冲突会消失吗?

</indulgent_rant>

</indulgent_rant>

采纳答案by Pascal Thivent

As pointed out by Timo, remove persistence-api-1.0.jar.

正如 Timo 所指出的,删除persistence-api-1.0.jar.

Just in case, here are the dependencies I'm using:

以防万一,这里是我正在使用的依赖项:

org.hibernate:hibernate-entitymanager:jar:3.5.3-Final:compile
+- org.hibernate:hibernate-core:jar:3.5.3-Final:compile
|  +- antlr:antlr:jar:2.7.6:compile
|  +- commons-collections:commons-collections:jar:3.2:compile
|  +- dom4j:dom4j:jar:1.6.1:compile
|  |  \- xml-apis:xml-apis:jar:1.0.b2:compile
|  \- javax.transaction:jta:jar:1.1:compile
+- org.hibernate:hibernate-annotations:jar:3.5.3-Final:compile
|  \- org.hibernate:hibernate-commons-annotations:jar:3.2.0.Final:compile
+- cglib:cglib:jar:2.2:compile
|  \- asm:asm:jar:3.1:compile
+- javassist:javassist:jar:3.9.0.GA:compile
\- org.hibernate.javax.persistence:hibernate-jpa-2.0-api:jar:1.0.0.Final:compile

I'm getting them from this single declaration in my pom.xml:

我从我的这个单一声明中得到它们pom.xml

<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-entitymanager</artifactId>
  <version>3.5.3-Final</version>
</dependency>

This should somehow answer your indulgent rant (if I rephrase: use Maven - or know what you're doing).

这应该以某种方式回答你放纵的咆哮(如果我改写:使用 Maven - 或者知道你在做什么)。

回答by Timo Westk?mper

Try replacing persistence-api-1.0.jar with a JPA 2.0 jar.

尝试用 JPA 2.0 jar 替换persistence-api-1.0.jar。

回答by Manish Jaiswal

Remove these files from project lib location persistence-api-1.0.jar ejb3-persistence.jar

从项目库位置 persistence-api-1.0.jar 中删除这些文件 ejb3-persistence.jar

add only javax.persistence-2.0.0.jar

只添加 javax.persistence-2.0.0.jar

it contains all the required fields and atrributes.

它包含所有必需的字段和属性。

try this it'll work for your problem.

试试这个它会解决你的问题。

Manish Jaiswal

马尼什·贾斯瓦尔

回答by Amit P

adding javax.persistence-2.0.0.jar should work fine...

添加 javax.persistence-2.0.0.jar 应该可以正常工作...

回答by Matt Campbell

Discovered a fix for the issue at least with what I am working with: IBM WebSphere 7 and MyEclipse Blue (Spring), or Bling, ver. 10.6. I created an application that has dependencies on other projects, one of these using Hibernate 3.6.3, requiring JPA 2.0. IBM WS loads JPA 1.0 to support itself ahead of JPA 2.0 and resolves all references to JPA to ver. 1.0. In the case of IBM WS, this causes the error reported above. The only fix I have come across since now includes adding JPA 2.0 as a shared library, then setting the server to load that library ahead of its native classes, similar to the method discussed here:

至少使用我正在使用的工具发现了该问题的修复程序:IBM WebSphere 7 和 MyEclipse Blue (Spring),或 Bling,ver。10.6. 我创建了一个依赖于其他项目的应用程序,其中一个使用 Hibernate 3.6.3,需要 JPA 2.0。IBM WS 加载 JPA 1.0 以在 JPA 2.0 之前支持自身,并将对 JPA 的所有引用解析为 ver。1.0. 在 IBM WS 的情况下,这会导致上面报告的错误。从现在起我遇到的唯一修复包括将 JPA 2.0 添加为共享库,然后设置服务器在其本机类之前加载该库,类似于此处讨论的方法:

http://www.mkyong.com/websphere/websphere-7-javaxpersistenceonetomany-orphanremoval-error/

http://www.mkyong.com/websphere/websphere-7-javaxpersistenceonetomany-orphanremoval-error/

But our server admins will not allow changes to the server-wide classloader policy. So I still needed to find a way to get my project to use JPA 2.0 and not 1.0. I tried a lot of things but finally went for a Hail Mary and it worked.

但是我们的服务器管理员不允许更改服务器范围的类加载器策略。所以我仍然需要找到一种方法让我的项目使用 JPA 2.0 而不是 1.0。我尝试了很多东西,但最终选择了冰雹玛丽,它奏效了。

The fix is to add the JPA 2.0 .jar file to your .war and .ear projects and then edit the Class-Path line in the manifest file in both your .war and .ear projects to point to the .jar files. It seems they must be in both or the approach will not work, so the JPA 2.0 .jar has to be in both the .war and .ear.

修复方法是将 JPA 2.0 .jar 文件添加到 .war 和 .ear 项目,然后编辑 .war 和 .ear 项目清单文件中的 Class-Path 行以指向 .jar 文件。似乎它们必须同时存在,否则该方法将不起作用,因此 JPA 2.0 .jar 必须同时存在于 .war 和 .ear 中。

In your app (.war project), add hibernate-jpa-2.0-api-1.0.0.Final.jar to the project root. MyEclipse Blue will show it as a file at that location and also listed under "Referenced Libraries" in the Package Explorer view. Then open the manifest file at /WebRoot/META-INF/MANIFEST.MF and make sure the Class-Path line reads:

在您的应用程序(.war 项目)中,将 hibernate-jpa-2.0-api-1.0.0.Final.jar 添加到项目根目录。MyEclipse Blue 将在该位置将其显示为文件,并在 Package Explorer 视图中的“Referenced Libraries”下列出。然后在 /WebRoot/META-INF/MANIFEST.MF 打开清单文件并确保 Class-Path 行显示:

Class-Path: /hibernate-jpa-2.0-api-1.0.0.Final.jar

In your .ear project, add hibernate-jpa-2.0-api-1.0.0.Final.jar to folder 'lib' at your project root. If there is no 'lib' folder there, create one. Then in /META-INF/MANIFEST.MF, make sure the Class-Path line reads:

在您的 .ear 项目中,将 hibernate-jpa-2.0-api-1.0.0.Final.jar 添加到项目根目录的“lib”文件夹中。如果那里没有“lib”文件夹,请创建一个。然后在 /META-INF/MANIFEST.MF 中,确保 Class-Path 行显示:

Class-Path: /lib/hibernate-jpa-2.0-api-1.0.0.Final.jar

Make sure all files are saved and do a clean build. Then export the .ear and deploy it as usual by the Integrated Solutions Console. But there is another thing you must do before the change will work; it does require changing the class-load policy for the app, but not the server. You are much more likely to get your WAS admin to go for that than changing the classloader policy for the entire server. To make the necessary change, after installing the .ear file, follow the left-hand nav bar click trail "Applications\Application Types\Websphere enterprise applications"and click on the name of your application, then click "Class loading and update detection". Then select "Classes loaded with local class loader first (parent last)"under "Class loader order", then click Apply, then Save. Now back at click trail "Applications\Application Types\Websphere enterprise applications", either start or restart your application. Test out your app and hopefully you will be good to go.

确保所有文件都已保存并进行干净的构建。然后通过集成解决方案控制台导出 .ear 并像往常一样部署它。但是,在更改生效之前,您还必须做另一件事;它确实需要更改应用程序的类加载策略,而不是服务器。与更改整个服务器的类加载器策略相比,您更有可能让 WAS 管理员这样做。要进行必要的更改,请在安装 .ear 文件后,按照左侧导航栏的单击轨迹"Applications\Application Types\Websphere enterprise applications"并单击您的应用程序名称,然后单击“类加载和update detection"。然后选择"Classes loaded with local class loader first (parent last)""Class loader order",然后单击Apply,然后Save。现在回到单击 trail "Applications\Application Types\Websphere enterprise applications",启动或重新启动您的应用程序。测试您的应用程序,希望您一切顺利。

Final note:Evey time you re-deploy your .ear file, you will need to set the classloader option I just discussed. The preference is not retained between deployments.

最后一点:每次重新部署 .ear 文件时,您都需要设置我刚刚讨论的类加载器选项。在部署之间不保留首选项。

回答by Kabron

Setting your server's application classloader policy to parent last will resolve this problem so long as you ensure the hibernate-jpa-2.0-api jar file is included in the EAR. If this is possible in your environment it's a much easier solution.

只要您确保 EAR 中包含 hibernate-jpa-2.0-api jar 文件,将服务器的应用程序类加载器策略设置为 parent last 将解决此问题。如果这在您的环境中是可能的,那么这是一个更简单的解决方案。

Tested on WebSphere 7.

在 WebSphere 7 上测试。