I have one db (MySql) and several schemas (schema_a,schema_b,schema_c). The schemas have same tables, identical.
The current schema depends on current user (session) data. For example, current user is from company A, then current schema is "schema_a". Accordingly, EmployeeRepository.findAll() method fetches data from schema "schema_a".
In hibernate with spring boot 2, is it possible dynamically change current entityManager? If yes, how?
Help, please
What you are talking about is referred to as a multi tenant architecture.
You could set the catalog
(that is the schema in your case) based on some authentication information.
How you would do that is implement an interceptor
that determines what schema needs to be set and then sets the correct "tenant" to the TheardLocal
(on which your request is running). I have used this approach with great success and I do believe it is exactly what you are looking for.
I could write it out here, however there is no need to keep repeating what already exists. There is a very good article, which was the inspiration for my solution, and I suggest you take a look here.