hibernate/hibernate-orm · error · TransientObjectException

object references an unsaved transient instance of

Error message

object references an unsaved transient instance of '{entityName}' (persist the transient instance before flushing)

What it means

Classic Hibernate transience error surfaced while obtaining an entity's id: the object references an unsaved transient instance whose id is null/sentinel — persist (or cascade to) the transient instance before flushing so the association can be resolved.

Solutions

  1. Call persist() (or save) on the referenced transient entity before flushing the referencing entity.
  2. Add cascade=PERSIST/ALL to the association if the referenced entity should be persisted automatically.
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at flush time when an association references a transient entity that was never persisted; save the referenced instance first or add cascade persist.

Common situations: See trigger scenarios.


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

Appendix: source

Thrown at hibernate-core/src/main/java/org/hibernate/metamodel/mapping/EntityIdentifierMapping.java:116

	 * @since 6.1.1
	 */
	default Object getIdentifierIfNotUnsaved(Object entity, SharedSessionContractImplementor session) {
		if ( entity == null ) {
			return null;
		}
		else if ( session == null ) {
			// If we have no session available, just return the identifier
			return getIdentifier( entity );
		}
		final Object id = session.getContextEntityIdentifier( entity );
		if ( id == null ) {
			// getContextEntityIdentifier() returned null, indicating that
			// the entity is not associated with the persistence context,
			// so look deeper for its id
			final String entityName = findContainingEntityMapping().getEntityName();
			if ( ForeignKeys.isTransient( entityName, entity, Boolean.FALSE, session ) ) {
				// TODO should be a TransientPropertyValueException
				throw new TransientObjectException( "object references an unsaved transient instance of '"
						+ (entityName == null ? session.guessEntityName( entity ) : entityName)
						+ "' (persist the transient instance before flushing)" );
			}
			return getIdentifier( entity );
		}
		else {
			return id;
		}
	}

	/**
	 * Inject an identifier value into an instance of the entity
	 */
	void setIdentifier(Object entity, Object id, SharedSessionContractImplementor session);

	/**
	 * The style of identifier used.
	 */

View on GitHub (pinned to fad1729dce)