Multiple Databases With Shared Entity Classes in Spring Boot and Java

DZone 's Guide to

Multiple Databases With Shared Entity Classes in Spring Boot and Java

We take a look at how to use a single implemented model between multiple databases in this post using Spring Boot. Interested? Read on for the details.

· Java Zone ·
Free Resource

Hello, everyone, It has been a few months since my last post. I have been busy traveling and relocating. In this post, I want to illustrate how a Spring Boot application can have multiple data sources with shared entity classes. The need for this arose in my current project where an in-memory database was needed for the high performance and a persistent database for storage.

In this blog post, I will use H2 for the in-memory database and Postgres for the persistent storage. I will setup up the application and show how the entities can be passed from one data source to another.

The application will be a Spring Boot application using Java 8 with REST enabled. I will highlight the important parts and classes of the application. The link to the GitHub code is given below. Let's get started.

The application will save a Person entity to an in-memory database and to a persistent database via a REST POST call. We create our entity,  Person.java:

For the sake of brevity, I leave out the Getters and Setters.

Next, we set up the two connections for the two databases. I will leave out the reading of the properties but highlight the important parts of the classes, InMemoryDatabaseConfiguration.java and  PeresistentDatabaseConfiguration.java:

As you can see from the two classes, the same data model package is used com.gabriel.multipledatabaseconnection.model. This means that the two databases will share the sameEntities, and within the application, they can be passed between the data sources.

Next, we configure our repositories. These are very simple and just extend Spring's JpaRepository:

The last step is to set up a controller, which I call theMutlipleDatabaseController. We will create a new person in both databases, but simply passing the new person entity from the in-memory database to the persistent database:

Note that the transaction managers are defined for the respective data sources and annotate the respective methods.

Last, we test. For this, I will use the SoapUI, the open-source version. To make a REST call to create John Connor, we will perform the following:

If we check in our databases using the pgAdmin4 and H2 web console:

There you have it — one REST call two data sources one entity persisted in both. All source code can be found on GitHub.

Note: I have also added the .idea files using IntelliJ (this is for the open-source version of IntelliJ).

databases ,entity ,h2 ,java ,model ,postgres ,spring boot

Published at DZone with permission of Gabriel Campbell , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}