0

I am using multiple datasource in my Spring Boot application.

In @EnableJpaRepositories, how do I add the base packages efficiently if I have a project structure like below:

Note - Repositories under all modules(module1, module2, module3) of com.project needs to access db1 except the repository under package(com.project.module3.submodule.repository) which needs to access db2.

com.project
     |
     com.project.module1.controller
     |
     com.project.module1.entity
     |
     com.project.module1.repository
     |
     com.project.module2.controller
     |
     com.project.module2.entity
     |
     com.project.module2.repository
     |
     com.project.module3.controller
     |
     com.project.module3.entity
     |
     com.project.module3.repository
     |
     com.project.module3.submodule.controller
     |
     com.project.module3.submodule.entity
     |
     com.project.module3.submodule.repository

What I tried so far:

//Method 1 works fine
//DB1 Config
@EnableJpaRepositories( entityManagerFactoryRef = //entityFactory//, transactionManagerRef = //transactionManager//,    basePackages = {"com.project.module1.repository", "com.project.module2.repository", "com.project.module3.repository" })

//DB2 Config 
@EnableJpaRepositories( entityManagerFactoryRef = //entityFactory//, transactionManagerRef = //transactionManager//,    basePackages = {"com.project.module3.submodule.repository" })

---------------------

//Method 2 throws below error
//DB1 Config 
@EnableJpaRepositories( entityManagerFactoryRef = //entityFactory//, transactionManagerRef = //transactionManager//,    basePackages = {"com.project"})

//DB2 Config (works fine)
@EnableJpaRepositories( entityManagerFactoryRef = //entityFactory//, transactionManagerRef = //transactionManager//,    basePackages = {"com.project.module3.submodule.repository" })

APPLICATION FAILED TO START
***************************

Description:

The bean 'SubmoduleRepo', defined in com.project.module3.submodule.repository.SubmoduleRepo defined in @EnableJpaRepositories declared on Db1Config, could not be registered. A bean with that name has already been defined in com.project.module3.submodule.repository.SubmoduleRepo defined in @EnableJpaRepositories declared on Db2Config and overriding is disabled.

Action:

Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true

Instead of specifying all the module repository packages in the DB1Config, is there a way to exclude submodule package. Because I have many module packages and its cumbersome to mention each repository package. How do I do this?

Thank you in advance!

code-geek
  • 441
  • 1
  • 8
  • 22
  • 1
    Well you shouldn't, really. The intended use is to let it happen automagically by creating logically grouped packages so you for example have a package 'db1' under which module1,2,3 live and a package 'db2' under which a module4 lives, which would be your module3.submodule. Then you can put configuration classes in packages db1 and db2 and give each their own EnableJpaRepositories. Like this answer describes in great detail: https://stackoverflow.com/a/45665826/424903 . I would really recommend restructuring so you can work with Spring Boot instead of fight against it. – Gimby Dec 03 '20 at 13:18
  • 1
    Does this answer your question? [Spring Data JPA - Multiple EnableJpaRepositories](https://stackoverflow.com/questions/45663025/spring-data-jpa-multiple-enablejparepositories) – Gimby Dec 03 '20 at 13:40
  • (sorry for the double comment referring to the same link, the second comment happens automatically when voting to close as a duplicate). – Gimby Dec 03 '20 at 13:41
  • Yes It answered my question. Thank you – code-geek Dec 03 '20 at 13:44

0 Answers0