我正在编写一个具有多个数据源和实体管理器的 Spring Boot 应用程序,我想使用 JPA CrudRepository 接口(interface),例如:
@Repository
public interface Car extends CrudRepository<Car.class, Long> {}
.
我收到以下错误:
Error creating bean with name 'carRepository': Cannot create inner bean '(inner bean)#350d0774' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#350d0774': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource
我无法弄清楚如何告诉 JpaRepositoryFactory 在构建存储库时使用哪个实体管理器。它默认尝试注入(inject)一个名为:entityManagerFactory的EntityManagerFactory
最佳答案
检查这个documentation关于属性。
在您的 Java 配置类上使用下面的 @EnableJpaRepositories
注释
具有 entityManagerFactoryRef
和 transactionManagerRef
属性。
@EnableJpaRepositories(basePackages = {
"com.myapp.repositories" }, entityManagerFactoryRef = "entityManagerFactoryRef1", transactionManagerRef = "transactionManagerRef1")
删除Car
接口(interface)上的@Repository
注释。如果您指定 basePackages
属性,Spring 会将其注册为 JPA 存储库。
关于spring - 定义与 JpaRepository 关联的实体管理器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34800820/