hibernate/hibernate-orm · error · HibernateException
No collection for replacement found
Error message
No collection for replacement found: {role} What it means
Guard inside replaceCollection: the collectionEntries map has no entry for the old collection instance, so there is nothing to replace for the given role. Happens when the session no longer tracks the original collection (e.g. it was evicted or cleared) at replacement time.
Solutions
- Ensure the collection with the given role is loaded in the persistence context before attempting replacement
- Verify the role name matches a mapped collection
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at hibernate-core/src/main/java/org/hibernate/engine/internal/StatefulPersistenceContext.java:1060 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of hibernate/hibernate-orm@fad1729dce (2026-08-22).
Data as JSON: /api/errors/8e256cef1b8075d1.
Report an issue: GitHub.
Appendix: source
Thrown at hibernate-core/src/main/java/org/hibernate/engine/internal/StatefulPersistenceContext.java:1060
throw new HibernateException( "Replacement of not directly accessible collection found: "
+ oldCollection.getRole() );
}
assert !collection.isDirectlyAccessible();
final var oldEntry = collectionEntries.remove( oldCollection.$$_hibernate_getInstanceId(), oldCollection );
final var entry =
oldEntry.getLoadedPersister() != null
// This is an already existing/loaded collection so ensure the loadedPersister is initialized
? new CollectionEntry( collection, session.getFactory() )
// A newly wrapped collection
: new CollectionEntry( persister, collection );
entry.setReadOnly( oldEntry.isReadOnly(), collection );
putCollectionEntry( collection, entry );
final Object key = collection.getKey();
if ( key != null ) {
final var collectionKey = session.generateCollectionKey( entry.getLoadedPersister(), key );
final var old = addCollectionByKey( collectionKey, collection );
if ( old == null ) {
throw new HibernateException( "No collection for replacement found: " + collectionKey.getRole() );
}
}
}
/**
* Add a collection to the cache, with a given collection entry.
*
* @param coll The collection for which we are adding an entry.
* @param entry The entry representing the collection.
* @param key The key of the collection's entry.
*/
private void addCollection(PersistentCollection<?> coll, CollectionEntry entry, Object key) {
putCollectionEntry( coll, entry );
final var collectionKey = session.generateCollectionKey( entry.getLoadedPersister(), key );
final var old = addCollectionByKey( collectionKey, coll );
if ( old != null ) {
if ( old == coll ) {
throw new AssertionFailure( "bug adding collection twice" );View on GitHub (pinned to fad1729dce)