春季未选择主要实体经理工厂

发布于 2025-02-01 21:54:34 字数 1794 浏览 1 评论 0原文

配置了两个数据源。主要EM工厂:

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
    basePackages = {"com.example.data.postgresql"},
    entityManagerFactoryRef = "postgreSqlEntityManagerFactory",
    transactionManagerRef = "postgreSqlEntityTransactionManager"
)
public class PostgreSqlJpaConfig {
...
@Bean
@Primary
public LocalContainerEntityManagerFactoryBean postgreSqlEntityManagerFactory(
        @Qualifier("postgreSqlDataSource") DataSource dataSource,
        EntityManagerFactoryBuilder builder){
    return builder.dataSource(dataSource).packages("com.example.postgresql").build();
}

辅助EM Factory:

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        basePackages = {"com.example.data.greenplum"},
        entityManagerFactoryRef = "greenPlumEntityManagerFactory",
        transactionManagerRef = "greenPlumEntityTransactionManager"
)
public class GreenPlumJpaConfig {

@Bean
public LocalContainerEntityManagerFactoryBean greenPlumEntityManagerFactory(
        @Qualifier("greenPlumDataSource") DataSource dataSource,
        EntityManagerFactoryBuilder builder){
    return builder.dataSource(dataSource).packages("com.example.greenplum").build();
}

服务在com.example.service中。有时他们会使用实体管理器,而春天不能在两个之间进行选择:

***************************
APPLICATION FAILED TO START
***************************

Description:

Field entityManager in ... required a single bean, but 2 were found:

我可以在每个EM注入中手动添加一个预选赛以使其正常工作,

@Autowired
public void setEntityManager(@Qualifier("postgreSqlEntityManagerFactory") EntityManager entityManager) {
    this.entityManager = entityManager;
}

但是应用程序中有120多个EM注射告诉Spring在未明确指定的所有情况下使用默认主要EM。

There are two datasources configured. Primary em factory:

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
    basePackages = {"com.example.data.postgresql"},
    entityManagerFactoryRef = "postgreSqlEntityManagerFactory",
    transactionManagerRef = "postgreSqlEntityTransactionManager"
)
public class PostgreSqlJpaConfig {
...
@Bean
@Primary
public LocalContainerEntityManagerFactoryBean postgreSqlEntityManagerFactory(
        @Qualifier("postgreSqlDataSource") DataSource dataSource,
        EntityManagerFactoryBuilder builder){
    return builder.dataSource(dataSource).packages("com.example.postgresql").build();
}

Secondary em factory:

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        basePackages = {"com.example.data.greenplum"},
        entityManagerFactoryRef = "greenPlumEntityManagerFactory",
        transactionManagerRef = "greenPlumEntityTransactionManager"
)
public class GreenPlumJpaConfig {

@Bean
public LocalContainerEntityManagerFactoryBean greenPlumEntityManagerFactory(
        @Qualifier("greenPlumDataSource") DataSource dataSource,
        EntityManagerFactoryBuilder builder){
    return builder.dataSource(dataSource).packages("com.example.greenplum").build();
}

Services lays in com.example.service. Sometimes they use entity manager, and Spring can't choose between two:

***************************
APPLICATION FAILED TO START
***************************

Description:

Field entityManager in ... required a single bean, but 2 were found:

I can manually add a qualifier to the every em injection to make it work,

@Autowired
public void setEntityManager(@Qualifier("postgreSqlEntityManagerFactory") EntityManager entityManager) {
    this.entityManager = entityManager;
}

but there are 120+ em injections in the app so I hope there should be another way to tell Spring to use the default primary em in all cases where it is not specified explicitly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

樱娆 2025-02-08 21:54:34

问题是过时的春季靴子。升级到2.6.2解决了问题。

The problem was the outdated Spring Boot. Upgrade to 2.6.2 solved the issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文