Hibernate / Hibernate 7 Basics Interview Questions
What are Hibernate 7's improvements to multi-tenancy support?
Hibernate 7 enhances multi-tenancy with better integration with auto-enabled filters for discriminator-based tenancy.
| Strategy | Isolation | Use case |
|---|---|---|
| DATABASE | Highest - one DB per tenant | Regulated industries; highest cost |
| SCHEMA | High - one schema per tenant | Good balance; PostgreSQL/Oracle |
| DISCRIMINATOR | Lowest - tenant_id column | SaaS with many small tenants; lowest cost |
// DISCRIMINATOR (most common in Hibernate 7) @FilterDef( name="tenantFilter", parameters=@ParamDef(name="tenantId", type=String.class), autoEnabled=true // Hibernate 7: auto-enabled! ) @Filter(name="tenantFilter", condition="tenant_id = :tenantId") @Entity public class Order { @Id private Long id; private String tenantId; // discriminator column private BigDecimal total; } // FilterDefContributor SPI: auto-resolves tenantId from ThreadLocal public class TenantFilterContributor implements FilterDefContributor { @Override public void contribute(FilterDefTarget target) { target.registerFilterDefinition( FilterDefinition.of("tenantFilter", Map.of("tenantId", TenantContext::getCurrentTenantId)) ); } } // DATABASE strategy: supply different connections per tenant public class TenantConnectionProvider implements MultiTenantConnectionProvider<String> { @Override public Connection getConnection(String tenantId) { return dataSourceMap.get(tenantId).getConnection(); } }
More Related questions...