定义哪个实体管理器与 JpaRepository 关联

Define which entity manager is associated with JpaRepository

我正在编写一个 Spring 启动应用程序,它有多个数据源和实体管理器,我想使用 JPA CrudRepository 接口,如:

@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 在构建存储库时使用哪个实体管理器。它默认尝试注入一个名为:entityManagerFactory

的 EntityManagerFactory

您可以在 spring.xml

中做什么
    <jpa:repositories base-package="com.a.b.repository"
    entity-manager-factory-ref="yrEntityMAnagerFectorybeanRef"
    transaction-manager-ref="yourTransactionManagerBeanRef"/>

在您的 EntityManagerFactory bean 中,您将引用您的 entitymanager bean。

在属性上检查此 documentation

在您的 Java 配置中使用下面的 @EnableJpaRepositories 注释 class 具有 entityManagerFactoryReftransactionManagerRef 属性。

@EnableJpaRepositories(basePackages = {
        "com.myapp.repositories" }, entityManagerFactoryRef = "entityManagerFactoryRef1", transactionManagerRef = "transactionManagerRef1")

删除 Car 接口上的 @Repository 注释。如果指定 basePackages 属性,Spring 会将其注册为 JPA 存储库。