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(); } }
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
