2014-09-13 21 views
0

我正在製作自己的Struts_1.2 + Log4J2_2.0.2 + Hibernate_4.3.6項目。我做了一個簡單的DAO類,並在本地環境(運行爲本地Java應用程序的簡單Java類)中運行,並且它100%正常工作。針對Hibernate的java.lang.NoSuchMethodError僅在Tomcat上配置類

現在我只是從一個動作控制器調用相同的DAO類,即使JSP將數據發送到動作控制器,然後它將數據傳輸到DAO,服務器是Tomcat 7.x,當我嘗試運行時,它表示

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

我無法理解什麼是沒有的Tomcat(本地應用程序),但是當涉及到Tomcat是不能夠找到在類中的方法運行相同的代碼。

我已經檢查了罐子,它包含的文件和文件具有方法也(沒有編譯器錯誤)

我會很感激,如果有人可以幫助我在此。

的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(爪哇):

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

POJO(爪哇):

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

連接(爪哇):

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

注意:讓我知道是否需要更多代碼。

+0

它看起來像'Hibernate Annotation jar'是錯誤的。它與jar版本不兼容。所以這就是爲什麼它顯示「'java.lang.NoSuchMethodError'」 – 2014-09-13 12:41:48

+0

請你完成'pom.xml',同時檢查'hibernate-commons-annotations'的pom依賴關係樹 – 2014-09-13 12:43:52

+0

@ ankur-singhal但是它怎麼樣可能相同的代碼是離線成功運行的(我使用PSVM調用了相同的dao函數,並且它正在工作)?(請在上面找到我的完整pom.xml) – Jayesh 2014-09-13 16:08:53

回答

0

@ Ankur-Singhal感謝您的快速支持,但我終於想出瞭解決方案。

解決方案:當我在pom.xml中Hibernate依賴之間切換,它下載一箇舊版本的休眠罐(版本3.xx的地方),並保持它無處除了一個位置 .metadata.plugins \ org.eclipse。 wst.server.core \ tmp0 \ wtpwebapps \ TestApp \ WEB-INF \ lib

因此,當我在eclipse中運行項目時,它與不同版本發生衝突,導出時出現正確的導出版本。

我關閉了Eclipse,刪除了lib文件夾,打開了eclipse,並重新構建它。

而不是它的工作正常。

我希望這可以幫助別人。

一些更多的信息:
- 我的項目文件夾不是在Eclipse工作區文件夾
-My Maven倉庫是在不同的位置
- 我的Tomcat是在外部位置從日食

因此,我的結論是如果有任何種類的jar不匹配,項目有maven並且不保存在ecplise工作區內,那麼我們所有的lib文件都保存在3個不同的位置: -
1)target \\ WEB-INF \ lib文件夾裏面的文件夾
2)maven倉庫(對於windows,%user%。2 \庫\
3)你的項目文件夾內保存Eclipse工作

1
  1. 在Eclipse中,烏爾項目名稱和「屬性」對話框的.metdata文件夾內。
  2. 左側菜單選擇「部署程序集」
  3. 右側窗口「Web部署程序集」和「添加」推送。
  4. 搜索並選擇「Maven依賴」和增加。(如果u有手動JAR-lib中添加過多。)

現在烏爾月食「運行爲」時間的Tomcat正確的部署項目依賴罐子。

我希望你能解決問題。

好運。

+0

我想通了。問題在於在pom.xml中頻繁更改之後,系統在tomcat服務器實例中複製了一些jar並且沒有刪除它。在那裏有2-3個冬眠罐在我沒有使用。我所做的是,我刪除了所有的jar,然後從Eclipse中刪除服務器實例並重新添加它。然後一切正常,100%罰款。儘管如此,我仍然非常感謝你在這裏抽出時間和發表回覆的努力。非常感謝你 :) – Jayesh 2014-09-16 12:10:24