0

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.

5
  • It look like the Hibernate Annotation jar is wrong. It is not compatible with the jar version. So that is why it showing the "java.lang.NoSuchMethodError" Commented Sep 13, 2014 at 12:41
  • pls post you complete pom.xml, also check pom dependency tree for hibernate-commons-annotations Commented Sep 13, 2014 at 12:43
  • @ankur-singhal but then how is it possible that the same code is running successfully offline(I called the same dao function using P.S.V.M., and it is working)?(please find my complete pom.xml above) Commented Sep 13, 2014 at 16:08
  • @ankur-singhal Please let me know what I am looking for in the dependency tree? I filtered the results but string "hibernate" and it gave 3 results : Hibernate-core : 4.3.6.final, hibernate-commons-annotations : 4.0.5.final and hibernate-jpa-2.1-api : 1.0.0.final Commented Sep 13, 2014 at 17:15
  • I did some more research and found that when I run the Tomcat from Eclipse, my hibernate jar is not deployed in the WAR that internally gets created. But when I manually export the WAR from eclipse and then load it in Tomcat using the tomcat console, the application is working fine. So, now I want to know, why my eclipse is not loading the same jar in tomcat when running the application internally but exporting it in the WAR file created? Commented Sep 13, 2014 at 19:40

2 Answers 2

1
  1. In eclipse, ur project name and "Properties" Dialog.
  2. Left menu choose "Deployment Assembly"
  3. Right window "Web Deployment Assembly" and "Add" push.
  4. search and choose "Maven Dependencies" and adding.(if u have manually jar-lib add too.)

Now ur eclipse "Run As" time Tomcat correct deploy project dependencies jar.

i wish u can fix problem.

good luck.

Sign up to request clarification or add additional context in comments.

1 Comment

I figured it out. The problem was the after frequent changes in pom.xml, the system copied some jars in tomcat server instance and did not removed it. There were 2-3 hibernate jars present over there none of which I was using. What I did was, I deleted all the jars, then removed the server instance from Eclipse and re-added it. And then everything worked 100% fine. Still I very much appreciate your efforts in taking out time and posting a reply here. Thank you very much :)
0

@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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.