{"record":{"id":"46678fbf56d06015","repo":"hibernate/hibernate-orm","slug":"causemessage-for-entity-entityname-with-id","errorCode":null,"errorMessage":"<causeMessage> for entity [<entityName> with id '<id>']","messagePattern":"<causeMessage> for entity \\[<entityName> with id '<id>'\\]","errorType":"exception","errorClass":"StaleObjectStateException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/engine/jdbc/mutation/internal/ModelMutationHelper.java","lineNumber":72,"sourceCode":"\t\t\tObject id,\n\t\t\tSessionFactoryImplementor sessionFactory) {\n\t\ttry {\n\t\t\tstatementDetails.getExpectation().verifyOutcome(\n\t\t\t\t\taffectedRowCount,\n\t\t\t\t\tstatementDetails.getStatement(),\n\t\t\t\t\tbatchPosition,\n\t\t\t\t\tstatementDetails.getSqlString()\n\t\t\t);\n\t\t\treturn true;\n\t\t}\n\t\tcatch (StaleStateException e) {\n\t\t\tif ( !statementDetails.getMutatingTableDetails().isOptional() && affectedRowCount == 0 ) {\n\t\t\t\tfinal String fullPath = mutationTarget.getNavigableRole().getFullPath();\n\t\t\t\tfinal var statistics = sessionFactory.getStatistics();\n\t\t\t\tif ( statistics.isStatisticsEnabled() ) {\n\t\t\t\t\tstatistics.optimisticFailure( fullPath );\n\t\t\t\t}\n\t\t\t\tthrow new StaleObjectStateException( fullPath, id, e );\n\t\t\t}\n\t\t\treturn false;\n\t\t}\n\t\tcatch (TooManyRowsAffectedException e) {\n\t\t\tthrow new HibernateException(\n\t\t\t\t\tString.format(\n\t\t\t\t\t\t\tLocale.ROOT,\n\t\t\t\t\t\t\t\"Duplicate identifier in table (%s) - %s#%s\",\n\t\t\t\t\t\t\tstatementDetails.getMutatingTableDetails().getTableName(),\n\t\t\t\t\t\t\tmutationTarget.getNavigableRole().getFullPath(),\n\t\t\t\t\t\t\tid\n\t\t\t\t\t)\n\t\t\t);\n\t\t}\n\t\tcatch (Throwable t) {\n\t\t\treturn false;\n\t\t}\n\t}","sourceCodeStart":54,"sourceCodeEnd":90,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/engine/jdbc/mutation/internal/ModelMutationHelper.java#L54-L90","documentation":"ModelMutationHelper executes insert/update/delete mutations and catches StaleStateException; when the mutating table is not optional and the JDBC driver reported 0 affected rows, it throws StaleObjectStateException(fullPath, id, cause). That exception's getMessage() composes '<causeMessage> for entity [EntityName with id '<id>']' — this is Hibernate's optimistic-locking conflict: the row was updated or deleted by another transaction since it was loaded.","triggerScenarios":"flush()/commit of an UPDATE or DELETE whose WHERE (id [+ version]) matched no row: a concurrent transaction committed changes first, the row was deleted, or the version/unsaved-value mapping is wrong so Hibernate compares stale criteria.","commonSituations":"Two users editing the same record; background jobs deleting entities out from under a loaded screen; long conversations with detached entities merged after the data changed; missing or misconfigured @Version columns.","solutions":["Catch OptimisticLockException/StaleObjectStateException at the use-case boundary and handle the conflict: reload the entity and retry, or report 409/conflict to the user","Verify @Version optimistic-locking is mapped to the actual DB column and that all writers update it","With assigned identifiers, check unsaved-value configuration so existing rows are updated, not treated inconsistently","Shorten conversations: load-modify-save within one transaction instead of holding entities across requests"],"exampleFix":"// before: unguarded merge\nem.merge(doc);\n\n// after: explicit conflict handling\ntry {\n    em.merge(doc);\n    em.flush();\n}\ncatch (OptimisticLockException e) {\n    Doc fresh = em.find(Doc.class, doc.getId()); // reload and reapply or report conflict\n    throw new ConcurrentModificationException(\"doc \" + doc.getId() + \" changed\", e);\n}","handlingStrategy":"retry","validationCode":"// optimistic guard: check the version before flushing changes\nDoc fresh = em.find(Doc.class, doc.getId());\nif (fresh == null || fresh.getVersion() > doc.getVersion()) {\n    throw new ConcurrentModificationException(\"doc changed, reload before edit\");\n}","typeGuard":null,"tryCatchPattern":"catch (OptimisticLockException e) {\n    // reload latest state, reapply user changes, retry once with a backoff\n}","preventionTips":["Map @Version columns and let all writers bump them","Keep load-modify-save conversations short","Handle OptimisticLockException explicitly at the service/UI boundary (retry or 409)"],"tags":["optimistic-locking","concurrency","stale-state","flush","hibernate"],"backgroundTag":"optimistic-lock-conflict","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}