{"record":{"id":"a9ba0c4f3b9085d9","repo":"hibernate/hibernate-orm","slug":"object-references-an-unsaved-transient-instance-of","errorCode":null,"errorMessage":"object references an unsaved transient instance of '{entityName}' (persist the transient instance before flushing)","messagePattern":"object references an unsaved transient instance of '(.+?)' \\(persist the transient instance before flushing\\)","errorType":"exception","errorClass":"TransientObjectException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/metamodel/mapping/EntityIdentifierMapping.java","lineNumber":116,"sourceCode":"\t * @since 6.1.1\n\t */\n\tdefault Object getIdentifierIfNotUnsaved(Object entity, SharedSessionContractImplementor session) {\n\t\tif ( entity == null ) {\n\t\t\treturn null;\n\t\t}\n\t\telse if ( session == null ) {\n\t\t\t// If we have no session available, just return the identifier\n\t\t\treturn getIdentifier( entity );\n\t\t}\n\t\tfinal Object id = session.getContextEntityIdentifier( entity );\n\t\tif ( id == null ) {\n\t\t\t// getContextEntityIdentifier() returned null, indicating that\n\t\t\t// the entity is not associated with the persistence context,\n\t\t\t// so look deeper for its id\n\t\t\tfinal String entityName = findContainingEntityMapping().getEntityName();\n\t\t\tif ( ForeignKeys.isTransient( entityName, entity, Boolean.FALSE, session ) ) {\n\t\t\t\t// TODO should be a TransientPropertyValueException\n\t\t\t\tthrow new TransientObjectException( \"object references an unsaved transient instance of '\"\n\t\t\t\t\t\t+ (entityName == null ? session.guessEntityName( entity ) : entityName)\n\t\t\t\t\t\t+ \"' (persist the transient instance before flushing)\" );\n\t\t\t}\n\t\t\treturn getIdentifier( entity );\n\t\t}\n\t\telse {\n\t\t\treturn id;\n\t\t}\n\t}\n\n\t/**\n\t * Inject an identifier value into an instance of the entity\n\t */\n\tvoid setIdentifier(Object entity, Object id, SharedSessionContractImplementor session);\n\n\t/**\n\t * The style of identifier used.\n\t */","sourceCodeStart":98,"sourceCodeEnd":134,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/EntityIdentifierMapping.java#L98-L134","documentation":"Error \"object references an unsaved transient instance of '{entityName}' (persist the transient instance before flushing)\" thrown in hibernate/hibernate-orm.","triggerScenarios":"Thrown at flush time when an association references a transient entity that was never persisted; save the referenced instance first or add cascade persist.","commonSituations":"See trigger scenarios.","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."],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}