Есть приложение на spring boot, которое использует подключение к двум бд: application.properties
spring.datasource.url=jdbc:h2:mem
spring.datasource.username=
spring.datasource.password=
spring.datasource.driver-class-name=org.h2.Driver
another.datasource.url=jdbc:mysql://localhost:3306/test
another.datasource.username=root
another.datasource.password=
another.datasource.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
another.datasource.driverClassName=com.mysql.jdbc.Driver
RepositoryConfig
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "anotherEntityManagerFactory",
transactionManagerRef = "anotherTransactionManager",
basePackages = {"test.repository.mysql"})
public class RepositoryConfig {
@Autowired
JpaVendorAdapter jpaVendorAdapter;
@Value("${another.datasource.url}")
private String databaseUrl;
@Value("${another.datasource.username}")
private String username;
@Value("${another.datasource.password}")
private String password;
@Value("${another.datasource.driverClassName}")
private String driverClassName;
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource(databaseUrl, username, password);
dataSource.setDriverClassName(driverClassName);
return dataSource;
}
@Bean(name = "anotherEntityManager")
public EntityManager entityManager() {
return entityManagerFactory().createEntityManager();
}
@Bean(name = "anotherEntityManagerFactory")
public EntityManagerFactory entityManagerFactory() {
Properties properties = new Properties();
LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
emf.setDataSource(dataSource());
emf.setJpaVendorAdapter(jpaVendorAdapter);
emf.setPackagesToScan("test.entity.mysql"); // <- package for entities
emf.setPersistenceUnitName("anotherEntity");
emf.setJpaProperties(properties);
emf.afterPropertiesSet();
return emf.getObject();
}
@Bean(name = "anotherTransactionManager")
public PlatformTransactionManager transactionManager() {
return new JpaTransactionManager(entityManagerFactory());
}
}
При такой конфигурации работает подключение к двум бд, но в дефолтном datasource
используются все entity
, как сделать, чтобы первый datasource
, который в application.properties как spring.datasource
, использовал только свои entity
?