Java 配置类中的Spring引导注入EntityManagerFactory
我正在使用SpringBoot,我想将spring与hibernate集成。我想制作一个会话工厂bean以供进一步使用。但是我不能自动连接EntityManagerFactory,我不能只在configuration类中自动连接它,在其他类中它可以工作。你能帮忙吗 配置类Java 配置类中的Spring引导注入EntityManagerFactory,java,spring,hibernate,spring-boot,jpa,Java,Spring,Hibernate,Spring Boot,Jpa,我正在使用SpringBoot,我想将spring与hibernate集成。我想制作一个会话工厂bean以供进一步使用。但是我不能自动连接EntityManagerFactory,我不能只在configuration类中自动连接它,在其他类中它可以工作。你能帮忙吗 配置类 package kz.training.springrest.configuration; @Configuration @EnableTransactionManagement public class DatabaseC
package kz.training.springrest.configuration;
@Configuration
@EnableTransactionManagement
public class DatabaseConfiguration {
@Autowired
private EntityManagerFactory entityManagerFactory;
@Bean
public SessionFactory getSessionFactory() {
if (entityManagerFactory.unwrap(SessionFactory.class) == null) {
throw new NullPointerException("factory is not a hibernate factory");
}
return entityManagerFactory.unwrap(SessionFactory.class);
}
}
依赖关系
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.2</version>
</dependency
</dependencies>
org.springframework.boot
SpringBootStarterWeb
org.springframework.boot
spring引导启动器数据jpa
org.springframework.boot
弹簧启动安全
org.springframework.boot
弹簧起动试验
测试
org.postgresql
postgresql
42.2.2
当你说
但我不能自动连接整个管理工厂
它在运行时编译或抛出异常失败吗?如果是后者,堆栈跟踪会说什么
一种可能的解决方案/解决方法是使用@PersistenceContext
注释将em注入配置而不是em工厂:
@Configuration
@EnableTransactionManagement
public class DatabaseConfiguration {
@PersistenceContext
private EntityManager entityManager;
@Bean
public SessionFactory getSessionFactory() {
// no need to check for null here because if the unwrap fails, it will do
// so by throwing a PersistenceException rather than returning null.
return entityManager.unwrap( Session.class ).getFactory();
}
}
我不太清楚为什么要公开这两个bean,因为正如@chrylis所指出的,您可以在需要的地方轻松地将EMF展开为SF
// Some spring component
@Component
public class MyFancyComponent {
@PersistenceContext
private EntityManager entityManager;
public void doSomethingFancy() {
// public SessionFactory API
final SessionFactory sf = entityManager
.unwrap( Session.class ).getFactory();
// public SessionFactoryImplementor SPI
final SessionFactoryImplementor sfi = entityManager
.unwrap( SessionImplementor.class ).getFactory();
}
}
有几项:首先,EntityManagerFactory
专门注入了@PersistenceUnit
。第二,当标准Hibernate接口现在是JPA时,为什么要手动执行此操作?最后,即使您让显示的代码正常工作,您也基本上清除了Spring的线程跟踪EntityManager
处理。它无法编译。我尝试了您的变体,但出现了下一个错误org.springframework.beans.factory.BeanCreationException:创建名为“databaseConfiguration”的bean时出错:注入持久性依赖项失败;嵌套异常为org.springframework.beans.factory.BeanCreationException:创建名为“getSessionFactory”的bean时出错