DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • How To Build Web Service Using Spring Boot 2.x
  • Redis-Based Tomcat Session Management
  • Introduction to Spring Boot and JDBCTemplate: JDBC Template
  • Keep Your Application Secrets Secret

Trending

  • S3 Vectors: How to Build a RAG Without a Vector Database
  • Building a DevOps-Ready Internal Developer Platform: A Hands-On Guide to Golden Paths, Self-Service, and Automated Delivery Pipelines
  • Integrating AI-Driven Decision-Making in Agile Frameworks: A Deep Dive into Real-World Applications and Challenges
  • Building Production-Grade GenAI on GCP with Vertex AI Agent Builder
  1. DZone
  2. Data Engineering
  3. Databases
  4. Multiple Databases With Shared Entity Classes in Spring Boot and Java

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.

By 
Gabriel Campbell user avatar
Gabriel Campbell
·
Updated Nov. 09, 18 · Tutorial
Likes (18)
Comment
Save
Tweet
Share
50.4K Views

Join the DZone community and get the full member experience.

Join For Free

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).

Database Spring Framework Spring Boot In-memory database application Java (programming language) Open source

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

Opinions expressed by DZone contributors are their own.

Related

  • How To Build Web Service Using Spring Boot 2.x
  • Redis-Based Tomcat Session Management
  • Introduction to Spring Boot and JDBCTemplate: JDBC Template
  • Keep Your Application Secrets Secret

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook