hibernate/hibernate-orm · error · IllegalArgumentException

Non-entity object instance passed to evict: ${object}

Error message

Non-entity object instance passed to evict: ${object}

What it means

Error "Non-entity object instance passed to evict: ${object}" thrown in hibernate/hibernate-orm.

Source

Thrown at hibernate-core/src/main/java/org/hibernate/event/internal/DefaultEvictEventListener.java:98

	 * Make sure the passed object is even an entity, and if not throw an exception.
	 * This is different to the legacy Hibernate behavior, but is what JPA 2.1
	 * requires with EntityManager.detach().
	 */
	private static void checkEntity(@Nonnull Object object, @Nonnull EventSource source) {
		final String entityName = source.getSession().guessEntityName( object );
		if ( entityName != null ) {
			try {
				final var persister =
						source.getFactory().getMappingMetamodel()
								.getEntityDescriptor( entityName );
				if ( persister != null ) {
					return; //ALL GOOD
				}
			}
			catch (Exception ignore) {
			}
		}
		throw new IllegalArgumentException( "Non-entity object instance passed to evict: " + object);
	}

	protected void doEvict(
			@Nonnull final Object object,
			@Nonnull final EntityKey key,
			@Nonnull final EntityPersister persister,
			@Nonnull final EventSource session)
			throws HibernateException {
		if ( EVENT_LISTENER_LOGGER.isTraceEnabled() ) {
			EVENT_LISTENER_LOGGER.evicting( infoString( persister ) );
		}

		final var persistenceContext = session.getPersistenceContextInternal();
		if ( persister.hasNaturalIdentifier() ) {
			persistenceContext.getNaturalIdResolutions().handleEviction( key.getIdentifier(), object, persister );
		}

		// remove all collections for the entity from the session-level cache

View on GitHub (pinned to fad1729dce)

Solutions

  1. Pass only mapped entity instances to Session.evict(); verify the object's class is annotated with @Entity or mapped in configuration.
  2. If the object is a DTO or value type, remove the evict() call since only managed entities belong to the persistence context.

When it happens

Trigger: Thrown when evict() receives an object that is neither a managed entity nor an entity proxy.

Common situations: Typical situations: passing a DTO, embeddable, or plain object to Session.evict(); a variable holding the wrong object.


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