hibernate/hibernate-orm · error · HibernateException

Found shared references to a collection: {role}

Error message

Found shared references to a collection: {role}

What it means

Error "Found shared references to a collection: {role}" thrown in hibernate/hibernate-orm.

Source

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

		}
	}

	/**
	 * 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" );
			}
			throw new HibernateException(
					"Found shared references to a collection: " + collectionKey.getRole()
			);
		}
	}

	private void putCollectionEntry(PersistentCollection<?> collection, CollectionEntry entry) {
		if ( collectionEntries == null ) {
			collectionEntries = new InstanceIdentityMap<>();
		}
		assert collection.$$_hibernate_getInstanceId() == 0;
		collection.$$_hibernate_setInstanceId( nextCollectionInstanceId() );
		collectionEntries.put( collection, entry );
	}

	/**
	 * Add a collection to the cache, creating a new collection entry for it
	 *
	 * @param collection The collection for which we are adding an entry.

View on GitHub (pinned to fad1729dce)

Solutions

  1. Do not share the same collection instance between two owners; create a separate collection per owner
  2. Clone the collection contents into a new collection for the second owner

When it happens

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