Introduction
Changelog
Contact
Guides
Getting Started
Entities
Repositories
Queries & Specifications
Configuration & Pooling
Migrations
Spring Boot
Spigot & Paper Plugins
Recipes
Reference
Databases
Troubleshooting
Advanced
Multi-tenancy
Audit Trail
Testing
TrademarkTrademark
Ctrl k
Search…
Get started
Introduction
Changelog
Contact
Guides
Getting Started
Entities
Repositories
Queries & Specifications
Configuration & Pooling
Migrations
Spring Boot
Spigot & Paper Plugins
Recipes
Reference
Databases
Troubleshooting
Advanced
Multi-tenancy
Audit Trail
Testing
TrademarkTrademark© Aveiro. All rights reserved.
Built with Aveiro
Advanced

Multi-tenancy

Isolate tenant data with the DISCRIMINATOR, SCHEMA, or DATABASE strategy, bind the current tenant with TenantContext, and rely on the strict leak-guard.
Updated last month
Audit Trail
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
DISCRIMINATORa tenant column on each rowone shared pool
SCHEMAone schema per tenantone shared pool
DATABASEone database per tenanta pool per tenant
DISCRIMINATOR is the lightest (good for many small tenants); DATABASE is the strongest isolation.

Enable it

config.multiTenancy(MultiTenancyConfig.discriminator());
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.