hibernate/hibernate-orm · error · PersistentObjectException

attempted to load into an instance that was already associat

Error message

attempted to load into an instance that was already associated with the session: ${infoString(persister, event.getEntityId(), event.getFactory())}

What it means

Error "attempted to load into an instance that was already associated with the session: ${infoString(persister, event.getEntityId(), event.getFactory())}" thrown in hibernate/hibernate-orm.

Source

Thrown at hibernate-core/src/main/java/org/hibernate/event/internal/DefaultLoadEventListener.java:193

	 *
	 * @param event The initiating load request event
	 * @param persister The persister corresponding to the entity to be loaded
	 * @param keyToLoad The key of the entity to be loaded
	 * @param options The defined load options
	 *
	 * @return The loaded entity.
	 */
	@Nullable
	private Object load(
			@Nonnull LoadEvent event,
			@Nonnull EntityPersister persister,
			@Nonnull EntityKey keyToLoad,
			@Nonnull LoadType options) {
		final Object instanceToLoad = event.getInstanceToLoad();
		if ( instanceToLoad != null ) {
			final var session = event.getSession();
			if ( session.getPersistenceContextInternal().getEntry( instanceToLoad ) != null ) {
				throw new PersistentObjectException(
						"attempted to load into an instance that was already associated with the session: "
								+ infoString( persister, event.getEntityId(), event.getFactory() )
				);
			}
			persister.setIdentifier( instanceToLoad, event.getEntityId(), session );
		}

		final Object entity = doLoad( event, persister, keyToLoad, options );
		final boolean isOptionalInstance = instanceToLoad != null;
		if ( entity == null
				&& ( !options.isAllowNulls() || isOptionalInstance ) ) {
			event.getFactory().getEntityNotFoundDelegate()
					.handleEntityNotFound( event.getEntityClassName(), event.getEntityId() );
		}
		else if ( isOptionalInstance && entity != instanceToLoad ) {
			throw new NonUniqueObjectException( event.getEntityId(), event.getEntityClassName() );
		}
		return entity;

View on GitHub (pinned to fad1729dce)

Solutions

  1. Use session.merge() instead of load() when you already hold an instance that may be associated with the session.
  2. Evict the existing instance before loading, or reuse the already-associated instance from the persistence context.

When it happens

Trigger: Thrown when two distinct object instances with the same persistent identity would coexist in one persistence context.

Common situations: Typical situations: loading an entity while a detached copy with the same id is re-attached; merging a copy while the managed instance is already present; duplicate instances produced by manual construction.


AI-assisted analysis of hibernate/hibernate-orm@fad1729dce (2026-08-22). Data as JSON: /api/errors/54e3e3b5e28a7c86. Report an issue: GitHub.