I try to implement Multiple Databases on MySql and Sql server2019 so i folow this this documentation spring-data-jpa-multiple-databases
but i have error in the second dataBase
Parameter 0 of constructor in com.hexa.infrastructure.doa.SecondAdapter required a bean of type 'com.hexa.infrastructure.doa.ISecondRepositoryJpa' that could not be found.
@NoArgsConstructor
@Configuration
@EnableJpaRepositories(
basePackages = "com.hexa.infrastructure.doa.Second",
entityManagerFactoryRef = "secondEntityManager",
transactionManagerRef = "secondTransactionManager")
public class PersistencesecondAutoConfiguration {
@Autowired
private Environment env;
@Bean
@ConfigurationProperties(prefix="spring.datasource-mysql")
public DataSource secondDataSource() {
return DataSourceBuilder.create().build();
}
@Bean
public LocalContainerEntityManagerFactoryBean secondEntityManager() {
final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(secondDataSource());
em.setPackagesToScan("com.hexa.infrastructure.jpa.entities.sqlserver.SecondEntity");
final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
final HashMap<String, Object> properties = new HashMap<String, Object>();
properties.put("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
properties.put("hibernate.dialect", env.getProperty("hibernate.dialect"));
em.setJpaPropertyMap(properties);
return em;
}
@Bean
public PlatformTransactionManager secondTransactionManager() {
final JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(secondEntityManager().getObject());
return transactionManager;
}
}
@Configuration
@EnableJpaRepositories(
basePackages = "com.hexa.infrastructure.doa.FirstAdapter",
entityManagerFactoryRef = "firstEntityManager",
transactionManagerRef = "firstTransactionManager"
)
public class PersistenceFirsyConfiguration {
@Autowired
private Environment env;
@Primary
@Bean
@ConfigurationProperties(prefix="spring.datasource")
public DataSource firstDataSource() {
return DataSourceBuilder.create().build();
}
@Primary
@Bean
public LocalContainerEntityManagerFactoryBean firstEntityManager() {
final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(firstDataSource());
em.setPackagesToScan("com.hexa.infrastructure.jpa.entities.mysql");
final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
final HashMap<String, Object> properties = new HashMap<String, Object>();
properties.put("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
properties.put("hibernate.dialect", env.getProperty("hibernate.dialect"));
em.setJpaPropertyMap(properties);
return em;
}
@Primary
@Bean
public PlatformTransactionManager firstTransactionManager() {
final JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(firstEntityManager().getObject());
return transactionManager;
}
}
I don't understand why it can't find the bean
I have test on a single Entity and on the package my same error.
I also changed invercy the primary annotation same result
if i change to basePackages = "com.hexa.infrastructure.doa"
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'secondEndPoint' defined in file [\\\com\hexa\application\SecondEndPoint.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'secondService' defined in file [\\\com\hexa\domain\SecondService.class]: Unsatisfied dependency expressed through constructor parameter 1; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'secondAdapter' defined in file [\\\com\hexa\infrastructure\doa\SecondAdapter.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ISecondRepositoryJpa' defined in com.hexa.infrastructure.doa.ISecondRepositoryJpa defined in @EnableJpaRepositories declared on PersistenceSecondAutoConfiguration: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class com.hexa.infrastructure.jpa.entities.sqlserver.SecondEntity
now it work but when i call api now i have sql error,
First dabase With data
java.sql.SQLSyntaxErrorException: (conn=457) Unknown column 'firstent0_.dateCreation' in 'field list'
new base Second
com.microsoft.sqlserver.jdbc.SQLServerException: Invalid object name 'entity2'.