【问题标题】:NoSuchMethodError: org.hibernate.internal.CoreMessageLogger.debugf(Ljava/lang/String;I)VNoSuchMethodError: org.hibernate.internal.CoreMessageLogger.debugf(Ljava/lang/String;I)V
【发布时间】:2017-04-06 15:10:12
【问题描述】:

今天我正在尝试使用 Junit 尝试与 hibernate 进行集成测试。我对集成测试很陌生,所以我希望我做错了什么。 我在 githhub 上找到了这个例子: example hibernate junit test 我已经下载并尝试了它,它可以工作,所以我尝试在我的 pom 上复制依赖项和类(因为我的目标是将来使用我的内部数据库并测试我已经编写的函数),并复制这个类:

package org.hibernate.tutorial.annotations;
import java.util.Date;
import java.util.List;
import junit.framework.TestCase;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class AnnotationsIllustrationTest extends TestCase {
private SessionFactory sessionFactory;

@Override
protected void setUp() throws Exception {
    // A SessionFactory is set up once for an application
    sessionFactory = new Configuration()
            .configure() // configures settings from hibernate.cfg.xml
            .buildSessionFactory();
}

@Override
protected void tearDown() throws Exception {
    if ( sessionFactory != null ) {
        sessionFactory.close();
    }
}

@SuppressWarnings({ "unchecked" })
public void testBasicUsage() {
    // create a couple of events...
    Session session = sessionFactory.openSession();
    session.beginTransaction();
    session.close();

    // now lets pull events from the database and list them
    session = sessionFactory.openSession();
    session.beginTransaction();
    List result = session.createQuery( "from Event" ).list();
//      for ( Event event : (List<Event>) result ) {
//          System.out.println( "Event (" + event.getDate() + ") : " +     event.getTitle() );
//      }
    session.getTransaction().commit();
    session.close();
}

但是当我尝试“Junit 测试”或“Maven 测试”时,我遇到了这个错误: java.lang.NoSuchMethodError: org.hibernate.internal.CoreMessageLogger.debugf(Ljava/lang/String;I)V

我已经看到问题出在依赖项上,所以我尝试更改我的依赖项的版本,仅在测试中使用一些依赖项,但它并没有改变任何东西......这里是我的 pom:

    <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.7</java.version>

    <!-- JBoss dependency versions -->
    <version.jboss.maven.plugin>7.1.1.Final</version.jboss.maven.plugin>

    <bcpkix-jdk15on.version>1.48</bcpkix-jdk15on.version>
    <cxf-bundle.version>2.5.2</cxf-bundle.version>
    <javax.ejb-api.version>3.2</javax.ejb-api.version>

    <commons-codec.version>1.6</commons-codec.version>
    <commons-configuration2.version>2.0</commons-configuration2.version>
    <commons-io.version>2.4</commons-io.version>
    <commons-lang.version>2.6</commons-lang.version>
    <commons-logging.version>1.2</commons-logging.version>
    <commons-logging-api.version>1.1</commons-logging-api.version>
    <commons-net.version>3.4</commons-net.version>
    <commons-collections.version>3.2.2</commons-collections.version>
    <commons-fileupload.version>1.2</commons-fileupload.version>

    <fluent-hc.version>4.3.6</fluent-hc.version>
    <httpclient-osgi.version>4.2.1</httpclient-osgi.version>
    <httpclient-cache.version>4.3.6</httpclient-cache.version>
    <httpcore.version>4.3.3</httpcore.version>
    <httpmime.version>4.3.6</httpmime.version>
    <log4j.version>1.2.17</log4j.version>
    <mysql-connector-java>5.1.31</mysql-connector-java>


    <!-- Maven Plugin -->
    <maven-javadoc-plugin.version>2.10.3</maven-javadoc-plugin.version>
    <maven-source-plugin.version>3.0.0</maven-source-plugin.version>
    <maven-compiler-plugin.version>3.5.1</maven-compiler-plugin.version>
    <maven-war-plugin.version>2.6</maven-war-plugin.version>
    <jdk.source>1.7</jdk.source>
    <jdk.target>1.7</jdk.target>

</properties>


<dependencies>

    <!-- Not provided: -->
    <!-- https://mvnrepository.com/artifact/commons-codec/commons-codec -->
    <dependency>
        <groupId>commons-codec</groupId>
        <artifactId>commons-codec</artifactId>
        <version>${commons-codec.version}</version>
    </dependency>


    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-configuration2</artifactId>
        <version>${commons-configuration2.version}</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/commons-lang/commons-lang -->
    <dependency>
        <groupId>commons-lang</groupId>
        <artifactId>commons-lang</artifactId>
        <version>${commons-lang.version}</version>
    </dependency>

    <!-- Commons Logging -->

    <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging-api -->
    <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging-api</artifactId>
        <version>${commons-logging-api.version}</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
    <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <version>${commons-logging.version}</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/commons-net/commons-net -->
    <dependency>
        <groupId>commons-net</groupId>
        <artifactId>commons-net</artifactId>
        <version>${commons-net.version}</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/javax.persistence/com.springsource.javax.persistence -->
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>${log4j.version}</version> <!-- In questo modo prende la versione da sopra -->
    </dependency>

    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient-cache</artifactId>
        <version>${httpclient-cache.version}</version>
    </dependency>

    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpcore</artifactId>
        <version>${httpcore.version}</version>
    </dependency>


    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpmime</artifactId>
        <version>${httpmime.version}</version>
    </dependency>


    <!-- Da qui in poi sono tutti Provided: -->
    <dependency>
        <groupId>org.jboss.as</groupId>
        <artifactId>jboss-as-web</artifactId>
        <version>${version.jboss.maven.plugin}</version>
        <scope>provided</scope>
    </dependency>

    <!-- Per Base32 -->
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient-osgi</artifactId>
        <version>${httpclient-osgi.version}</version>
        <scope>provided</scope>
    </dependency>

    <!-- Database -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>${mysql-connector-java}</version>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>javax.ejb</groupId>
        <artifactId>javax.ejb-api</artifactId>
        <version>${javax.ejb-api.version}</version>
        <scope>provided</scope>
    </dependency>


    <!-- https://mvnrepository.com/artifact/org.bouncycastle/bcpkix-jdk15on -->
    <dependency>
        <groupId>org.bouncycastle</groupId>
        <artifactId>bcpkix-jdk15on</artifactId>
        <version>${bcpkix-jdk15on.version}</version>
        <!-- <scope>provided</scope> -->
    </dependency>


    <!-- CXF -->
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-bundle</artifactId>
        <version>${cxf-bundle.version}</version>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>fluent-hc</artifactId>
        <version>${fluent-hc.version}</version>
        <scope>provided</scope>
    </dependency>

    <!-- Prova Junit -->

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-simple</artifactId>
        <version>1.6.1</version>
    </dependency>

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.10</version>
    </dependency>

    <!-- The H2 in-memory database -->
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <version>1.2.145</version>
    </dependency>

   <!--  to use Hibernate as the JPA provider at a guess! -->
   <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>5.0.2.Final</version>
    </dependency>


</dependencies>
<!-- build -->
<build>
    <sourceDirectory>src/main/java</sourceDirectory>
    <finalName>${project.artifactId}</finalName>

    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>

    <plugins>
        <plugin>
            <groupId>org.jboss.as.plugins</groupId>
            <artifactId>jboss-as-maven-plugin</artifactId>
            <version>${version.jboss.maven.plugin}</version>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>${maven-compiler-plugin.version}</version>
            <configuration>
                <source>${jdk.source}</source>
                <target>${jdk.target}</target>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>${maven-war-plugin.version}</version>
            <configuration>
                <failOnMissingWebXml>false</failOnMissingWebXml>
                <archive>
                    <manifestFile>src/main/resources/META-INF/MANIFEST.MF</manifestFile>
                </archive>
            </configuration>
        </plugin>

    </plugins>
</build>

提供了一些依赖,因为我使用jboss来部署我的项目;我还复制了 src/test/resources 中的 persistence.xml 和 hibernate.cfg.xml。

我做错了什么?有一个最佳实践来做我想做的事吗?

【问题讨论】:

  • 你解决了吗?我想我有类似的问题,但我不知道如何解决它!
  • 我可以和你聊天吗?我将解释我所做的一些事情
  • 我现在不能聊天 :-( 但是,你为什么不给自己写一个答案?如果你写我也会给答案加一个 +1 !
  • 因为首先你和我都是意大利人,其次因为我不确定我是否找到了解决问题的方法,所以我记得我尝试过一些东西但我不确定最后是否我找到了解决方案,其中一部分我不再使用它,所以我不确定结果。
  • 我解决了这个问题!在另一点上,在我的情况下,问题是由两次声明的依赖引起的! “奇怪”的是,在我升级 hibernate-core 版本之前就出现了这个错误! :-) 不过还是谢谢你

标签: java hibernate maven junit integration-testing


【解决方案1】:

正如我所说,我遇到了类似的问题,在我的例子中,我解决了检查 maven 依赖树...我在提示符中使用了这个命令:

mvn 依赖:tree -Dverbose 并阅读响应,我发现一个库声明了两次,但它不是 Hibernate 的库,只有在我从 hibernate4 移植到 hibernate5 后才导致此问题。

希望对遇到这个问题的人有用。

【讨论】:

    猜你喜欢
    • 2016-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-06
    • 2019-04-09
    • 1970-01-01
    • 2020-08-29
    • 1970-01-01
    相关资源
    最近更新 更多