diff --git a/hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/DB2LegacyDialect.java b/hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/DB2LegacyDialect.java index 4aa408b64b..0dc198df18 100644 --- a/hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/DB2LegacyDialect.java +++ b/hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/DB2LegacyDialect.java @@ -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; diff --git a/hibernate-core/src/main/java/org/hibernate/id/IdentityGenerator.java b/hibernate-core/src/main/java/org/hibernate/id/IdentityGenerator.java index 407cab115e..f07d21409f 100644 --- a/hibernate-core/src/main/java/org/hibernate/id/IdentityGenerator.java +++ b/hibernate-core/src/main/java/org/hibernate/id/IdentityGenerator.java @@ -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 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 ) ) {