无法通过 Spring 在 Hibernate5 中使用 SessionFactory.getCurrentSession() 获取会话
Posted
技术标签:
【中文标题】无法通过 Spring 在 Hibernate5 中使用 SessionFactory.getCurrentSession() 获取会话【英文标题】:Cannot get session using SessionFactory.getCurrentSession() in Hibernate5 with Spring 【发布时间】:2019-01-12 21:30:02 【问题描述】:我对 Spring 和 Hibernate 完全陌生。我很难让 Spring 和 Hibernate 合作,以便我的 Spring 配置从 SessionFactory 对象返回一个 Session 对象。
特别是,我遵循了以下教程:https://www.baeldung.com/hibernate-5-spring。本教程用于为 H2 数据库配置 Spring 和 Hibernate。但是,我目前正在为 Oracle 12.2 配置 Spring 和 Hibernate。我已经反映了配置文件中的更改。
就像前面提到的网站建议的那样,我已经编写了一个包含所有必需依赖项的 Maven 项目。此外,我正在使用基于 XML 的配置来配置 Hibernate 5。
请在下面找到代码:
这里是 Spring - Hibernate 配置文件:
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan" value="com.test">
<property name="hibernateProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">create</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="debug">true</prop>
</props>
</property>
</bean>
<bean id="dataSource" class="org.apache.tomcat.dbcp.dbcp2.BasicDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:ORCL"/>
<property name="username" value="sa"/>
<property name="password" value="sa"/>
</bean>
<bean id="txManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
这是我用来尝试保护 Hibernate Session 对象的 Java 代码:
@Autowired
private SessionFactory sessionFactory;
public Session getSession()
Session session = null;
try
session = sessionFactory.getCurrentSession();
if(session != null)
System.out.println("Session is VALID.");
else
System.out.println("Session is INVALID.");
catch(IllegalStateException e)
System.out.println("getSession: Illegal State Exception: " + e.getMessage());
catch(Exception e)
System.out.println("getSession: Exception: " + e.getMessage());
return session;
这里是项目 pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>com.test</groupId>
<artifactId>Flow</artifactId>
<version>1.0-SNAPSHOT</version>
<name>Flow</name>
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<org.springframework.version>5.0.2.RELEASE</org.springframework.version>
<org.springframework.data.version>1.10.6.RELEASE</org.springframework.data.version>
<org.springframework.security.version>4.2.1.RELEASE</org.springframework.security.version>
<hibernate.version>5.2.10.Final</hibernate.version>
<hibernatesearch.version>5.8.2.Final</hibernatesearch.version>
<tomcat-dbcp.version>9.0.0.M26</tomcat-dbcp.version>
<jta.version>1.1</jta.version>
<hsqldb.version>2.3.4</hsqldb.version>
<oracle.version>12.2.0.1</oracle.version>
<commons-lang3.version>3.5</commons-lang3.version>
</properties>
<repositories>
<repository>
<id>maven.oracle.com</id>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
<url>https://maven.oracle.com</url>
<layout>default</layout>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>maven.oracle.com</id>
<url>https://maven.oracle.com</url>
</pluginRepository>
</pluginRepositories>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>$org.springframework.version</version>
<exclusions>
<exclusion>
<artifactId>commons-logging</artifactId>
<groupId>commons-logging</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>$org.springframework.version</version>
</dependency>
<!-- persistence -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>$org.springframework.version</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>$org.springframework.data.version</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>$hibernate.version</version>
</dependency>
<dependency>
<groupId>javax.transaction</groupId>
<artifactId>jta</artifactId>
<version>$jta.version</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-dbcp</artifactId>
<version>$tomcat-dbcp.version</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>$commons-lang3.version</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.oracle.jdbc</groupId>
<artifactId>ojdbc8</artifactId>
<version>$oracle.version</version>
</dependency>
</dependencies>
<build>
<finalName>Flows</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
我期望收到的是一个 Session 对象;但是,当我运行我的代码时,变量 session
i 返回 null。任何帮助将不胜感激。谢谢!
【问题讨论】:
【参考方案1】:hibernate 属性hibernate.current_session_context_class
定义SessionFactory
如何检索当前Session
。当使用 LocalSessionFactoryBean
构建 SessionFactory
时,默认情况下它将设置为 SpringSessionContext
这基本上意味着 Spring 将为您管理会话。
所以在大多数情况下,您不需要通过调用sessionFactory.getCurrentSession()
来获取会话。只需使用@PersistenceContext
注入并使用Session
:
@PersistenceContext
private Session session;
【讨论】:
以上是关于无法通过 Spring 在 Hibernate5 中使用 SessionFactory.getCurrentSession() 获取会话的主要内容,如果未能解决你的问题,请参考以下文章
SSH框架的简化(struts2spring4hibernate5)
SSH框架的简化(struts2spring4hibernate5)