SpringMVC4+Hibernate4运行报错Could not obtain transaction-synchronized Session for current thread

来自  http://blog.csdn.net/caiwenfeng_for_23/article/details/45242545



查了一下相关资料,整理如下:

原因:Hibernate4 No Session found for current thread原因

解决方法:


1.  在spring 配置文件中加入

程序代码 程序代码

<tx:annotation-driven transaction-manager="transactionManager"/>

并且在处理业务逻辑的类上采用注解
程序代码 程序代码

@Service
public class CustomerServiceImpl implements CustomerService {  
    @Transactional
    public void saveCustomer(Customer customer) {
        customerDaoImpl.saveCustomer(customer);
    }
    ...
}


另外在 hibernate 的配置文件中,也可以增加这样的配置来避免这个错误:
程序代码 程序代码

<property name="current_session_context_class">thread</property>

猜你喜欢

转载自blog.csdn.net/laozhong110/article/details/70670601
今日推荐