Springboot配置Hibernate Session

2021/8/30 23:09:34

本文主要是介绍Springboot配置Hibernate Session,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

Springboot配置Hibernate Session

近期公司老项目需要从Spring MVC升级到Springboot,项目DAO层针对Hibernate Session进行了大量的封装,但是在升级过程中发现SessionFactory无法注入,找了几种方式都没有成功,最后在stackoverflow上找到的方法如下:

  • 在application.properties中配置
spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate5.SpringSessionContext
  • 在DAO层通过以下代码获取Hibernate Session
@PersistenceContext
 private EntityManager entityManager;

 public Session getSession() {
  return entityManager.unwrap(Session.class);
 }


这篇关于Springboot配置Hibernate Session的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程