1

I have two databases that have the exact same tables. They have the same model. I want to create a simple Spring Boot application that will copy some records from one DB to the other. Both DBs are form the same vendor.

Ideally I would like to have one domain model and simply assign the adequate datasource based on my needs, possibly through the @Qualified annotation.

Another workaround would be to duplicate the domain model and create two configurations for each database.

What is the correct way of solving this problem?

Thank you in advance

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
Investigator
  • 1,431
  • 2
  • 17
  • 24
  • See https://stackoverflow.com/questions/48674412/multiple-data-sources-with-same-entity-and-repo/48675156#48675156. And resources : http://tech.asimio.net/2017/01/17/Multitenant-applications-using-Spring-Boot-JPA-Hibernate-and-Postgres.html and https://rjbtechnology.com/blog/2017-01/discriminator-multitenancy-with-spring-and-hibernate-no-filters/. – cactuschibre Jun 27 '18 at 07:59

1 Answers1

2

You should create a project with two modules: one - with your model, another - with two data sources and repos for every entity in these DBs. Then in the second module you can work with data from two data sources.

For example:

Project:
  - module: 'model':
    - entity1
    - entity2
  - module: 'work'
    - data source #1:
      - repo1_1
      - repo1_2
    - data source #2:
      - repo2_1
      - repo2_2

Additional info: 1, 2.

Cepr0
  • 28,144
  • 8
  • 75
  • 101
  • 1
    Thank you Cepr0. I have created a small demo for this purpose: https://github.com/klearchos/spring-boot-multiple-data-sources – Investigator Feb 28 '18 at 08:35
  • @Investigator By and large, it is not necessary to use several modules (this becomes important in a large project), you can do everything just in one module... The main thing is that two data sources must refer to the same model, but to different repositories. – Cepr0 Feb 28 '18 at 08:44
  • @Investigator, in your project the package com.example.target.repo is missing... Anyone have a better solution ? I need 6 datasources for 1 model. I would like avoiding duplicates the repositories... – cactuschibre Jun 25 '18 at 15:31
  • Hi @fandango thank you for the information I will check it. I had to replicate the repositories. – Investigator Jun 25 '18 at 16:04