我是 JavaEE 的新手,我一直在从事一个简单的 Springboot 项目。 每次我运行它时,我都会收到此错误:
请随时回答我的问题,非常感谢代码中的任何改进。
Field sessionFactory in com.example.dao.CartDaoImpl required a bean of type 'org.hibernate.SessionFactory' that could not be found.
Action:
Consider defining a bean of type 'org.hibernate.SessionFactory' in your configuration.
最佳答案
可以从已经在 spring-boot-starter-data-jpa
中配置的 EntityManager 获取 Session。所以注入(inject) EntityManager 而不是 SessionFactory:
@Autowired
private EntityManager entityManager;
private Session getSession() {
return entityManager.unwrap(Session.class);
}
并在需要的地方使用 getSession() 方法。
关于java - 考虑在您的配置中定义一个类型为 'org.hibernate.SessionFactory' 的 bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45120775/