{"record":{"id":"d35ebf22491a6ee8","repo":"hibernate/hibernate-orm","slug":"encountered-primary-keys-differing-name-on-table","errorCode":null,"errorMessage":"Encountered primary keys differing name on table %s","messagePattern":"Encountered primary keys differing name on table (.+?)","errorType":"exception","errorClass":"SchemaExtractionException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/tool/schema/extract/internal/AbstractInformationExtractorImpl.java","lineNumber":948,"sourceCode":"\n\tprivate PrimaryKeyInformation extractPrimaryKeyInformation(TableInformation tableInformation, ResultSet resultSet)\n\t\t\tthrows SQLException {\n\n\t\tfinal List<ColumnInformation> columns = new ArrayList<>();\n\t\tboolean firstPass = true;\n\t\tIdentifier primaryKeyIdentifier = null;\n\n\t\twhile ( resultSet.next() ) {\n\t\t\tfinal String currentPkName = resultSet.getString( getResultSetPrimaryKeyNameLabel() );\n\t\t\tfinal Identifier currentPrimaryKeyIdentifier =\n\t\t\t\t\tcurrentPkName == null ? null : toIdentifier( currentPkName );\n\t\t\tif ( firstPass ) {\n\t\t\t\tprimaryKeyIdentifier = currentPrimaryKeyIdentifier;\n\t\t\t\tfirstPass = false;\n\t\t\t}\n\t\t\telse {\n\t\t\t\tif ( !Objects.equals( primaryKeyIdentifier, currentPrimaryKeyIdentifier ) ) {\n\t\t\t\t\tthrow new SchemaExtractionException( \"Encountered primary keys differing name on table \"\n\t\t\t\t\t\t\t+ tableInformation.getName().toString() );\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tfinal int columnPosition = resultSet.getInt( getResultSetColumnPositionColumn() );\n\t\t\tfinal int index = columnPosition - 1;\n\t\t\t// Fill up the array list with nulls up to the desired index, because some JDBC drivers don't return results ordered by column position\n\t\t\twhile ( columns.size() <= index ) {\n\t\t\t\tcolumns.add( null );\n\t\t\t}\n\t\t\tfinal Identifier columnIdentifier =\n\t\t\t\t\ttoIdentifier( resultSet.getString( getResultSetColumnNameLabel() ) );\n\t\t\tcolumns.set( index, tableInformation.getColumn( columnIdentifier ) );\n\t\t}\n\t\tif ( firstPass ) {\n\t\t\t// we did not find any results (no pk)\n\t\t\treturn null;\n\t\t}","sourceCodeStart":930,"sourceCodeEnd":966,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/tool/schema/extract/internal/AbstractInformationExtractorImpl.java#L930-L966","documentation":"Reading a single table's primary key via the JDBC metadata resultset, Hibernate expects every row for that table to repeat the same PK_NAME. When successive rows report different primary-key names, the metadata is self-inconsistent and extraction fails with SchemaExtractionException instead of building a wrong PrimaryKeyInformation.","triggerScenarios":"Per-table primary-key extraction where the driver returns rows with differing PK_NAME for the same table: a driver defect, or a resultset that actually spans name-matching tables in other schemas because the driver ignored the schema filter.","commonSituations":"Older Oracle/MySQL/DB2 JDBC drivers; same-named tables in multiple schemas on case-normalizing databases; views or materialized views with unusual metadata; drivers after a major version change.","solutions":["Upgrade the JDBC driver — inconsistent PK_NAME for one table violates the JDBC contract.","Qualify the mapping's catalog/schema so the resultset contains only the intended table's rows.","Toggle hibernate.hbm2ddl.jdbc_metadata_extraction_strategy (grouped vs individually) to take a different metadata query path."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n    schemaValidator.validate(metadata, databaseModel);\n} catch (SchemaExtractionException e) {\n    if (e.getMessage() != null && e.getMessage().contains(\"primary keys differing name\")) {\n        // driver metadata inconsistency — upgrade driver, then re-run\n    }\n    throw e;\n}","preventionTips":["Pin a JDBC driver version validated in CI against the real database.","Qualify catalog/schema on mappings to keep metadata resultsets single-table.","Re-test schema validation after any driver upgrade."],"tags":["hibernate","schema-extraction","primary-key","jdbc-driver","metadata"],"backgroundTag":"inconsistent-primary-key-metadata","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}