;
Я выполняю сценарий таким образом
Код: Выделить всё
@Transactional
public void executeSqlScript() throws SQLException {
for (Map.Entry entry : dataSources.entrySet()) {
ScriptUtils.executeSqlScript(entry.getValue().getConnection(), new ClassPathResource("test.sql"));
}
}
Код: Выделить всё
TestTable testTable = testTableRepository.findLatest();
Вот как загружаются мои источники данных:
Код: Выделить всё
CONFIG::
datasource:
connectionPrefix: jdbc:mysql://localhost:3306/
username: root
password: root
databases:
- demo1
- demo2
- demo3
- demo4
@Bean
public Map localDataSources() throws SQLException {
Map dataSources = new HashMap();
for (String db : dataSourceConfigProperties.getDatabases()) {
MysqlDataSource dataSource = new MysqlDataSource();
dataSource.setUser(dataSourceConfigProperties.getUsername());
dataSource.setPassword(dataSourceConfigProperties.getPassword());
dataSource.setUrl(dataSourceConfigProperties.getConnectionPrefix() + db);
dataSources.put(dataSourceConfigProperties.getConnectionPrefix() + db, dataSource);
return dataSources;
}
@Bean
public Map entityManagerFactories(Map dataSources) {
Map entityManagerFactories = new HashMap();
for (Map.Entry entry : dataSources.entrySet()) {
LocalContainerEntityManagerFactoryBean emFactoryBean = new LocalContainerEntityManagerFactoryBean();
emFactoryBean.setDataSource(entry.getValue());
emFactoryBean.setPackagesToScan("com.test.mangager.model"); // Set the package to scan for entities
emFactoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
Map properties = new HashMap();
properties.put("hibernate.hbm2ddl.auto", "update");
properties.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
emFactoryBean.setJpaPropertyMap(properties);
emFactoryBean.afterPropertiesSet();
entityManagerFactories.put(entry.getKey(), emFactoryBean);
}
return entityManagerFactories;
}
@Bean
public Map entityManagers(Map entityManagerFactories) {
Map entityManagers = new HashMap();
for (Map.Entry entry : entityManagerFactories.entrySet()) {
EntityManager em = entry.getValue().getObject().createEntityManager();
entityManagers.put(entry.getKey(), em);
}
return entityManagers;
}
@Bean
public Map transactionManager(Map dataSources) {
Map transactionManagers = new HashMap();
for (Map.Entry entry : dataSources.entrySet()) {
transactionManagers.put(entry.getKey(), new DataSourceTransactionManager(entry.getValue()));
}
return transactionManagers;
}
Ошибка создания bean-компонента с именем testTableRepository, определенным в com.test.manager.repository.TestTableRepository, определенном в @EnableJpaRepositories, объявленном в приложении: Невозможно разрешить ссылку на bean-компонент «jpaSharedEM_entityManagerFactory» при настройке свойства bean-компонента «entityManager»
Подробнее здесь: https://stackoverflow.com/questions/787 ... e-with-jpa