错误:Hibernate 4.3.5.final核心中的java.lang.NoSuchMethodError

时间:2014-08-18 19:34:55

标签: hibernate jpa jboss

有一个类似的问题:here但我没有使用Spring和JPA。此外,我已经尝试过解决方案并且没有成功。 Hibernate版本也不同。

问题: 一个 EAR项目 EJB 3.1 Hibernate 4.3.5.Final 没有JPA,使用 Hibernate Core(Mysql)

部署的项目已启动,部署中没有错误。

使用@ManytoOne映射创建了两个实体后,我收到了错误消息:

    java.lang.NoSuchMethodError: javax.persistence.JoinColumn.foreignKey()Ljavax/persistence/ForeignKey;
    org.hibernate.cfg.AnnotationBinder.bindManyToOne(AnnotationBinder.java:2881)
    org.hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.java:1795)
    org.hibernate.cfg.AnnotationBinder.processIdPropertiesIfNotAlready(AnnotationBinder.java:963)
    org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:796)
    org.hibernate.cfg.Configuration$MetadataSourceQueue.processAnnotatedClassesQueue(Configuration.java:3788)

(错误太长了,如果想要stackTrace,请告诉。)

以下是我创建的两个实体,然后出现错误:

        package br.gov.xyz.malotes.model;

        import javax.persistence.CascadeType;
        import javax.persistence.Column;
        import javax.persistence.Entity;
        import javax.persistence.GeneratedValue;
        import javax.persistence.Id;
        import javax.persistence.JoinColumn;
        import javax.persistence.ManyToOne;

        import org.hibernate.annotations.GenericGenerator;

        @Entity
        public class Usuario {
            @Id
            @GenericGenerator(name="gen",strategy="increment")
            @GeneratedValue(generator="gen")
            @Column(name = "id", unique = true, nullable = false, precision = 15, scale = 0)
            private int id;

            private String nome;

            private String login;

            private String senha;

            @ManyToOne//(cascade = CascadeType.ALL)  // TIRAR A CASCATA AQUI AGRANTE QUE A TABELA UNIDADE NAO SEJA ALTERADA SEM QUERER
            @JoinColumn(name="unidade_id")
            private Unidade unidade;


            public Unidade getUnidade() {
                return unidade;
            }
            public void setUnidade(Unidade unidade) {
                this.unidade = unidade;
            }
            public int getId() {
                return id;
            }
            public void setId(int id) {
                this.id = id;
            }
            public String getNome() {
                return nome;
            }
            public void setNome(String nome) {
                this.nome = nome;
            }
            public String getLogin() {
                return login;
            }
            public void setLogin(String login) {
                this.login = login;
            }
            public String getSenha() {
                return senha;
            }
            public void setSenha(String senha) {
                this.senha = senha;
            }



        }

另一个实体:

        package br.gov.xyz.malotes.model;

        import java.util.Set;

        import javax.persistence.Column;
        import javax.persistence.Entity;
        import javax.persistence.GeneratedValue;
        import javax.persistence.Id;
        import javax.persistence.OneToMany;
        import javax.persistence.Table;

        import org.hibernate.annotations.GenericGenerator;

        @Entity
        public class Unidade {

            @Id
            @GenericGenerator(name="gen",strategy="increment")
            @GeneratedValue(generator="gen")
            @Column(name = "id", unique = true, nullable = false, precision = 15, scale = 0)
            private int id;

            @OneToMany(mappedBy="unidade")
            private Set<Usuario> usuario;

            private String nome;

            private String endereco;

            public Set<Usuario> getUsuario() {
                return usuario;
            }

            public void setUsuario(Set<Usuario> usuario) {
                this.usuario = usuario;
            }

            public void setId(int id) {
                this.id = id;
            }

            public String getNome() {
                return nome;
            }

            public void setNome(String nome) {
                this.nome = nome;
            }

            public String getEndereco() {
                return endereco;
            }

            public void setEndereco(String endereco) {
                this.endereco = endereco;
            }



        }

