gpt4 book ai didi

spring - 定义与 JpaRepository 关联的实体管理器

转载 作者:行者123 更新时间:2023-12-02 05:48:46 25 4
gpt4 key购买 nike

我正在编写一个具有多个数据源和实体管理器的 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 注释具有 entityManagerFactoryReftransactionManagerRef 属性。

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

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

关于spring - 定义与 JpaRepository 关联的实体管理器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34800820/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com