Java 获取SesionFactory中的Spring数据JPA循环引用
我正在尝试启动一个Spring项目,我已经完成了所有配置,但是,每当我尝试启动它时,我都会收到以下错误消息:Java 获取SesionFactory中的Spring数据JPA循环引用,java,spring,hibernate,spring-boot,spring-data-jpa,Java,Spring,Hibernate,Spring Boot,Spring Data Jpa,我正在尝试启动一个Spring项目,我已经完成了所有配置,但是,每当我尝试启动它时,我都会收到以下错误消息: Error creating bean with name 'getSessionFactory': Requested bean is currently in creation: Is there an unresolvable circular reference? 下面是我用于此的文件 @springboot应用程序 公共类Westworld应用程序{ 公共静态void mai
Error creating bean with name 'getSessionFactory': Requested bean is currently in creation: Is there an unresolvable circular reference?
下面是我用于此的文件
@springboot应用程序
公共类Westworld应用程序{
公共静态void main(字符串[]args){
SpringApplication.run(WestworldApplication.class,args);
RegisterRobot re=新的RegisterRobot();
}
}
@EnableAutoConfiguration
@配置
公共类HibernateConfig{
@自动连线
私人实体管理工厂实体管理工厂;
@豆子
public SessionFactory getSessionFactory(){
if(entityManagerFactory.unwrap(SessionFactory.class)==null){
抛出新的NullPointerException(“工厂不是休眠工厂”);
}
返回entityManagerFactory.unwrap(SessionFactory.class);
}
}
从hibernate 5.3开始,
SessionFactory
就是EntityManagerFactory
,这是出于安全原因而使用的密码。(SessionFactory
实际上现在扩展了EntityManagerFactory
。因此,通过定义这个bean,您实际上定义了自己的EntityManagerFactory
、的弹簧启动,并且运行了一个循环。不过,一般来说,您不应该使用SessionFactory
,而应该使用普通的JPA。@M.denum我对此非常不满,w简单的JPA是什么意思?使用EntityManager
而不是hibernateSession
和SessionFactory
。从hibernate 5.3开始,SessionFactory
就是EntityManager工厂。(SessionFactory
实际上现在扩展了EntityManagerFactory
。因此,通过定义这个bean,您实际上定义了自己的EntityManagerFactory
、的弹簧启动,并且运行了一个循环。不过,一般来说,您不应该使用SessionFactory
,而应该使用普通的JPA。@M.denum我对此非常不满,w简单的JPA是什么意思?使用EntityManager
而不是hibernateSession
和SessionFactory
。
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306
spring.datasource.username=*****
spring.datasource.password=*****
spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect
spring.jpa.show-sql=false
spring.jpa.hibernate.ddl-auto=none