gpt4 book ai didi

java - 升级到 Hibernate 5 后 Spring 4 的 Hibernate 依赖问题

转载 作者:行者123 更新时间:2023-11-30 02:37:06 26 4
gpt4 key购买 nike

下面是我的 pom:

<dependencies>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.6.0</version>
</dependency>
<dependency>
<groupId>com.github.dvdme</groupId>
<artifactId>ForecastIOLib</artifactId>
<version>1.5.1</version>
</dependency>
<dependency>
<groupId>com.wewherego</groupId>
<artifactId>api-core</artifactId>
<version>1.4.1</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.3.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.3.1.GA</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.3.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
<version>5.1.0.Final</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.32</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.2.RELEASE</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>1.7.12</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.3</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.7</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.8.7</version>
</dependency>
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.12.1.GA</version>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>10.2.0.4.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<dependency>
<groupId>com.googlecode.lambdaj</groupId>
<artifactId>lambdaj</artifactId>
<version>2.3.3</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.7.2</version>
</dependency>
<dependency>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.4.0</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-s3</artifactId>
<version>1.11.98</version>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>1.6.4</version>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-easymock</artifactId>
<version>1.6.4</version>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>1.6.4</version>
</dependency>
<dependency>
<groupId>org.easymock</groupId>
<artifactId>easymock</artifactId>
<version>3.4</version>
<scope>test</scope>
</dependency>
</dependencies>

hibernate 配置是通过注释设置的:

公共(public)类 HibnerateConfiguration {

//declarations that are ignored

public static AnnotationConfiguration config() throws IOException {
AnnotationConfiguration hibernateConfig = new AnnotationConfiguration();
hibernateConfig.setProperty(HIBERNATE_DIALECT_STRING, HIBERNATE_DIALECT_MYSQL_VALUE);
hibernateConfig.setProperty(HIBERNATE_DRIVER_CLASS_STRING, HIBERNATE_DRIVER_CLASS_MYSQL_VALUE);
String jdbcString = "jdbc:"
+ getHost()
+ ":"
+ .getPort()
+ "/"
+ getDBName()
+ "?autoReconnect=true";
hibernateConfig.setProperty(HIBERNATE_DB_URL_STRING, jdbcString);
hibernateConfig.setProperty(HIBERNATE_DB_USERNAME_STRING, getUser());
hibernateConfig.setProperty(HIBERNATE_DRIVER_PW_STRING, getPassword());
hibernateConfig.setProperty(HIBERNATE_SESSION_CONTEXT_CLASS_STRING, HIBERNATE_SESSION_CONTEXT_CLASS_VALUE);
hibernateConfig.setProperty(HIBERNATE_TIMEOUT_STRING, HIBERNATE_TIMEOUT_VALUE);
hibernateConfig.addAnnotatedClass(myClass.class);
return hibernateConfig;
}

}

在处理 session 时,我有:

包 com.wewherego.api.malloc.infrastruct;

public class SessionHandler {
private static SessionFactory sessions= null;
static {
try {
if (sessions== null) {
synchronized (SessionFactory.class) {
sessions= HibnerateConfiguration.config().buildSessionFactory();
}
}
}
catch(Throwable t) {
throw new ExceptionInInitializerError(t);
}
}

/*
* Returns a session anyway. If currently no session exist, open a new one;
* If there is a current session, use the existing one.
* ***NOTE: potential issue in this method when dealing with session like this***
*/
@Override
public Session getSession() {
try {
return sessions.getCurrentSession();
}
catch (HibernateException he) {
return sessions.getCurrentSession();
}
}

public void close() {
sessions.close();
}
}

它被用作:

session.createQuery(String.format("FROM %s", "myTable"));

将抛出此异常:

Caused by: java.lang.NoSuchMethodError: org.hibernate.Session.createQuery(Ljava/lang/String;)Lorg/hibernate/query/Query;

在运行时,依赖项是:

runtime dependencies

hibernate文档在Session类中仍然有createQuery:https://docs.jboss.org/hibernate/orm/3.5/api/org/hibernate/Session.html#createQuery(java.lang.String)

但是我肯定错过了什么吗?

最佳答案

您的类路径上有两个版本的 hibernate:3.2.6 和 5.2。删除3.2.6版本

该问题部分是由于 hibernate 项目在版本 3.2 和 3.3 之间更改了其 Maven Artifact 名称,导致 Maven 版本冲突处理无效。

要查看项目中正在拉取版本 3.2.6 的内容,请使用以下命令:

mvn dependency:tree

您将获得依赖树。找到正在拉动 hibernate.jar 的内容,然后使用 <excludes> 排除依赖项标签。

从快速查看依赖项列表来看,不需要此依赖项,因为注释直接包含在 hibernate-core.jar 中(或 hibernate-core 拉取的依赖项之一):

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.3.1.GA</version>
</dependency>

它是pulling hibernate 3.2.6.ga 。删除它,然后重试,它可能是唯一拉动 hibernate 3.2.6 的一个。

关于java - 升级到 Hibernate 5 后 Spring 4 的 Hibernate 依赖问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42758012/

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