springspring-bootjdbcdatasourcespring-jdbc

EntityCallback not working at all after configuring multiple jdbc data sources and repositories


This is a non trivial, at least for me, problem I've been trying to solve for weeks now.

After configuring correctly two different datasources using spring data jdbc and manually set a repository bean factory, now I can't manage any entity functionality.

Here is my foo datasource config:

@Configuration
public class BarConfig {

    @Bean
    @ConfigurationProperties("spring.datasource.bar")
    public DataSourceProperties barDataSourceProperties() {
        return new DataSourceProperties();
    }

    @Bean
    @ConfigurationProperties(prefix = "spring.datasource")
    public HikariDataSource barHikariDataSource() {
        return barDataSourceProperties().initializeDataSourceBuilder().type(HikariDataSource.class).build();
    }

    @Bean
    public DataSource barDataSource() {
        return barDataSourceProperties()
                .initializeDataSourceBuilder()
                .build();
    }

    @Bean
    public NamedParameterJdbcTemplate barNamedParameterJdbcTemplate(@Qualifier("barDataSource") DataSource dataSource) {
        return new NamedParameterJdbcTemplate(dataSource);
    }

    @Bean
    public DataAccessStrategy barDataAccessStrategy(@Qualifier("barNamedParameterJdbcTemplate") NamedParameterJdbcOperations operations,
                                                    JdbcConverter jdbcConverter,
                                                    JdbcMappingContext context,
                                                    Dialect dialect) {

        final SqlGeneratorSource sqlGeneratorSource =
                new SqlGeneratorSource(
                        context,
                        jdbcConverter,
                        dialect
                );

        final DataAccessStrategyFactory factory =
                new DataAccessStrategyFactory(
                        sqlGeneratorSource,
                        jdbcConverter,
                        operations,
                        new SqlParametersFactory(context, jdbcConverter),
                        new InsertStrategyFactory(operations, dialect)
                );

        return factory.create();
    }
}

My repository factory:

@Configuration
public class BarRepoConfig {

    private final JdbcRepositoryFactory barRepositoryFactory;

    public BarRepoConfig(
            @Qualifier("barDataAccessStrategy") DataAccessStrategy dataAccessStrategy,
            RelationalMappingContext context,
            JdbcConverter converter,
            Dialect dialect,
            ApplicationEventPublisher publisher,
            @Qualifier("barNamedParameterJdbcTemplate") NamedParameterJdbcTemplate ldapNamedParameterJdbcTemplate) {

        this.barRepositoryFactory = new JdbcRepositoryFactory(
                dataAccessStrategy,
                context,
                converter,
                dialect,
                publisher,
                ldapNamedParameterJdbcTemplate);
    }

    @Bean
    @Primary
    public BarRepo barRepositoryTest() {
        return barRepositoryFactory.getRepository(BarRepo.class);
    }

}

So, whenever I try to implement any EntityCallback like BeforeDeleteCallback or AfterSaveCallback it does not work.

This is the repository: https://github.com/Albertlorseg/spring-boot-jdbc-multiple-datasources-repositories

I tried to manually configure an entity manager but it's something from JPA.


Solution

  • Turns out you need to manually create a JdbcAggregateTemplate bean using ApplicationContext