[英]Getting “HibernateException: No Hibernate Session bound to thread, & config doesn't allow creation ..” while execting spring hibernate
I am trying to write a simple spring-hibernate command line application with transaction management support, but I am constantly getting "org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here" 我正在尝试编写一个具有事务管理支持的简单的spring-hibernate 命令行应用程序 ,但是我一直在获取“ org.hibernate.HibernateException:没有Hibernate Session绑定到线程,并且配置不允许在这里创建非事务性会话”
my spring config file (applicationContext.xml) 我的春季配置文件(applicationContext.xml)
<!-- Context Provider -->
<bean class="com.rps.util.ContextUtil" />
<!-- The transactional service objects -->
<bean id="service" class="com.rps.service.DefaultService" p:parentDao-ref="parentDao" p:childDao-ref="childDao" />
<!--the transactional advice-->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="get*" read-only="true" />
<tx:method name="*" />
</tx:attributes>
</tx:advice>
<!--this transactional advice runs for any service method execution-->
<aop:config>
<aop:advisor pointcut="execution(* com.rps.service..*.*(..))" advice-ref="txAdvice" />
</aop:config>
<!-- The PlatformTransactionManager -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" p:sessionFactory-ref="sessionFactory" />
<!-- The DataSource -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" p:driverClassName="com.mysql.jdbc.Driver" p:url="jdbc:mysql://localhost:3306/OsivTest" p:username="XXX" p:password="YYY" />
<!-- Session Factory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" p:dataSource-ref="dataSource">
<property name="mappingResources">
<list>
<value>com/rps/domain/Parent.hbm.xml</value>
<value>com/rps/domain/Child.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<!-- Data Access Objects -->
<bean id="parentDao" class="com.rps.data.hbm.HibernateParentDao" p:sessionFactory-ref="sessionFactory" />
<bean id="childDao" class="com.rps.data.hbm.HibernateChildDao" p:sessionFactory-ref="sessionFactory" />
DAO Code (Session Factory object set by IoC container as configured in context file above) DAO代码(由IoC容器在上面的上下文文件中配置的会话工厂对象)
public List<T> findAll() {
return sessionFactory.getCurrentSession().createCriteria(getPersistentClass()).list();
}
DefaultService Class: (parentDao injected from IoC container) DefaultService类:(从IoC容器注入的parentDao)
public class DefaultService implements Service {
@Override
public List<Parent> getAllParent() {
return parentDao.findAll();
}
//Other methods
}
Main method code: 主要方法代码:
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Service s = (Service) context.getBean("service");
s.getAllParent();
}
I am using hibernate version 3.1.3 and spring 3. How do we configure one session per thread? 我正在使用休眠版本3.1.3和spring3。如何为每个线程配置一个会话? I believe the issues is that no session is being associated with my execution thread. 我相信问题在于没有会话与我的执行线程相关联。 I was assuming that transaction manager will open a session if not already found, but it does not do so. 我假设事务管理器将打开一个会话(如果尚未找到),但是它没有这样做。
Quote 1: 引用1:
I am using hibernate version 3.1.3 and spring 3 我正在使用休眠版本3.1.3和Spring 3
Quote 2: 引用2:
Note 注意
As of Spring 3.0, Spring requires Hibernate 3.2 or later. 从Spring 3.0开始,Spring需要Hibernate 3.2或更高版本。
BTW, the current stable Hibernate version is 3.6.7 顺便说一句, 当前稳定的Hibernate版本是3.6.7
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.