java - Hibernate 5 integration with Spring Boot 2
问题描述
Am planning to create a spring boot (version 2) app with hibernate 5.3 , but am facing issues while integrating hibernate 5 . Since its a spring boot app, the container will auto configure the datasource and JPA variant EntityManagerFactory and we can create Hibernate SessionFactory from this EntityManagerFactory using the unwrap() method.
So this is my code for the Hibernate config class
@Configuration
public class HibernateUtil {
@Autowired
private EntityManagerFactory entityMangerFact;
@Bean
public SessionFactory sessionFactory() {
return entityMangerFact.unwrap(SessionFactory.class);
}
}
But it is thowing BeanCurrentlyInCreationException . But if i put the unwrap() in the service class method , it wont throw exceptions .but i think that not the right thing, since we will have more service methods, and we may need to call unwrap() on each service methods. Error log:
Caused by: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'sessionFactory': Requested bean is currently in creation: Is there an unresolvable circular reference?
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.beforeSingletonCreation(DefaultSingletonBeanRegistry.java:339) ~[spring-beans-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:215) ~[spring-beans-5.1.4.RELEASE.jar:5.1.4.RELEASE]
Why the unwrap() is failing in the configuration class ?
解决方案
在 spring-boot 中,您可以访问 EntityManagerFactory ,因为您可以签入此数据源配置,但您不需要EntityManager
直接使用与数据库交互,您可以使用spring-data-jpa
推荐阅读
- hadoop - 我知道 MapReduce 使用键值对。在这种情况下,关键和价值到底是什么?
- javascript - 如何找到对象的最大值?
- java - 如何解决 pom.xml - android eclipse 应用程序中的错误?
- c++ - 在外部命名空间中使用内部命名空间而不重新打开外部命名空间
- python - 多次使用 itertools.product 的结果
- php - Laravel 5.0 助手类设置
- php - 文本框中的 ajax 输出在提交的 post 方法中不可用
- reactjs - 在 React Native Expo 上加载自定义字体会出错
- r - 将 2 个数据框与函数相结合,例如笛卡尔积或交叉连接
- android - 我制作 rss 阅读器有问题(使用 RecyclerView 和 Gson 和 CardView)