hibernate/hibernate-orm · error · TransientObjectException
object references an unsaved transient instance of '{entityN
Error message
object references an unsaved transient instance of '{entityName}' (persist the transient instance before flushing) What it means
Error "object references an unsaved transient instance of '{entityName}' (persist the transient instance before flushing)" thrown in hibernate/hibernate-orm.
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)
Solutions
- Call persist() (or save) on the referenced transient entity before flushing the referencing entity.
- Add cascade=PERSIST/ALL to the association if the referenced entity should be persisted automatically.
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.