gpt4 book ai didi

java - NoSuchMethodError : org. hibernate.internal.CoreMessageLogger.debugf(Ljava/lang/String;I)V

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:50:46 28 4
gpt4 key购买 nike

今天我尝试使用 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。

我做错了什么?有做我想做的最佳实践吗?

最佳答案

当我 xommented 时,我遇到了类似的问题,在我的例子中,我解决了检查 maven 依赖关系树的问题......我在我的提示中使用了这个命令:

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

我希望它对任何遇到这个问题的人都有用。

关于java - NoSuchMethodError : org. hibernate.internal.CoreMessageLogger.debugf(Ljava/lang/String;I)V,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43259068/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com