Multiple Data Sources with same entity and repo - spring

Currently working on a project where my Spring Boot project needs to
leverage multiple data sources or schema in the same DB server. I have
found several tutorials that teach multiple data source configuration in
spring boot where entity foo exists in DataSource A and bar exists in
DataSource B namely below.,
https://scattercode.co.uk/2016/01/05/multiple-databases-with-spring-boot-
and-spring-data-jpa/
https://scattercode.co.uk/2013/11/18/spring-data-multiple-databases/
https://medium.com/#joeclever/using-multiple-datasources-with-spring-boot-and-spring-data-6430b00c02e7
But my use case is that entities foo and bar are present in multiple schema and I want to use a single entity and repository to access all schema.Data is not replicated in all schema.It is divided among them.
So if I need to search for User John Doe I have to go through Schema 1 and
if I don't find him, move onto the next schema.
I have tried all the above tutorials(even though they don't line up with my
use case) with the hope that I could hack it to get it working just as a
proof of concept.
I have also looked into AbstractRoutingDataSource
(http://fizzylogic.nl/2016/01/24/make-your-spring-boot-application-multi-tenant-aware-in-2-steps/ , http://kimrudolph.de/blog/spring-datasource-routing)
and MultiTentancy but both of these talk about having access to a single
schema at any point in time.
I just need some guidance or link to follow and get this accomplished.
Thanks in advance.

You need to look at AbstractRoutingDataSource and use it.
So if I need to search for User John Doe I have to go through Schema 1 and if I don't find him, move onto the next schema.
Thus you need to search in first schema and if not found, then go on to next schema.
In that example as given in the above link,
CustomerContextHolder.setCustomerType(CustomerType.GOLD);
List<Item> items = catalog.getItems();
if(isEmpty(goldItems)){
CustomerContextHolder.setCustomerType(CustomerType.SILVER);
items = catalog.getItems();
}
More details can be found in another qn here

Managed to resolve the issue by using https://github.com/wmeints/spring-multi-tenant-demo.
Thanks #surya for your recommendation.

Related

How can i maintain different query files for different countries in Spring boot

I am working on a multi-country application where I have different db schemas for each country. I want to understand how can i call a different query file based on the country a user belongs to.. One way i thought of was maintaining different query files for different countries (IqueryConstantsIndia, IQueryConstntChina etc) and to check the country of the user in the constructor of the repository class and then call the query file based on the same but how do i return a class name is what i am stuck with.. Can anyone help on this or if you think there is a better way of implementing this.

Update entity with OneToMany relationship with JPA/Spring

I've been researching this for a while and still couldn't find a satisfactory answer for my problem.
I have an entity on my postgres DB (product) that has a ManyToOne relationship with another entity (Dun).
Each product may have N duns.
On my PUT endpoint, the desired behavior would be:
Every time I update a product, it replaces all the duns with the ones provided on the endpoint.
Is there any way to handle it automatically by Hibernate/JPA?
In order to make it easier to test and explain the issue, I've uploaded a project on Github on the following link https://github.com/brunapereira/jpaexample
If there's no way to handle it automatically by JPA, what's the best way to solve it with code?
Thanks in advance,
First, you need to remove Duns by product id, then get the product by id, add the new duns then save back.

How to create custom SpringBoot Crud Repository Methods to find specific details?

So i have 5 tables as Users,Orders,Products,Ingredients and nutrients,
Each is connected to adjacent table via #ManyToOne annotation.
What i am stuck at is how to find product details of a particular user for a particular order and similarly finding ingredients for a particular product for a particular order for a particular user and similarly for nutrients.
I know i have to declare custom crud methods according to a given syntax and i did so but still the method is not working.
Here is the link to the git repository
https://github.com/Guneet007/API.git
You can use Specification/Querydsl for defining custom criteria. Because we should use more than 3 parameters with AND suffix.
One more issue I have found in your code is that your property name not match with your method findByProductId() it should be findByProduct(Product product). Look same for others too.
https://spring.io/blog/2011/04/26/advanced-spring-data-jpa-specifications-and-querydsl/

when I try to add a second field set to an entity it doesn't show in the screens

I have six entities and I want to create two field sets from one to two others. One field set is ONE_TO_MANY and I used field set in the ONE side to build the relation and the other is a MANY_TO_MANY and I tried the simplest example from Northwind and that didn't work, and I tried adding --joinTable, --joinColumns, etc. and that didn't work either. Only the ONE_TO_MANY works and only one of those.
Is there a good example with Roo 2.0.0.RC1 that works? or a manual work around?
Remember that Spring Roo just generates and maintains the code that he knows. In this case, you are trying to create a really specific structure, so I recommend you to create it manually. Spring Roo doesnt't have any problem with custom code.
You could check the following repository where exists a lot of examples about relation management managed by Spring Roo or manually.
https://github.com/DISID/disid-proofs
Hope it helps,

Can I commit a portion of an #Transactional sequence?

I have a Spring Boot application, and have a webservice where a user can POST a model of a CollegeCourse instance which includes links between that class and the Students who are taking it. (The data is used to store rows in the association table, since those classes have a many-to-many relationship.) This works fine.
Say the enrollment in the course changes. The User expects to send the same JSON structure to the webservice handling the PUT call. The code took the easy path for updating, first finding and deleting all the existing CollegeCourse-Student links, then saving the new links. (Rather than iterating through the two lists, matching up items.) This part worked also as given.
We then added a uniqueness constraint to the CollegeCourse-Student association table, so that said table could not have a single Student linked to one CollegeCourse multiple times. This crashed and burned. A debugging session revealed the culprit: the delete of the CollegeCourse-Student records did not actually remove them from the database until the transaction completed. Thus, when we tried to add the new links back in, any holdovers from the original POST conflicted with what was already in the database.
The service handling the PUT is preceded by a #Transactional annotation. I tried moving the code to find and delete the associations in a separate method, and tried both #Transactional(propagation=Propagation.REQUIRED) and REQUIRES_NEW, but neither prevented failing the uniqueness constraint. I also added #EnableTransactionManagement to my Application class - same story. Is there a simple solution to my dilemma?
Without knowing exactly what your repository looks like, have you tried to do a manual flush on the entity manager after the deletions?
Something along the lines of
entityManager.flush();
Or, if you're using a Spring Data JPA repository, you should be able to define a flush method in that interface and call it.

Resources