org.hibernate.HibernateException:找不到当前线程的会话 [英] org.hibernate.HibernateException: No Session found for current thread
问题描述
我收到了Spring3和Hibernte4的上述异常
以下是我的bean xml文件
<?xml version =1.0encoding =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 =dataSourceclass =org.springframework.jdbc.datasource.DriverManagerDataSource>
< property name =driverClassNamevalue =com.mysql.jdbc.Driver/>
< property name =urlvalue =jdbc:mysql:// localhost:3306 / GHS/>
< property name =usernamevalue =root/>
< property name =passwordvalue =newpwd/>
< / bean>
< bean id =sessionFactory
class =org.springframework.orm.hibernate4.LocalSessionFactoryBean>
< property name =dataSourceref =dataSource/>
< property name =hibernateProperties>
<道具>
< prop key =dialect> org.hibernate.dialect.MySQL5Dialect< / prop>
< /道具>
< / 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();
$ b $ p
$ b 以下代码在标题中引发异常, p>
public class Main {
public static void main(String [] args){
ClassPathXmlApplicationContext context =
new ClassPathXmlApplicationContext(dao-beans.xml);
BaseDAO bd =(BaseDAO)context.getBean(baseDAO);
bd.getCurrentSession();
$ b 有没有人对如何解决这个问题有所了解?
解决方案 getCurrentSession() 。
您需要声明适当的事务管理器,划分事务的边界并在其中执行数据访问。例如,如下所示:
< bean id =transactionManagerclass =org.springframework.orm.hibernate4.HibernateTransactionManager >
< property name =sessionFactoryref =sessionFactory/>
< / bean>
。
PlatformTransactionManager ptm = context.getBean(PlatformTransactionManager.class);
TransactionTemplate tx = new TransactionTemplate(ptm);
tx.execute(new TransactionCallbackWithoutResult(){
public void doInTransactionWithoutResult(TransactionStatus status){
//在这里执行数据访问
}
}) ;
另请参阅:
I'm getting the above exception with Spring3 and Hibernte4
The following is my bean xml file
<?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>
My BaseDAO class looks like this
public class BaseDAOImpl implements BaseDAO{
private SessionFactory sessionFactory;
@Autowired
public BaseDAOImpl(SessionFactory sessionFactory){
this.sessionFactory = sessionFactory;
}
@Override
public Session getCurrentSession(){
return sessionFactory.getCurrentSession();
}
}
The following code throws the exception in the title
public class Main {
public static void main(String[] args){
ClassPathXmlApplicationContext context =
new ClassPathXmlApplicationContext("dao-beans.xml");
BaseDAO bd = (BaseDAO) context.getBean("baseDAO");
bd.getCurrentSession();
}
}
Does anyone have an idea about how to solve this problem?
解决方案 getCurrentSession()
only makes sense inside a scope of transaction.
You need to declare an appropriate transaction manager, demarcate boundaries of transaction and perform data access inside it. For example, as follows:
<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
}
});
See also:
这篇关于org.hibernate.HibernateException:找不到当前线程的会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文