Skip to content

Commit

Permalink
HHH-18809 auto-disable use of getGeneratedKeys() when there is a @for…
Browse files Browse the repository at this point in the history
…mula

Signed-off-by: Gavin King <[email protected]>
  • Loading branch information
gavinking committed Nov 4, 2024
1 parent fc1d0f5 commit e4b52c2
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -326,7 +327,11 @@ private static List<? extends ModelPart> getActualGeneratedModelParts(
public static GeneratedValuesMutationDelegate getGeneratedValuesDelegate(
EntityPersister persister,
EventType timing) {
final boolean hasGeneratedProperties = !persister.getGeneratedProperties( timing ).isEmpty();
final List<? extends ModelPart> 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();

Expand All @@ -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 );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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
Expand Down

0 comments on commit e4b52c2

Please sign in to comment.