IllegalArgumentException during SessionFactory initialization after failed upgrade
0 reputation · 20 Dec 2020, 20:01 UTC
0 reputation · 20 Dec 2020, 20:01 UTC
A Hibernate upgrade that changes the dialect or mapping metadata often ends with an IllegalArgumentException during SessionFactory initialization. Existing sessions remain open until explicitly closed, creating potential resource leaks.
1. The database schema may be out of sync with the new mapping, leading to second‑level cache corruption and ClassCastException on subsequent reads.
2. Automatic schema migration (e.g., hbm2ddl.auto=update) is risky in production and may require manual intervention.
SessionFactory be safely rebuilt while sessions linger?SessionFactory.getCache().evictAll() sufficient to eliminate stale cache entries, or is a more granular eviction strategy needed?34775 reputation · 20 Dec 2020, 23:08 UTC
Close all active sessions before rebuilding the SessionFactory. Rebuilding while sessions linger is unsafe. Those sessions were created against the previous metamodel and hold JDBC connections and first-level state that will never reconcile with the new mappings. Stop accepting work, close every open Session, then rebuild.
SessionFactory.getCache().evictAll() is not sufficient on its own. It clears only the regions Hibernate knows about. With an external provider such as Ehcache or Infinispan, persistent or clustered cache can survive a JVM restart. A provider-level clear or cache-region version bump is required. Treat evictAll as necessary but not sufficient.
Recommended sequence for this case:
Likely explanation: An IllegalArgumentException during bootstrap most commonly means the new Hibernate version rejects a setting or mapping: a removed or renamed dialect class, a property name that no longer exists, or an annotation or mapping construct whose semantics changed across versions. Schema drift and second-level cache corruption can cause ClassCastException on subsequent reads after an upgrade.
Confirmed facts: The exception message and its caused-by chain name the offending setting or entity. A failed SessionFactory build does not invalidate an already running factory. hbm2ddl.auto=update does not perform reliable migrations and is unsafe in production. Cache eviction is mandatory when entity mappings or the Hibernate version change the serialized form.
Provide the full IllegalArgumentException stack trace including the caused-by chain. The exact offending class or property determines whether a dialect change, property rename, or mapping adjustment is required and changes the fix in step 6.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.