ramsrib / multi-tenant-app-demo

Demonstrates the discriminator field (shared schema) based multi-tenant application using Spring Boot & Hibernate 5.
69 stars 26 forks source link

findById #2

Open MartynenkoA opened 6 years ago

MartynenkoA commented 6 years ago

findById is not working, when i tring to getbyid (in header tenant1) but userId is id of user tanant2 in responce i get information about user2

MartynenkoA commented 6 years ago

in console i have this request select user0_.user_id as user_id1_00, user0_.first_name as first_na2_00, user0_.last_name as last_nam3_00, user0_.tenant_id as tenant_i4_00, user0_.username as username5_00 from userinfo user0 where user0_.user_id=?

where is "user0_.tenant_id = ?" ??

Codem3ay commented 5 years ago

Same problem here. Did you manage to find a solution for this?!

ls-amir-beygi commented 5 years ago

It looks like hibernate filter does not work on findById ! https://coderanch.com/t/543756/databases/Hibernate-Filter-working

MikhailEpatko commented 5 years ago

According to https://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/Hibernate_User_Guide.html (see "Example 89. Query entities mapped with @Filter"): "Filters apply to entity queries, but not to direct fetching. Therefore, in the following example, the filter is not taken into consideration when fetching an entity from the Persistence Context."

To resolve the issue you may use custom query in your JpaRepository like this:

@Query("SELECT u FROM User u WHERE u.id = ?1") Optional findUserById(@NonNull String id);

In this case Hibernate adds condition from @Filter to the query.

M-Devloo commented 4 years ago

As @MikhailEpatko mentioned, this is indeed not applied due to direct fetching. This can also be fixed by overriding the SimpleJpaRepository base class.

A production ready example can be found here: https://github.com/M-Devloo/Spring-boot-auth0-discriminator-multitenancy/pull/3/files

Note: this is a part of my SaaS Multi Tenancy implementation which is fully tested as well has been proven to be reliable in production.