diff --git a/hibernate-core/src/main/java/org/hibernate/generator/values/internal/GeneratedValuesHelper.java b/hibernate-core/src/main/java/org/hibernate/generator/values/internal/GeneratedValuesHelper.java index 28775eaf5be8..2bb21a814f49 100644 --- a/hibernate-core/src/main/java/org/hibernate/generator/values/internal/GeneratedValuesHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/generator/values/internal/GeneratedValuesHelper.java @@ -27,6 +27,7 @@ import org.hibernate.internal.CoreMessageLogger; import org.hibernate.metamodel.mapping.BasicValuedModelPart; import org.hibernate.metamodel.mapping.ModelPart; +import org.hibernate.metamodel.mapping.SelectableMapping; import org.hibernate.persister.entity.EntityPersister; import org.hibernate.persister.entity.mutation.EntityTableMapping; import org.hibernate.pretty.MessageHelper; @@ -326,7 +327,11 @@ private static List getActualGeneratedModelParts( public static GeneratedValuesMutationDelegate getGeneratedValuesDelegate( EntityPersister persister, EventType timing) { - final boolean hasGeneratedProperties = !persister.getGeneratedProperties( timing ).isEmpty(); + final List generatedProperties = persister.getGeneratedProperties( timing ); + final boolean hasGeneratedProperties = !generatedProperties.isEmpty(); + final boolean hasFormula = + generatedProperties.stream() + .anyMatch( part -> part instanceof SelectableMapping selectable && selectable.isFormula() ); final boolean hasRowId = timing == EventType.INSERT && persister.getRowIdMapping() != null; final Dialect dialect = persister.getFactory().getJdbcServices().getDialect(); @@ -341,7 +346,8 @@ && noCustomSql( persister, timing ) ) { return null; } - if ( dialect.supportsInsertReturningGeneratedKeys() + if ( !hasFormula + && dialect.supportsInsertReturningGeneratedKeys() && persister.getFactory().getSessionFactoryOptions().isGetGeneratedKeysEnabled() ) { return new GetGeneratedKeysDelegate( persister, false, timing ); } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/generated/formula/FormulaGeneratedTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/generated/formula/FormulaGeneratedTest.java index a7707894398c..5f8a4c7f81e5 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/generated/formula/FormulaGeneratedTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/generated/formula/FormulaGeneratedTest.java @@ -9,16 +9,13 @@ import org.hibernate.annotations.Formula; import org.hibernate.annotations.Generated; import org.hibernate.testing.orm.junit.DomainModel; -import org.hibernate.testing.orm.junit.ServiceRegistry; import org.hibernate.testing.orm.junit.SessionFactory; import org.hibernate.testing.orm.junit.SessionFactoryScope; -import org.hibernate.testing.orm.junit.Setting; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; import java.math.BigDecimal; -import static org.hibernate.cfg.JdbcSettings.USE_GET_GENERATED_KEYS; import static org.junit.Assert.assertEquals; /** @@ -27,7 +24,7 @@ @SuppressWarnings("JUnitMalformedDeclaration") @DomainModel(annotatedClasses = FormulaGeneratedTest.OrderLine.class) @SessionFactory -@ServiceRegistry(settings = @Setting(name = USE_GET_GENERATED_KEYS, value = "false")) +//@ServiceRegistry(settings = @Setting(name = USE_GET_GENERATED_KEYS, value = "false")) public class FormulaGeneratedTest { @Test