Multiple datasource for one model - spring

I had one database from which I was taking the data in my spring boot microservice loading that into a Request[Model] object. Now I have 2 database both contains the same type of data. How can i use multiple databases in spring boot under one model only.

Found the solution. You have to create 2 Entity manager, 2 Transaction manager, 2 Datasource config classes and 2 Repository in 2 different folders. Here is my code
https://github.com/j-arpit/MULTIDATABASE.git

Related

How to change schema in Spring JPA based on request

I have a multitenant system currently working with python. I have different schemas for different tenants. Now we are planning to change the system to spring boot.
How can we change/select the schema based on the request in JPA/Hibernate?
You can set the following property dynamically -
spring.jpa.properties.hibernate.default_schema=MYSCHEMA. For more advanced concept of using multiple datasources take a look at AbstractRoutingDatasource

Different Springboot services using a same tables

We have a running Springboot service A that created some relational entities using Spring JPA with Hibernate ORM.
We need to create a new Springboot service B that needs to access A's tables but with different queries.
There are few options I though of:
Making service B use Spring JPA and Hibernate and copy the same entity models from service A
But I'm not sure if this method causes any synchronization issues caused by Hibernate's first level caching.
Both services will not be using 2nd level cache.
Same like option 1, making service B use Spring JPA and Hibernate but import service A as a dependency in service B instead of copying the entity models.
Making service B use Spring JdbcTemplate if we are not creating any new entities in service B.
I also like to know how service B's table can have a unidirectional foriegn key relationship (#ManyToOne or #OneToOne) with service A's table.
Please suggest me which option is better or if there's a better way.
If it is a bad pratice to use other service's tables, please suggest the correct design. \
Thanks

How can I re generate a new JPA entity(and a database table in a different database schema) for an existing jhipster project?

How can I generate an entity(and a database table) in a different schema other than the default public schema for an existing Jhipster(4.6.2) project generated with spring boot and AngularJS as the technology stack. [Database - Postgresql]
I am very interested if it is possible. Otherwise one simple way to reach that: you could try to generate the entity using the jhipster command jhipster entity <entityName> --[options] see for more details.
And customize your application to use multiple databases by following this excellent article: https://www.baeldung.com/spring-data-jpa-multiple-databases

Spring boot - connect hibernate with one oracle huge table

I am creating a spring boot application and I only have one table in my oracle database (one table with more than 80 fields...), so, what is the best way to implement this in my spring boot-groovy app?
Do I need to have one entity with 80 attributes??
Can I have a hibernate entity model different from the awful oracle model? (having more than one entity in my app but connected with the same huge table).
Any ideas or tools are more than welcome.

Several Read-Only DB Connections in Spring and Hibernate

We have a Spring+Hibernate application (using Spring 2, from AppFuse 1.9) which is in a desperate need to be updated to Spring 3. We're slowly working on that.
In the meantime, I'd like to take some of the load off our primary database server, and set up the read-only controllers (which just display information) to read from our database slaves.
More specifically, we have multiple databases servers (master+slaves), and I'd like to be able to set up multiple database connections, and then specify that controller1 uses db1, and controller's 2 and 3 use db2.
How can we achieve this?
You should be able to do that with AbstractRoutingDataSource class in Spring. This blog should help you. You can wire each data source for each of your controllers.

Resources