本文介绍了带有2个数据库配置的Spring Boot - 使用第二个配置延迟加载不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有2个数据库配置的Spring Boot项目。

I have Spring Boot project with 2 database configs.

主数据库配置:

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(transactionManagerRef = "primaryTransactionManager", entityManagerFactoryRef = "primaryEntityManagerFactory", basePackages = { "com.example.repository.primary" })
public class PrimaryDbConfig {

    @Primary
    @Bean(name = "primaryDataSource")
    @ConfigurationProperties(prefix = "spring.primary.datasource")
    public DataSource dataSource() {
        return DataSourceBuilder.create().build();
    }

    @Primary
    @Bean(name = "primaryEntityManagerFactory")
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder,         @Qualifier("primaryDataSource") DataSource dataSource) {
        return builder.dataSource(dataSource).packages("com.example.domain.primary").persistenceUnit("primary-persistence-unit").build();
    }

    @Primary
    @Bean(name = "primaryTransactionManager")
    public PlatformTransactionManager transactionManager(@Qualifier("primaryEntityManagerFactory") EntityManagerFactory entityManagerFactory) {
        return new JpaTransactionManager(entityManagerFactory);
    }
}

次要:

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef = "secondaryEntityManagerFactory", transactionManagerRef = "secondaryTransactionManager", basePackages = { "com.example.repository.secondary" })
public class SecondaryDbConfig {

    @Bean(name = "secondaryDataSource")
    @ConfigurationProperties(prefix = "spring.secondary.datasource")
    public DataSource dataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "secondaryEntityManagerFactory")
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder,         @Qualifier("secondaryDataSource") DataSource dataSource) {
        return builder.dataSource(dataSource).packages("com.example.domain.secondary").persistenceUnit("secondary-persistence-unit").build();
    }

    @Bean(name = "secondaryTransactionManager")
    public PlatformTransactionManager transactionManager(@Qualifier("secondaryEntityManagerFactory") EntityManagerFactory entityManagerFactory) {
        return new JpaTransactionManager(entityManagerFactory);
    }
}

有一个实体加载了第二个DB:

There is entity which is loaded with second DB:

@Entity
@Table(name = "users")
public class User {

    @OneToMany(mappedBy = "user")
    private List<Company> companies;

使用:

public interface UserRepository extends JpaRepository<User, Long> {
    User findByEmail(String email);
}

当我使用 UserRepository ,用户被检索但是当我点击加载公司时,我得到 com.sun.jdi.InvocationException发生了调用方法。 - 。如果我将 SecondaryDbConfig 更改为 @Primary 或添加 fetch = FetchType.EAGER 然后按预期检索公司。我已经尝试添加 @Transactional(transactionManager =secondaryTransactionManager)但它没有帮助。

When I use UserRepository, User is retrieved but when I hit to load companies, I get com.sun.jdi.InvocationException occurred invoking method. - com.sun.jdi.InvocationException occurred invoking method. If I change SecondaryDbConfig to @Primary or add fetch = FetchType.EAGER then companies are retrieved as expected. I have tried adding @Transactional(transactionManager="secondaryTransactionManager") but it didn't help.

我错过了什么?使用非主数据库配置时,延迟加载实体属性的正确方法是什么?

What did I miss? What's the proper approach to lazy load entity properties when using not primary database config?

推荐答案

尝试将@Transactional添加到@Service检索公司名单的方法或控制人。

Try to add @Transactional to your @Service method(s) or controller which retrieved list of company.

祝你好运!

这篇关于带有2个数据库配置的Spring Boot - 使用第二个配置延迟加载不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 06:57