gpt4 book ai didi

spring - org.hibernate.HibernateException : No Session found for current thread

转载 作者:IT老高 更新时间:2023-10-28 13:43:45 25 4
gpt4 key购买 nike

Spring3 和 Hibernte4 出现上述异常

以下是我的 bean xml 文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">

<context:annotation-config/>


<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/GHS"/>
<property name="username" value="root"/>
<property name="password" value="newpwd"/>
</bean>

<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="hibernateProperties">
<props>
<prop key="dialect">org.hibernate.dialect.MySQL5Dialect</prop>
</props>
</property>
<property name="packagesToScan">
<list>
<value>com.example.ghs.model.timetable</value>
</list>
</property>
</bean>

<bean id="baseDAO"
class="com.example.ghs.dao.BaseDAOImpl"/>
</beans>

我的 BaseDAO 类看起来像这样

public class BaseDAOImpl implements BaseDAO{
private SessionFactory sessionFactory;

@Autowired
public BaseDAOImpl(SessionFactory sessionFactory){
this.sessionFactory = sessionFactory;
}

@Override
public Session getCurrentSession(){
return sessionFactory.getCurrentSession();
}
}

以下代码抛出标题中的异常

public class Main {
public static void main(String[] args){
ClassPathXmlApplicationContext context =
new ClassPathXmlApplicationContext("dao-beans.xml");
BaseDAO bd = (BaseDAO) context.getBean("baseDAO");
bd.getCurrentSession();
}
}

有人知道如何解决这个问题吗?

最佳答案

getCurrentSession() 仅在事务范围内才有意义。

您需要声明一个适当的事务管理器,划定事务的边界并在其中执行数据访问。例如如下:

<bean id = "transactionManager" class = "org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name = "sessionFactory" ref = "sessionFactory" />
</bean>

.

PlatformTransactionManager ptm = context.getBean(PlatformTransactionManager.class);
TransactionTemplate tx = new TransactionTemplate(ptm);

tx.execute(new TransactionCallbackWithoutResult() {
public void doInTransactionWithoutResult(TransactionStatus status) {
// Perform data access here
}
});

另请参阅:

关于spring - org.hibernate.HibernateException : No Session found for current thread,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10459922/

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