hibernate/hibernate-orm · error · AssertionFailure
force initializing collection loading
Error message
force initializing collection loading
What it means
forceInitialization fully loads a collection (used by cascading actions and lazy-state handling). If the collection is already mid-initialization, re-entering initialize would recurse, so Hibernate throws AssertionFailure. This is a reentrancy problem: loading collection A triggers, through listeners, bidirectional cascades, or eager cycles, a force-initialization of A itself.
Source
Thrown at hibernate-core/src/main/java/org/hibernate/collection/spi/AbstractPersistentCollection.java:847
// the param would have to be bound twice. Until we eventually add "parameter bind points" concepts to the
// AST in ORM 5+, handling this type of condition is either extremely difficult or impossible. Forcing
// recreation isn't ideal, but not really any other option in ORM 4.
// Selecting a type used in where part of update statement
// (must match condition in org.hibernate.persister.collection.BasicCollectionPersister#doUpdateRows).
// See HHH-9474
final Type whereType =
persister.hasIndex()
? persister.getIndexType()
: persister.getElementType();
return whereType instanceof CompositeType compositeType
&& compositeType.hasNullProperty();
}
@Override
public final void forceInitialization() throws HibernateException {
if ( !initialized ) {
if ( initializing ) {
throw new AssertionFailure( "force initializing collection loading" );
}
initialize( false );
}
}
/**
* Get the current snapshot from the session
*/
protected final Serializable getSnapshot() {
return session.getPersistenceContext().getSnapshot( this );
}
@Override
public final boolean wasInitialized() {
return initialized;
}
View on GitHub (pinned to fad1729dce)
Solutions
- Break the cycle: make one side of the bidirectional association lazy and drop mapping-level eager fetch
- Remove collection initialization from load-time event listeners and interceptors
- Prefer query-level join fetch over mapping-level eager fetching for the paths that need data
- Reproduce with a minimal mapping and check/report on the Hibernate JIRA - AssertionFailure marks an internal invariant break
Example fix
// before: eager on both sides of a bidirectional relation @OneToMany(mappedBy = "parent", fetch = FetchType.EAGER) List<Child> children; @ManyToOne(fetch = FetchType.EAGER) Parent parent; // after @OneToMany(mappedBy = "parent", fetch = FetchType.LAZY) List<Child> children; // fetch explicitly with 'join fetch' where needed
Defensive patterns
Strategy: validation
Prevention
- Avoid eager fetch on both sides of a bidirectional relation
- Never initialize collections inside load-time event listeners or interceptors
- Keep cascade graphs shallow; prefer explicit queries over deep eager graphs
- Treat AssertionFailure with 'force initializing collection loading' as a design smell to fix, not to catch
When it happens
Trigger: Event listeners or interceptors that initialize collections during onLoad/onLoadCollection; bidirectional relations eager-fetched on both sides forming a cycle; bytecode enhancement interacting with custom loaders; rare internal bugs in specific versions.
Common situations: Custom load-time listeners touching associations; circular fetch-join graphs; behavior changes after a Hibernate upgrade.
Related errors
- Property '<propertyName>' may not be annotated '@BatchSize'
- Tried to assemble a different subclass instance
- Cannot lazily initialize collection
- Cannot lazily initialize collection (collection is being rem
- Session/EntityManager is closed
AI-assisted analysis of hibernate/hibernate-orm@fad1729dce (2026-08-22).
Data as JSON: /api/errors/beb73d8a776ade78.
Report an issue: GitHub.