G Bisconcini
G Bisconcini

Reputation: 764

Error: java.lang.NoSuchMethodError in Hibernate 4.3.5.final core

There was a similar question: here but I'm not using Spring and JPA. Also, I've already tried the solution and don't have success. The Hibernate version is different too.

Problem: An EAR project with EJB 3.1 , Hibernate 4.3.5.Final no JPA, using Hibernate Core (Mysql).

The deployed project is up, with no errors in deploy.

After I created two Entity's using @ManytoOne mapping, I'm getting the error:

    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)

(The error was too long, if want the stackTrace, please tell.)

Here are the two Entity's I created, and then, the error appeared:

        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;
            }



        }

The other entity:

        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;
            }



        }

If necessary, here is the pom.xml in Maven:

<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>

Can anyone help me to find a solution?

Upvotes: 0

Views: 4287

Answers (2)

G Bisconcini
G Bisconcini

Reputation: 764

The solution was upgrade from JPA 2.0 to JPA 2.1 module of the Jboss 7.1.1 Final. To do this upgrade:

1 - download the JPA 2.1 ---no generic by Hibernate here: Maven Spec Hibernate JPA 2.1

2 - put the jar downloaded here : jboss-as-7.1.1.Final/modules/javax/persistence/api/main OR If you are using enterprise JBoss (EAP) then put the jar into this folder instead: JBOSS-EAP-6.4.0\modules\system\layers\base\javax\persistence\api\main (like lukman commented)

3 - edit the module.xml in the same dir to use that jar downloaded, like these:

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

4 - restart your Jboss

Dont have sure; but it seems the JPA 2.0 doesnt have the javax.persistence.JoinColumn.foreignKey() Method. So I was trying to code in JPA 2.1 way; but the Jboss factory uses JPA 2.0 built in instead

Upvotes: 3

Ankur Singhal
Ankur Singhal

Reputation: 26067

Since you are using

javax.persistence.JoinColumn

which is JPA annotation.

This exception occurs of there are conflicting version of javax.persistence.ForeignKey class. Check your classpath and if you are using maven check the dependencies.

If you are using eclipse and maven is integrated with eclipse, then you can get the complete dependency hierarchy of dependencies in pom.xml.

There could be multiple versions of the jar having this class, may be through transitive dependency. You might need to exclude the older version.

Upvotes: 0

Related Questions