HHH-18642 Revert to Statement.RETURN_GENERATED_KEYS for identity only

This commit is contained in:
Marco Belladelli 2024-09-23 17:04:59 +02:00
parent 0c8ac1c6e3
commit 735f066c3d
2 changed files with 25 additions and 5 deletions

View File

@ -1057,6 +1057,19 @@ public class DB2LegacyDialect extends Dialect {
return DB2IdentityColumnSupport.INSTANCE;
}
/**
* @return {@code true} because we can use {@code select ... from new table (insert .... )}
*/
@Override
public boolean supportsInsertReturning() {
return true;
}
@Override
public boolean supportsInsertReturningRowId() {
return false;
}
@Override
public boolean supportsValuesList() {
return true;

View File

@ -6,9 +6,10 @@
*/
package org.hibernate.id;
import java.util.List;
import org.hibernate.boot.spi.SessionFactoryOptions;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.generator.OnExecutionGenerator;
import org.hibernate.id.factory.spi.StandardGenerator;
import org.hibernate.id.insert.BasicSelectingDelegate;
@ -16,6 +17,7 @@ import org.hibernate.id.insert.GetGeneratedKeysDelegate;
import org.hibernate.id.insert.InsertGeneratedIdentifierDelegate;
import org.hibernate.id.insert.InsertReturningDelegate;
import org.hibernate.id.insert.UniqueKeySelectingDelegate;
import org.hibernate.metamodel.mapping.ModelPart;
import static org.hibernate.generator.EventType.INSERT;
import static org.hibernate.generator.internal.NaturalIdHelper.getNaturalIdPropertyNames;
@ -54,11 +56,16 @@ public class IdentityGenerator
@Override
public InsertGeneratedIdentifierDelegate getGeneratedIdentifierDelegate(PostInsertIdentityPersister persister) {
final SessionFactoryImplementor factory = persister.getFactory();
final Dialect dialect = factory.getJdbcServices().getDialect();
final Dialect dialect = persister.getFactory().getJdbcServices().getDialect();
final SessionFactoryOptions sessionFactoryOptions = persister.getFactory().getSessionFactoryOptions();
final List<? extends ModelPart> generatedProperties = persister.getGeneratedProperties( INSERT );
if ( generatedProperties.size() == 1 && sessionFactoryOptions.isGetGeneratedKeysEnabled() ) {
// Use Connection#prepareStatement(sql, Statement.RETURN_GENERATED_KEYS) when only retrieving identity
assert generatedProperties.get( 0 ).isEntityIdentifierMapping();
return dialect.getIdentityColumnSupport().buildGetGeneratedKeysDelegate( persister );
}
// Try to use generic delegates if the dialects supports them
final SessionFactoryOptions sessionFactoryOptions = factory.getSessionFactoryOptions();
if ( dialect.supportsInsertReturningGeneratedKeys() && sessionFactoryOptions.isGetGeneratedKeysEnabled() ) {
else if ( dialect.supportsInsertReturningGeneratedKeys() && sessionFactoryOptions.isGetGeneratedKeysEnabled() ) {
return new GetGeneratedKeysDelegate( persister, false, INSERT );
}
else if ( dialect.supportsInsertReturning() && noCustomSql( persister, INSERT ) ) {