{"record":{"id":"acd54d4e4ba50968","repo":"hibernate/hibernate-orm","slug":"entity-references-an-unsaved-transient-instance-of","errorCode":null,"errorMessage":"Entity references an unsaved transient instance of '{}' (make the transient instance persistent before flushing)","messagePattern":"Entity references an unsaved transient instance of '(.+?)' \\(make the transient instance persistent before flushing\\)","errorType":"exception","errorClass":"TransientObjectException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/engine/internal/ForeignKeys.java","lineNumber":420,"sourceCode":"\n\tpublic static Object getEntityIdentifier(\n\t\t\tfinal String entityName,\n\t\t\tfinal Object object,\n\t\t\tfinal SharedSessionContractImplementor session) {\n\t\tif ( object == null ) {\n\t\t\treturn null;\n\t\t}\n\t\telse {\n\t\t\tfinal Object id = session.getContextEntityIdentifier( object );\n\t\t\treturn id == null\n\t\t\t\t\t? session.getEntityPersister( entityName, object ).getIdentifier( object, session )\n\t\t\t\t\t: id;\n\t\t}\n\t}\n\n\tprivate static void throwIfTransient(String entityName, Object object, SharedSessionContractImplementor session) {\n\t\tif ( isTransient( entityName, object, Boolean.FALSE, session ) ) {\n\t\t\tthrow new TransientObjectException( \"Entity references an unsaved transient instance of '\"\n\t\t\t\t\t+ (entityName == null ? session.guessEntityName(object) : entityName)\n\t\t\t\t\t+ \"' (make the transient instance persistent before flushing)\" );\n\t\t}\n\t}\n\n\t/**\n\t * Find all non-nullable references to entities that have not yet\n\t * been inserted in the database, where the foreign key\n\t * is a reference to an unsaved transient entity. .\n\t *\n\t * @param entityName - the entity name\n\t * @param entity - the entity instance\n\t * @param values - insertable properties of the object (including backrefs),\n\t * possibly with substitutions\n\t * @param isEarlyInsert - true if the entity needs to be executed as soon as possible\n\t * (e.g., to generate an ID)\n\t * @param session - the session\n\t *","sourceCodeStart":402,"sourceCodeEnd":438,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/engine/internal/ForeignKeys.java#L402-L438","documentation":"Error \"Entity references an unsaved transient instance of '{}' (make the transient instance persistent before flushing)\" thrown in hibernate/hibernate-orm.","triggerScenarios":"Thrown at hibernate-core/src/main/java/org/hibernate/engine/internal/ForeignKeys.java:420 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Persist the transient instance with session.persist() before flushing the referencing entity","Add the appropriate cascade (e.g. PERSIST/ALL) on the association so the transient child is saved 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"}