{"record":{"id":"7d4998d0a4353b7c","repo":"hibernate/hibernate-orm","slug":"duplicate-identifier-in-table-s-s-s-7d4998","errorCode":null,"errorMessage":"Duplicate identifier in table (%s) - %s#%s","messagePattern":"Duplicate identifier in table \\((.+?)\\) - (.+?)#(.+?)","errorType":"exception","errorClass":"HibernateException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/engine/jdbc/mutation/internal/ModelMutationHelper.java","lineNumber":77,"sourceCode":"\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}\n\n\tpublic static PreparedStatementGroup toPreparedStatementGroup(\n\t\t\tMutationType mutationType,\n\t\t\tMutationTarget<?,?> mutationTarget,\n\t\t\tGeneratedValuesMutationDelegate delegate,","sourceCodeStart":59,"sourceCodeEnd":95,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/engine/jdbc/mutation/internal/ModelMutationHelper.java#L59-L95","documentation":"When a single insert/update/delete reports more affected rows than expected (TooManyRowsAffectedException), ModelMutationHelper wraps it as HibernateException 'Duplicate identifier in table (T) - Role#id': the database updated/deleted several rows for one identifier, which by Hibernate's mapping should be impossible for a correctly-keyed table.","triggerScenarios":"The mutated table physically contains multiple rows with the same primary key value — typically secondary tables or JOINED-inheritance child tables whose PK/FK constraint is missing or was dropped — or data was bulk-imported/merged creating duplicates, so an UPDATE ... WHERE id = ? hits several rows.","commonSituations":"Bulk loads into inheritance tables without enforced primary keys; constraints dropped 'temporarily' in prod and never restored; manual data fixes that inserted duplicate child rows; database restores that duplicated rows.","solutions":["Find the duplicates: SELECT id, COUNT(*) FROM <table> GROUP BY id HAVING COUNT(*) > 1, then repair/delete the extra rows","Restore the primary key / unique constraints on the affected table so duplicates cannot recur","Verify the entity mapping is not accidentally mapping two entities to the same table row space (shared tables, @Tables/@Table annotations)"],"exampleFix":"-- diagnosis\nSELECT id, COUNT(*) FROM child_table GROUP BY id HAVING COUNT(*) > 1;\n-- repair\nDELETE FROM child_table WHERE rowid NOT IN (SELECT MIN(rowid) FROM child_table GROUP BY id);\nALTER TABLE child_table ADD PRIMARY KEY (id);","handlingStrategy":"validation","validationCode":"// pre-flight duplicate check on id columns used by Hibernate\nSELECT id, COUNT(*) FROM child_table GROUP BY id HAVING COUNT(*) > 1;\n-- add PK constraints so the invariant cannot regress\nALTER TABLE child_table ADD CONSTRAINT pk_child PRIMARY KEY (id);","typeGuard":null,"tryCatchPattern":"try {\n    session.merge(entity);\n    session.flush();\n}\ncatch (HibernateException e) {\n    if (e.getMessage() != null && e.getMessage().startsWith(\"Duplicate identifier in table\")) {\n        // run the duplicate-detection query and page an on-call data fix\n    }\n}","preventionTips":["Always enforce PK/unique constraints on secondary and inheritance tables","Validate bulk-imported data for duplicate ids before it reaches the app","Audit any manual DDL that dropped constraints"],"tags":["data-integrity","duplicate-key","inheritance","update","hibernate"],"backgroundTag":"duplicate-primary-key-rows","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}