Isolate tenant data with the DISCRIMINATOR, SCHEMA, or DATABASE strategy, bind the current tenant with TenantContext, and rely on the strict leak-guard.
Multi-tenancy is off by default. When enabled, JEHibernate isolates each tenant's data using one of three strategies and a thread-bound current-tenant context.
Strategies
Strategy
Isolation
Pooling
DISCRIMINATOR
a tenant column on each row
one shared pool
SCHEMA
one schema per tenant
one shared pool
DATABASE
one database per tenant
a pool per tenant
DISCRIMINATOR is the lightest (good for many small tenants); DATABASE is the strongest isolation.
For DISCRIMINATOR, entities carry a @TenantId column; the resolver fills it from the bound tenant on write and filters by it on read.
Binding the current tenant
TenantContext is a thread-local scope. Open it around the work that should see one tenant's data; the nested AutoCloseable resets it when the block exits.
try (var ignored = TenantContext.open("acme")) {
noteRepo.findAll(); // sees only acme's rows
}
Supply your own TenantResolver to derive the tenant from a request, a player, or a session instead of binding it explicitly.
The leak-guard
The resolver is strict: if a tenant-scoped operation runs with no tenant bound, it throws rather than silently returning another tenant's data. This turns a dangerous data-leak bug into a loud failure you catch in testing.
Always bind before you query
Under multi-tenancy, a query outside a TenantContext.open(...) scope throws by design. Bind the tenant at the edge of each request or task.
See the repository's docs/multi-tenancy-guide.md for the SCHEMA and DATABASE provider wiring and per-tenant DataSource setup.