-1

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'.
Jack Boch
  • 127
  • 1
  • 9
  • 1
    `com.hexa.infrastructure.doa.SecondAdapter` has no empty constructor, thus to create an instance, spring needs an instance of `com.hexa.infrastructure.doa.ISecondRepositoryJpa` which does not exist – XtremeBaumer Sep 15 '22 at 09:16
  • I do not understand it works very well when there is only one base. and the fisrt entity is built on the same principle – Jack Boch Sep 15 '22 at 09:33
  • Does this answer your question? [Spring boot - Not a managed type](https://stackoverflow.com/questions/28664064/spring-boot-not-a-managed-type) – XtremeBaumer Sep 15 '22 at 09:51

1 Answers1

0

Your basepackage in @EnableJpaRepositories

@EnableJpaRepositories(
        basePackages = "com.hexa.infrastructure.doa.Second"

Means it won't find your Repository class which is is in package

com.hexa.infrastructure.doa.ISecondRepositoryJpa
johnnyutts
  • 1,349
  • 1
  • 8
  • 11
  • not work same error – Jack Boch Sep 15 '22 at 09:26
  • Did you change to basePackages = "com.hexa.infrastructure.doa" ? – johnnyutts Sep 15 '22 at 09:30
  • i try basePackages = "com.hexa.infrastructure.doa.ISecondRepositoryJpa" and if i change I have a similar exception which I put in my question – Jack Boch Sep 15 '22 at 09:46
  • After changing package to com.hexa.infrastructure.doa, your error is showing that SecondEntity is not a manged type. Add @EntityScan('com.hexa.infrastructure.jpa.entities') – johnnyutts Sep 15 '22 at 10:05
  • change to em.setPackagesToScan("com.hexa.infrastructure.jpa.entities.sqlserver"); – Jack Boch Sep 15 '22 at 11:51
  • But now I have error sql does not exist before splitz database – Jack Boch Sep 15 '22 at 11:53
  • Add @EntityScan('com.hexa.infrastructure.jpa.entities') to the same classes you added @EnableJpaRepositories – johnnyutts Sep 15 '22 at 12:06
  • @EntityScan is useless because I feel like setPackagesToScan has the last word. And now I no longer have a bean problem, but a sql problem like if config and badly change or different interpretation – Jack Boch Sep 15 '22 at 12:24