Shared-schema multi-tenancy in Spring Boot + Hibernate: the reference code we actually run
Multi-tenancy is one of those architectural decisions that looks deceptively simple until you start implementing it.
You add a tenant_id column, make sure queries are scoped to the current tenant, and assume you're done.
But what happens when a request has no tenant context? What happens when Spring Security hasn't populated the authentication context yet? What about native queries, background jobs, or writes that attempt to associate an entity with another tenant?
We ran into these questions while building our SaaS application, PresenceWave.
Rather than publish another theoretical explanation of multi-tenancy, I wanted to document the implementation we actually use: shared-schema, discriminator-based multi-tenancy with Spring Boot and Hibernate 6.
We've also extracted the relevant implementation into an open-source repository.β¦