Spring 使用 2 个数据库配置启动 - 使用第二个配置的延迟加载不起作用

Spring Boot with 2 database configs - lazy loading with second config does not work

我有 Spring 带有 2 个数据库配置的引导项目。

主数据库配置:

@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);
    }
}

存在加载了第二个数据库的实体:

@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 时,会检索到 User,但是当我点击加载公司时,我会得到 com.sun.jdi.InvocationException occurred invoking method. - com.sun.jdi.InvocationException occurred invoking method。如果我将 SecondaryDbConfig 更改为 @Primary 或添加 fetch = FetchType.EAGER,则会按预期检索公司。我试过添加 @Transactional(transactionManager="secondaryTransactionManager") 但没有用。

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

尝试将@Transactional 添加到检索公司列表的@Service 方法或控制器中。

祝你好运!

我遇到了类似的问题。要使用多个数据库启用 JPA 实体的延迟加载,您需要在 DBConfig 类 中为每个数据库创建单独的 OpenEntityManagerInViewFilter bean。 我在这里找到了解决方案: spring-boot-jpa-multiple-data-sources 在上面的文章中搜索术语“懒惰”。

我通过在@Service 上添加@Transactional 解决了这个问题class。