hibernate/hibernate-orm · error · AssertionFailure

getGeneratedKeys() support is not enabled

Error message

getGeneratedKeys() support is not enabled

What it means

An org.hibernate.AssertionFailure thrown by MutationStatementPreparerImpl.checkAutoGeneratedKeysSupportEnabled(): an insert mutation is being prepared with Statement.RETURN_GENERATED_KEYS (identity-style id generation), but the SessionFactory setting hibernate.jdbc.use_get_generated_keys resolved to false. It signals a configuration contradiction, not a runtime data problem.

Source

Thrown at hibernate-core/src/main/java/org/hibernate/engine/jdbc/internal/MutationStatementPreparerImpl.java:62

		};
	}

	@Override
	public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) {
		if ( autoGeneratedKeys == PreparedStatement.RETURN_GENERATED_KEYS ) {
			checkAutoGeneratedKeysSupportEnabled();
		}
		return new StatementPreparationTemplate( sql ) {
			public PreparedStatement doPrepare() throws SQLException {
				//noinspection resource
				return connection().prepareStatement( sql, autoGeneratedKeys );
			}
		}.prepareStatement();
	}

	private void checkAutoGeneratedKeysSupportEnabled() {
		if ( ! settings().isGetGeneratedKeysEnabled() ) {
			throw new AssertionFailure( "getGeneratedKeys() support is not enabled" );
		}
	}

	@Override
	public PreparedStatement prepareStatement(String sql, String[] columnNames) {
		checkAutoGeneratedKeysSupportEnabled();
		return new StatementPreparationTemplate( sql ) {
			public PreparedStatement doPrepare() throws SQLException {
				//noinspection resource
				return connection().prepareStatement( sql, columnNames );
			}
		}.prepareStatement();
	}

	private abstract class StatementPreparationTemplate {
		protected final String sql;

		protected StatementPreparationTemplate(String incomingSql) {

View on GitHub (pinned to fad1729dce)

Solutions

  1. Remove the explicit hibernate.jdbc.use_get_generated_keys=false setting, or set it to true
  2. If the target driver genuinely lacks getGeneratedKeys support, change id generation to SEQUENCE, TABLE or UUID instead of IDENTITY
  3. Audit programmatic configuration (SessionFactoryBuilder, StandardServiceRegistryBuilder) for overrides of this key

Example fix

# before
hibernate.jdbc.use_get_generated_keys=false
@Entity class A { @Id @GeneratedValue(strategy=IDENTITY) Long id; }

# after: drop the override (default is auto-detected)
# hibernate.jdbc.use_get_generated_keys removed
Defensive patterns

Strategy: validation

Validate before calling

// assert config consistency before building the factory
Map<String,Object> props = new HashMap<>();
if (Boolean.FALSE.equals(props.get("hibernate.jdbc.use_get_generated_keys"))
        && usesIdentityGeneration(metadata)) {
    throw new IllegalStateException("IDENTITY generation requires hibernate.jdbc.use_get_generated_keys");
}

Prevention

When it happens

Trigger: Entity uses GenerationType.IDENTITY (or native) while hibernate.jdbc.use_get_generated_keys=false is set explicitly in persistence.xml/application.properties or via SessionFactoryBuilder, so the generated-keys insert path trips the assertion at flush time.

Common situations: Configs copied from old projects or tuning guides that disabled generated keys (often for batch-insert performance) and later reused on entities with identity columns; programmatic SessionFactoryBuilder overriding the setting.

Related errors


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