2

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

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

@Norbest Bisci thanx for your answer. I didn't know MultiTenancy approach :)
@dosmac I know the feeling, when I had that problem I searched for a looong time. glad it helped! Happy coding :D
0

I realised that when I use both MySQL and PostgreSQL. The first parameter is different.

For MySQL:

schema = "data"

@Table(schema = "data", name = "datasourcesample")

For PostgreSQL:

catalog = "data1"

@Table(catalog = "data1", name = "datasourcesample")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.