- hibernate 执行插入修改删除需要事务操作
org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread
at org.springframework.orm.hibernate5.SpringSessionContext.currentSession(SpringSessionContext.java:142)
at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:495)
- 解决添加事务
- spring核心配置文件添加
<tx:annotation-driven/>
- 持久层添加
@Transactional
- 使用
Session session = sessionFactory.getCurrentSession();
获取 Session。
Spring事务<tx:annotation-driven/>的理解 https://www.cnblogs.com/alice-cj/p/10417773.html
OpenSession与GetCurrentSession的区别 https://zhuanlan.zhihu.com/p/129631802