1

I'm trying to write an application that accesses data from two sources. I'm using Spring Boot 2.3.2. I've looked at several sources for info about how to configure the app: the Spring documentation talks about setting up multiple datasources, but does not explain how to link up JPA repositories. This Baeldung article goes a lot further, but I'm looking to take advantage of autoconfiguration in Spring.

So far, I've created a separate package, added a config class (along with model and repositories), and included this package in scanBasePackages so that it's picked up. Since I'll have more than one datasource, I've added this to my @SpringBootApplication:

@Bean
@Primary
public DataSourceProperties dataSourceProperties() {
   return new DataSourceProperties();
}

This successfully loads up my Spring app using the standard spring config values. The two databases are on different servers, but should share characteristics (other than url and credentials).

So, my auxiliary configuration file looks like this

@Configuration
@EnableAutoConfiguration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef = "orgEntityManagerFactory",
        transactionManagerRef = "orgTransactionManager",
        basePackages = {
                "pacage2.repositories"
        }
)
public class DataSourceConfiguration {
// added because of this answer: https://stackoverflow.com/a/51305724/167889
    @Bean
    public EntityManagerFactoryBuilder entityManagerFactoryBuilder() {
        return new EntityManagerFactoryBuilder(new HibernateJpaVendorAdapter(), new HashMap<>(), null);
    }

    @Bean
    @ConfigurationProperties(prefix = "external.datasource")
    public DataSourceProperties orgDataSourceProperties() {
        return new DataSourceProperties();
    }

    @Bean
    public HikariDataSource orgDataSource(@Qualifier("orgDataSourceProperties") DataSourceProperties properties) {
        return properties.initializeDataSourceBuilder().type(HikariDataSource.class)
                .build();
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean orgEntityManagerFactory(EntityManagerFactoryBuilder builder,
                                                                          @Qualifier("orgDataSource") DataSource dataSource) {
        return builder
                .dataSource(dataSource)
                .packages("package2.model")
                .build();
    }

    @Bean
    public PlatformTransactionManager orgTransactionManager(
            @Qualifier("orgEntityManagerFactory") EntityManagerFactory entityManagerFactory
    ) {
        return new JpaTransactionManager(entityManagerFactory);
    }

}

Now, the error I'm getting right now is Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set. However, I have that value in my config and it's applied by the Spring auto config. I believe it needs to be set in the EntityManagerFactoryBuilder and by creating my own, the autoconfig isn't getting applied.

How can I have my cake and eat it too? I'd like to leverage as much of the robust autoconfiguration that Spring provides to setup datasources and wire them to the appropriate repositories. Effectively, all that I want to change is the url and credentials, and I can separate the entities and repositories into a completely separate package for easy scanning.

end-user
  • 2,845
  • 6
  • 30
  • 56
  • Maybe it helps: https://stackoverflow.com/questions/26548505/org-hibernate-hibernateexception-access-to-dialectresolutioninfo-cannot-be-null – alex87 Aug 24 '20 at 17:44

0 Answers0