NHibernate second-level cache region defaults to entity name when region name omitted
25.5K reputation · 18 Jan 2022, 19:18 UTC
When configuring NHibernate's second‑level cache, the goal is to guarantee that each entity (or collection) uses a distinct cache region so that eviction and expiration policies apply only to the intended data.
If the region attribute is omitted in the <cache> mapping or the programmatic equivalent, NHibernate treats the region as the default region named after the entity's fully qualified name. This fallback can cause unrelated entities to share the same region when their names coincide or when collections inherit the owning entity's region, leading to unexpected cache hits/misses and reduced caching efficiency.
The uncertainty lies in detecting whether any mapping has left the region name unspecified and in enforcing an explicit region name across all cached entities without manual inspection.
- How can we programmatically verify that every mapped entity or collection defines a non‑empty region name?
- What configuration or mapping options exist to require an explicit region name and prevent the default fallback?
- Is there a way to configure NHibernate to throw an exception or warning when a cache region name is omitted?