Sample Application to demonstrate Multi-tenancy based on discriminator field using Spring Boot & Hibernate.
Since hibernate (5.x) has some issues (refer the link below) in supporting the 'Discriminator' based multi-tenancy strategy. This following workaround will help to achieve the same.
Approach:
Explanation:
X-TenantID
http header and set's it in the ThreadLocal variable using TenantContext
class. If http header is not present in request, it'll be rejected.UserServiceAspect
class) intercepts the service call and set's the hibernate tenant filter.@Transactional
for UserServiceAspect
to work.EmptyInterceptor
) class which sets the tenantId value during the save/delete/flush-dirty entity events.TenantSupport
interface for the Entity interceptor to work. Refer: