使用 Jboss EAP 6.1 在代码中使用 javax.persistence.JoinColumn.foreignKey 注释时出错

Error on using javax.persistence.JoinColumn.foreignKey annotation in code using Jboss EAP 6.1

我在我的代码中使用 javax.persistence.JoinColumn.foreignKey。该项目使用 WildFly 服务器运行良好,但是当我使用 Jboss EAP 6.1 时,出现以下错误:

java.lang.NoSuchMethodError: javax.persistence.JoinColumn.foreignKey()Ljavax/persistence/ForeignKey; org.hibernate.cfg.AnnotationBinder.bindManyToOne(AnnotationBinder.java:2884) org.hibernate.cfg.AnnotationBinder.bindOneToOne(AnnotationBinder.java:3051) org.hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.java:1839) org.hibernate.cfg.AnnotationBinder.processIdPropertiesIfNotAlready(AnnotationBinder.java:963) org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:796)

当我进一步调查这个问题时,我发现 JBoss 图书馆不支持加入列。

但是,我的项目库支持加入列,我打算在我的代码中使用这些库。但是 Jboos 库似乎覆盖了 class 路径中的库。

我在 jboss-deployment-structure.xml

中尝试了以下代码
<exclusions>
    <module name="javax.persistence.api"/>
    <module name="javaee.api"/> 
</exclusions> 

但是我遇到了以下错误

11:19:55,195 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-7) MSC000001: Failed to start service jboss.deployment.unit."myappl.war".PARSE: org.jboss.msc.service.StartException in service jboss.deployment.unit."myappl.war".PARSE: JBAS018733: Failed to process phase PARSE of deployment "myappl.war" Caused by: java.lang.IllegalStateException: JBWS021001: Unexpected element parsing handlers: handler-config

JBoss EAP 6.x 是 compliant with JPA 2.0 API while javax.persistence.ForeignKey 已添加到 JPA 2.1 中。

为了使其正常工作,您需要手动集成它。可以找到一个可能的解决方法 here

此问题的根本原因是 EAP 6.3.x 基于 Java EE 6 规范,而 JPA 2.0 是该规范的一部分。最新版本的 Hibernate (4.3.x) 带有 JPA 2.1 版,它是 Java EE 7 规范的一部分。理论上,要解决问题,排除jboss-deployment-structure.xml文件中的'org.hibernate'模块和'javax.persistence'就可以了,但并没有解决问题。根据 Red Hat 的说法,这是一个 BUG。

1) 一个解决方案是使用 EAP 6.3.x 发行版中的 Hibernate 版本。已解决,但应用程序无法保存到服务器库的版本。

2) 另一种解决方案是直接在分发中将 JPA 2.0 的版本更改为 2.1,位于 \JBOSS_DIST\modules\system\layers\base\javax\persistence\api\main。也解决了,但是这个解决方案是不可接受的,因为 EAP 中使用 Hibernate 版本 6.3.x 的应用程序可能会受到影响。

3)本来可以按照这个地址http://mariemjabloun.blogspot.com.br/2014/03/use-jboss-jpa-21-and-hibernate-43-on.html的步骤来的,但是特别费力地找了个办法。

4) 最后,经过多次搜索和尝试,在 RedHat 的网站上找到了解决问题的合理解决方案:https://access.redhat.com/solutions/404223。相信在以后的EAP版本中,应该会更正。