hibernate/hibernate-orm · error · SchemaExtractionException

Encountered primary keys differing name on table %s

Error message

Encountered primary keys differing name on table %s

What it means

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.

Source

Thrown at hibernate-core/src/main/java/org/hibernate/tool/schema/extract/internal/AbstractInformationExtractorImpl.java:948

	private PrimaryKeyInformation extractPrimaryKeyInformation(TableInformation tableInformation, ResultSet resultSet)
			throws SQLException {

		final List<ColumnInformation> columns = new ArrayList<>();
		boolean firstPass = true;
		Identifier primaryKeyIdentifier = null;

		while ( resultSet.next() ) {
			final String currentPkName = resultSet.getString( getResultSetPrimaryKeyNameLabel() );
			final Identifier currentPrimaryKeyIdentifier =
					currentPkName == null ? null : toIdentifier( currentPkName );
			if ( firstPass ) {
				primaryKeyIdentifier = currentPrimaryKeyIdentifier;
				firstPass = false;
			}
			else {
				if ( !Objects.equals( primaryKeyIdentifier, currentPrimaryKeyIdentifier ) ) {
					throw new SchemaExtractionException( "Encountered primary keys differing name on table "
							+ tableInformation.getName().toString() );
				}
			}

			final int columnPosition = resultSet.getInt( getResultSetColumnPositionColumn() );
			final int index = columnPosition - 1;
			// Fill up the array list with nulls up to the desired index, because some JDBC drivers don't return results ordered by column position
			while ( columns.size() <= index ) {
				columns.add( null );
			}
			final Identifier columnIdentifier =
					toIdentifier( resultSet.getString( getResultSetColumnNameLabel() ) );
			columns.set( index, tableInformation.getColumn( columnIdentifier ) );
		}
		if ( firstPass ) {
			// we did not find any results (no pk)
			return null;
		}

View on GitHub (pinned to fad1729dce)

Solutions

  1. Upgrade the JDBC driver — inconsistent PK_NAME for one table violates the JDBC contract.
  2. Qualify the mapping's catalog/schema so the resultset contains only the intended table's rows.
  3. Toggle hibernate.hbm2ddl.jdbc_metadata_extraction_strategy (grouped vs individually) to take a different metadata query path.
Defensive patterns

Strategy: try-catch

Try / catch

try {
    schemaValidator.validate(metadata, databaseModel);
} catch (SchemaExtractionException e) {
    if (e.getMessage() != null && e.getMessage().contains("primary keys differing name")) {
        // driver metadata inconsistency — upgrade driver, then re-run
    }
    throw e;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of hibernate/hibernate-orm@fad1729dce (2026-08-22). Data as JSON: /api/errors/d35ebf22491a6ee8. Report an issue: GitHub.