javahibernatetomcatstrutsnosuchmethoderror

java.lang.NoSuchMethodError for Hibernate Configuration class coming only on Tomcat


I am making a Struts_1.2 + Log4J2_2.0.2 + Hibernate_4.3.6 project of my own. And I made a simple DAO class and ran in local environment(simple java class that runs as local Java application), and it worked 100% fine.

Now I just called the same DAO class from a Action Controller i.e made a JSP sent the data to Action Controller then it transferred the data to DAO, server is Tomcat 7.x, and when I try to run, it says

java.lang.NoSuchMethodError: org.hibernate.cfg.Configuration.addAnnotatedClass(Ljava/lang/Class;)Lorg/hibernate/cfg/Configuration;

What I am unable to understand is the same code running without Tomcat(local application) but when it comes to Tomcat it is not able to find the method in the class.

I have checked the jar, it contains the file and that file has the method also(no compiler error)

I will be very grateful, if anybody can help me on this.

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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>TestApp</groupId>
    <artifactId>TestApp</artifactId>
    <version>1.0</version>
    <packaging>war</packaging>
    <name>TestApp</name>
    <description>Website</description>
    <build>
        <sourceDirectory>src</sourceDirectory>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <warSourceDirectory>WebContent</warSourceDirectory>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>struts</groupId>
            <artifactId>struts</artifactId>
            <version>1.2.8</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.3.6.Final</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.32</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.0.2</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
        </dependency>
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-email</artifactId>
            <version>1.3.3</version>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.3</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-client</artifactId>
            <version>2.12</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-server</artifactId>
            <version>2.12</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-common</artifactId>
            <version>2.12</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet-core</artifactId>
            <version>2.12</version>
        </dependency>
    </dependencies>
</project>

DAO(Java):

public void saveHitCountInDB(HitCount count) {
        session.update(count);
        session.flush();
 }

POJO(Java):

@Entity
@Table(name = "hitcounter")
public class HitCount {
     @Id
     @Column(name = "count", unique = true, nullable = false)
     private int hitCount;
     //getter and setter and constructors
}

Connection(Java):

configuration = new Configuration().configure();
configuration.addAnnotatedClass(HitCount.class); //problem reported here

Note: let me know if more code is required.


Solution

  • @Ankur-Singhal Thanks for your quick support, but I figured out the solution on my end.

    Solution: When I switched between Hibernate dependencies in POM.XML, it downloaded one old version of hibernate jar(somewhere in version 3.x.x) and kept it nowhere except one location .metadata.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\TestApp\WEB-INF\lib

    As a result, when I run the project in Eclipse it's getting conflicted with a different version and when I export it, there is a correct version getting exported.

    I closed Eclipse, deleted the lib folder, opened Eclipse, and rebuilt it.

    And now it's working fine.

    I hope this helps someone else as well.

    Some more info:
    -My project folder was not in the Eclipse workspace folder
    -My maven repositories are at different locations
    -My Tomcat is at external location from eclipse

    So what I concluded is that, if there is any kind of jar mismatch, where the project is having maven and is not kept inside Ecplise workspace, then all the lib files are kept at 3 different locations:-

    1. target<appname>\WEB-INF\lib folder inside the project folder
    2. maven repository(for windows, %user%.m2\repository<br/>
    3. inside your project folder kept inside the .metdata folder of Eclipse workspace