在使用idea开发springboot项目的时候,想获取一个seesionfactory的bean,网上用配置在application中提示HibernateJpaSessionFactoryBean过时,运行报错。
自己写了一个
@Bean LocalSessionFactoryBean sessionFactoryBean(DataSource dataSource) { LocalSessionFactoryBean sessionFactoryBean = new LocalSessionFactoryBean(); sessionFactoryBean.setDataSource(dataSource); sessionFactoryBean.setPackagesToScan("com.example.kms"); Properties properties = new Properties(); properties.setProperty("dialect", "org.hibernate.dialect.MySQL5Dialect"); sessionFactoryBean.setHibernateProperties(properties); return sessionFactoryBean; }
运行异常:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'java.util.Set<javax.persistence.EntityManager>' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1507) ~[spring-beans-5.0.3.RELEASE.jar:5.0.3.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1104) ~[spring-beans-5.0.3.RELEASE.jar:5.0.3.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1065) ~[spring-beans-5.0.3.RELEASE.jar:5.0.3.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:815) ~[spring-beans-5.0.3.RELEASE.jar:5.0.3.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:721) ~[spring-beans-5.0.3.RELEASE.jar:5.0.3.RELEASE]
... 19 common frames omitted
。求解怎么正确获取到hibernate的sessionfactory
applcation.properties配置
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.url= spring.datasource.username= spring.datasource.password= spring.jpa.database=MYSQL spring.jpa.show-sql=true spring.jpa.generate-ddl=true spring.jpa.hibernate.ddl-auto=update spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate5.SpringSessionContext
springboot构造的是JPA信息,Hibernate实现,均使用EntityManager注入
entityManager.unwrap(Session.class)获取Session
使用entityfactory再来构造一个sessionfactory的bean,就会出现循环引用,是因为构造的这个bean取代了springboot自动构造的entityfactory么?这样怎么解决?
<p>同样的问题,求指导</p>
<p>同样的问题,求指导</p>
<p>spring boot2 默认集成的是5.2版本hibernate,研究了几天确实没法获取sessionFactory 会出现循环引用。而且5.2版本接口变动比较大,新项目可以使用,遗留项目不建议升级。</p>
解决方法:spring boot2+hibernate 5.012 版本。
1:在pom中添加一个属性<hibernate.version>5.0.12.Final</hibernate.version>
2:然后在添加一个依赖
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
</dependency>
回复 <a class="referer" target="_blank">@Adil_</a> : 不知道你解决没,还是留一个答案给后来的人看吧.
@Autowired private EntityManagerFactory entityManagerFactory;
SessionFactory sessionFactory = entityManagerFactory.unwrap(SessionFactory.class);
求解决方案
如果要用spring boot 2.0 + Hibernate 5.2 该怎么获取SessionFactory
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。