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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

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

  • Secure by Design: Modernizing Authentication With Centralized Access and Adaptive Signals
  • Kullback–Leibler Divergence: Theory, Applications, and Implications
  • 5 Subtle Indicators Your Development Environment Is Under Siege
  • Event-Driven Architectures: Designing Scalable and Resilient Cloud Solutions
  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
49.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, DZone MVB. 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
Oops! Something Went Wrong

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!