如有必要,这里是Maven中的 pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/maven-v4_0_0.xsd">
                   <modelVersion>4.0.0</modelVersion>

               <parent>
                  <artifactId>malotes</artifactId>
                  <groupId>br.gov.xyz</groupId>
                  <version>0.1</version>
               </parent>

               <artifactId>malotes-ejb</artifactId>
               <packaging>ejb</packaging>

               <name>malotes EJB module</name>

               <dependencies>

               <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-core</artifactId>
                <version>4.3.5.Final</version>
            </dependency>

                  <!-- Declare the APIs we depend on and need for compilation. All of 
                 them are provided by JBoss AS 7 -->

                  <!-- Import the EJB API, we use provided scope as the API is included 
                 in JBoss AS 7 -->
                  <dependency>
                 <groupId>org.jboss.spec.javax.ejb</groupId>
                 <artifactId>jboss-ejb-api_3.1_spec</artifactId>
                 <scope>provided</scope>
                  </dependency>

                  <!-- Import the CDI API, we use provided scope as the API is included 
                 in JBoss AS 7 -->
                  <dependency>
                 <groupId>javax.enterprise</groupId>
                 <artifactId>cdi-api</artifactId>
                 <scope>provided</scope>
                  </dependency>

            <dependency>
                <groupId>org.hibernate.javax.persistence</groupId>
                <artifactId>hibernate-jpa-2.1-api</artifactId>
                <version>1.0.0.Final</version>
            </dependency>


            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.6</version>
            </dependency>



                  <!-- Test scope dependencies -->
                  <dependency>
                 <groupId>junit</groupId>
                 <artifactId>junit</artifactId>
                  </dependency>

                  <!-- Optional, but highly recommended -->
                  <!-- Arquillian allows you to test enterprise code such as EJBs and 
                 Transactional(JTA) JPA from JUnit/TestNG -->


               </dependencies>



               <build>
                  <plugins>
                 <plugin>
                    <artifactId>maven-ejb-plugin</artifactId>
                    <version>2.3</version>
                    <configuration>
                       <!-- Tell Maven we are using EJB 3.1 -->
                       <ejbVersion>3.1</ejbVersion>
                    </configuration>
                 </plugin>


                 <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <executions>
                  <execution>
                    <id>default-testCompile</id>
                    <phase>test-compile</phase>
                    <goals>
                      <goal>testCompile</goal>
                    </goals>
                    <configuration>
                      <source>1.7</source>
                      <target>1.7</target>
                    </configuration>
                  </execution>
                  <execution>
                    <id>default-compile</id>
                    <phase>compile</phase>
                    <goals>
                      <goal>compile</goal>
                    </goals>
                    <configuration>
                      <source>1.7</source>
                      <target>1.7</target>
                    </configuration>
                  </execution>
                </executions>
                <configuration>
                  <source>1.7</source>
                  <target>1.7</target>
                </configuration>
                  </plugin>



                  </plugins>
               </build>

               <profiles>
                  <profile>
                 <!-- The default profile skips all tests, though you can tune it 
                    to run just unit tests based on a custom pattern -->
                 <!-- Seperate profiles are provided for running all tests, including 
                    Arquillian tests that execute in the specified container -->
                 <id>default</id>
                 <activation>
                    <activeByDefault>true</activeByDefault>
                 </activation>
                 <build>
                    <plugins>
                       <plugin>
                      <artifactId>maven-surefire-plugin</artifactId>
                      <version>2.4.3</version>
                      <configuration>
                         <skip>true</skip>
                      </configuration>
                       </plugin>
                    </plugins>
                 </build>
                  </profile>

                  <profile>
                 <!-- We add the JBoss repository as we need the JBoss AS connectors 
                    for Arquillian -->
                 <repositories>
                    <!-- The JBoss Community public repository is a composite repository 
                       of several major repositories -->
                    <!-- see http://community.jboss.org/wiki/MavenGettingStarted-Users -->
                    <repository>
                       <id>jboss-public-repository</id>
                       <name>JBoss Repository</name>
                       <url>http://repository.jboss.org/nexus/content/groups/public</url>
                       <!-- These optional flags are designed to speed up your builds 
                      by reducing remote server calls -->
                       <releases>
                       </releases>
                       <snapshots>
                      <enabled>false</enabled>
                       </snapshots>
                    </repository>
                 </repositories>

                 <pluginRepositories>
                    <pluginRepository>
                       <id>jboss-public-repository</id>
                       <name>JBoss Repository</name>
                       <url>http://repository.jboss.org/nexus/content/groups/public</url>
                       <releases>
                       </releases>
                       <snapshots>
                      <enabled>false</enabled>
                       </snapshots>
                    </pluginRepository>
                 </pluginRepositories>

                 <!-- An optional Arquillian testing profile that executes tests 
                    in your JBoss AS instance -->
                 <!-- This profile will start a new JBoss AS instance, and execute 
                    the test, shutting it down when done -->
                 <!-- Run with: mvn clean test -Parq-jbossas-managed -->
                 <id>arq-jbossas-managed</id>
                 <dependencies>
                    <dependency>
                       <groupId>org.jboss.as</groupId>
                       <artifactId>jboss-as-arquillian-container-managed</artifactId>
                       <version>7.0.2.Final</version>
                       <scope>test</scope>
                    </dependency>
                 </dependencies>
                  </profile>

                  <profile>
                 <!-- We add the JBoss repository as we need the JBoss AS connectors 
                    for Arquillian -->
                 <repositories>
                    <!-- The JBoss Community public repository is a composite repository 
                       of several major repositories -->
                    <!-- see http://community.jboss.org/wiki/MavenGettingStarted-Users -->
                    <repository>
                       <id>jboss-public-repository</id>
                       <name>JBoss Repository</name>
                       <url>http://repository.jboss.org/nexus/content/groups/public</url>
                       <!-- These optional flags are designed to speed up your builds 
                      by reducing remote server calls -->
                       <releases>
                       </releases>
                       <snapshots>
                      <enabled>false</enabled>
                       </snapshots>
                    </repository>
                 </repositories>

                 <pluginRepositories>
                    <pluginRepository>
                       <id>jboss-public-repository</id>
                       <name>JBoss Repository</name>
                       <url>http://repository.jboss.org/nexus/content/groups/public</url>
                       <releases>
                       </releases>
                       <snapshots>
                      <enabled>false</enabled>
                       </snapshots>
                    </pluginRepository>
                 </pluginRepositories>

                 <!-- An optional Arquillian testing profile that executes tests 
                    in a remote JBoss AS instance -->
                 <!-- Run with: mvn clean test -Parq-jbossas-remote -->
                 <id>arq-jbossas-remote</id>
                 <dependencies>
                    <dependency>
                       <groupId>org.jboss.as</groupId>
                       <artifactId>jboss-as-arquillian-container-remote</artifactId>
                       <version>7.0.2.Final</version>
                       <scope>test</scope>
                    </dependency>
                 </dependencies>
                  </profile>

               </profiles>

            </project>

任何人都可以帮我找到解决方案吗?

2 个答案:

答案 0 :(得分:3)

解决方案是从JPA 2.0升级到Jboss 7.1.1 Final的JPA 2.1模块。 要进行此升级:

1 - 在这里下载JPA 2.1 --- Hibernate没有泛型:Maven Spec Hibernate JPA 2.1

2 - 将jar下载到此处:jboss-as-7.1.1.Final/modules/javax/persistence/api/main或者如果您正在使用企业JBoss(EAP),那么将jar放入此文件夹中:JBOSS-EAP-6.4.0\modules\system\layers\base\javax\persistence\api\main(如lukman所评论)

3 - 编辑同一目录中的module.xml以使用下载的jar,如下所示:

 <resources>
    <resource-root path="hibernate-jpa-2.1-api-1.0.0.Final.jar"/>
</resources>

4 - 重启你的Jboss

不确定;但似乎JPA 2.0没有javax.persistence.JoinColumn.foreignKey()方法。所以我试图用JPA 2.1方式编写代码;但Jboss工厂使用内置的JPA 2.0

答案 1 :(得分:0)

因为您正在使用

javax.persistence.JoinColumn

是JPA注释。

javax.persistence.ForeignKey class版本存在冲突时会发生此异常。检查您的classpath,如果您正在使用maven,请检查依赖项。

如果您正在使用eclipse并且maven与eclipse集成,那么您可以在pom.xml中获得完整的依赖关系层次结构。

可能有多个版本的jar具有此类,可能是通过传递依赖。您可能需要排除旧版本。