hibernate/hibernate-orm · error · HibernateException

Replacement of not directly accessible collection found: {ro

Error message

Replacement of not directly accessible collection found: {role}

What it means

Error "Replacement of not directly accessible collection found: {role}" thrown in hibernate/hibernate-orm.

Source

Thrown at hibernate-core/src/main/java/org/hibernate/engine/internal/StatefulPersistenceContext.java:1042

		final Object key = collection.getKey();
		assert key != null;
		final var collectionEntry = new CollectionEntry( persister, key );
		addCollection( collection, collectionEntry, key );
		if ( session.getLoadQueryInfluencers().effectivelyBatchLoadable( persister ) ) {
			getBatchFetchQueue().addBatchLoadableCollection( collection, collectionEntry );
		}
	}

	@Override
	public void addNewCollection(CollectionPersister persister, PersistentCollection<?> collection)
			throws HibernateException {
		addCollection( collection, persister );
	}

	@Override
	public void replaceCollection(CollectionPersister persister, PersistentCollection<?> oldCollection, PersistentCollection<?> collection) {
		if ( !oldCollection.isDirectlyAccessible() ) {
			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() );

View on GitHub (pinned to fad1729dce)

Solutions

  1. Do not replace a collection that is not directly accessible through its owner; access and modify it via the owning entity
  2. Fetch the collection through the owner before replacing it

When it happens

Trigger: Thrown at hibernate-core/src/main/java/org/hibernate/engine/internal/StatefulPersistenceContext.java:1042 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/497111ab18111523. Report an issue: GitHub.