From 9246ebbd9d050042200530209ee9f569c4da4ce3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yoann=20Rodi=C3=A8re?= Date: Thu, 12 Sep 2024 17:17:09 +0200 Subject: [PATCH] Expose property `quarkus.hibernate-orm.flush.mode` --- .../properties/ConfigPropertiesTest.java | 51 +++++++++++++++++++ .../defaultpu/MyEntityForDefaultPU.java | 31 +++++++++++ .../overridespu/MyEntityForOverridesPU.java | 31 +++++++++++ .../FastBootHibernatePersistenceProvider.java | 4 ++ ...ernateOrmRuntimeConfigPersistenceUnit.java | 25 +++++++++ ...tHibernateReactivePersistenceProvider.java | 4 ++ 6 files changed, 146 insertions(+) create mode 100644 extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/config/properties/ConfigPropertiesTest.java create mode 100644 extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/config/properties/defaultpu/MyEntityForDefaultPU.java create mode 100644 extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/config/properties/overridespu/MyEntityForOverridesPU.java diff --git a/extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/config/properties/ConfigPropertiesTest.java b/extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/config/properties/ConfigPropertiesTest.java new file mode 100644 index 0000000000000..2d7211ddc7eb2 --- /dev/null +++ b/extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/config/properties/ConfigPropertiesTest.java @@ -0,0 +1,51 @@ +package io.quarkus.hibernate.orm.config.properties; + +import static org.assertj.core.api.Assertions.assertThat; + +import jakarta.inject.Inject; +import jakarta.transaction.Transactional; + +import org.hibernate.FlushMode; +import org.hibernate.Session; +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.spec.JavaArchive; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +import io.quarkus.hibernate.orm.PersistenceUnit; +import io.quarkus.hibernate.orm.config.properties.defaultpu.MyEntityForDefaultPU; +import io.quarkus.hibernate.orm.config.properties.overridespu.MyEntityForOverridesPU; +import io.quarkus.test.QuarkusUnitTest; + +/** + * Tests that configuration properties set in Quarkus are translated to the right key and value in Hibernate ORM. + */ +public class ConfigPropertiesTest { + + @RegisterExtension + static final QuarkusUnitTest config = new QuarkusUnitTest().setArchiveProducer( + () -> ShrinkWrap.create(JavaArchive.class) + .addPackage(MyEntityForDefaultPU.class.getPackage()) + .addPackage(MyEntityForOverridesPU.class.getPackage())) + .withConfigurationResource("application.properties") + .overrideConfigKey("quarkus.hibernate-orm.packages", MyEntityForDefaultPU.class.getPackageName()) + .overrideConfigKey("quarkus.hibernate-orm.\"overrides\".packages", MyEntityForOverridesPU.class.getPackageName()) + .overrideConfigKey("quarkus.hibernate-orm.\"overrides\".datasource", "") + // Overrides to test that Quarkus configuration properties are taken into account + .overrideConfigKey("quarkus.hibernate-orm.\"overrides\".flush.mode", "always"); + + @Inject + Session sessionForDefaultPU; + + @Inject + @PersistenceUnit("overrides") + Session sessionForOverridesPU; + + @Test + @Transactional + public void propertiesAffectingSession() { + assertThat(sessionForDefaultPU.getHibernateFlushMode()).isEqualTo(FlushMode.AUTO); + assertThat(sessionForOverridesPU.getHibernateFlushMode()).isEqualTo(FlushMode.ALWAYS); + } + +} diff --git a/extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/config/properties/defaultpu/MyEntityForDefaultPU.java b/extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/config/properties/defaultpu/MyEntityForDefaultPU.java new file mode 100644 index 0000000000000..c410f2e46674e --- /dev/null +++ b/extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/config/properties/defaultpu/MyEntityForDefaultPU.java @@ -0,0 +1,31 @@ +package io.quarkus.hibernate.orm.config.properties.defaultpu; + +import jakarta.persistence.Entity; +import jakarta.persistence.Id; + +@Entity +public class MyEntityForDefaultPU { + @Id + private long id; + + private String name; + + public MyEntityForDefaultPU() { + } + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/config/properties/overridespu/MyEntityForOverridesPU.java b/extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/config/properties/overridespu/MyEntityForOverridesPU.java new file mode 100644 index 0000000000000..9ad74808b9a34 --- /dev/null +++ b/extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/config/properties/overridespu/MyEntityForOverridesPU.java @@ -0,0 +1,31 @@ +package io.quarkus.hibernate.orm.config.properties.overridespu; + +import jakarta.persistence.Entity; +import jakarta.persistence.Id; + +@Entity +public class MyEntityForOverridesPU { + @Id + private long id; + + private String name; + + public MyEntityForOverridesPU() { + } + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/extensions/hibernate-orm/runtime/src/main/java/io/quarkus/hibernate/orm/runtime/FastBootHibernatePersistenceProvider.java b/extensions/hibernate-orm/runtime/src/main/java/io/quarkus/hibernate/orm/runtime/FastBootHibernatePersistenceProvider.java index c87cc25b89e96..6c37f8b83a99a 100644 --- a/extensions/hibernate-orm/runtime/src/main/java/io/quarkus/hibernate/orm/runtime/FastBootHibernatePersistenceProvider.java +++ b/extensions/hibernate-orm/runtime/src/main/java/io/quarkus/hibernate/orm/runtime/FastBootHibernatePersistenceProvider.java @@ -18,6 +18,7 @@ import org.hibernate.boot.registry.StandardServiceRegistry; import org.hibernate.boot.registry.internal.StandardServiceRegistryImpl; import org.hibernate.cfg.AvailableSettings; +import org.hibernate.jpa.HibernateHints; import org.hibernate.jpa.boot.spi.EntityManagerFactoryBuilder; import org.hibernate.jpa.boot.spi.PersistenceUnitDescriptor; import org.hibernate.service.Service; @@ -453,6 +454,9 @@ private static void injectRuntimeConfiguration(HibernateOrmRuntimeConfigPersiste runtimeSettingsBuilder.put(AvailableSettings.LOG_SLOW_QUERY, persistenceUnitConfig.log().queriesSlowerThanMs().get()); } + + runtimeSettingsBuilder.put(HibernateHints.HINT_FLUSH_MODE, + persistenceUnitConfig.flush().mode()); } } diff --git a/extensions/hibernate-orm/runtime/src/main/java/io/quarkus/hibernate/orm/runtime/HibernateOrmRuntimeConfigPersistenceUnit.java b/extensions/hibernate-orm/runtime/src/main/java/io/quarkus/hibernate/orm/runtime/HibernateOrmRuntimeConfigPersistenceUnit.java index 3d02a2d4f631e..61fe9ba2c4b6b 100644 --- a/extensions/hibernate-orm/runtime/src/main/java/io/quarkus/hibernate/orm/runtime/HibernateOrmRuntimeConfigPersistenceUnit.java +++ b/extensions/hibernate-orm/runtime/src/main/java/io/quarkus/hibernate/orm/runtime/HibernateOrmRuntimeConfigPersistenceUnit.java @@ -3,6 +3,8 @@ import java.util.Map; import java.util.Optional; +import org.hibernate.FlushMode; + import io.quarkus.runtime.annotations.ConfigDocDefault; import io.quarkus.runtime.annotations.ConfigDocMapKey; import io.quarkus.runtime.annotations.ConfigDocSection; @@ -50,6 +52,12 @@ public interface HibernateOrmRuntimeConfigPersistenceUnit { @ConfigDocSection HibernateOrmConfigPersistenceUnitLog log(); + /** + * Flush configuration. + */ + @ConfigDocSection + HibernateOrmConfigPersistenceUnitFlush flush(); + /** * Properties that should be passed on directly to Hibernate ORM. * Use the full configuration property key here, @@ -200,4 +208,21 @@ interface HibernateOrmConfigPersistenceUnitLog { } + @ConfigGroup + interface HibernateOrmConfigPersistenceUnitFlush { + /** + * The default flushing strategy, or when to flush entities to the database in a Hibernate session: + * before every query, on commit, ... + * + * This default can be overridden on a per-session basis with `Session#setHibernateFlushMode()` + * or on a per-query basis with the hint `HibernateHints#HINT_FLUSH_MODE`. + * + * See the javadoc of `org.hibernate.FlushMode` for details. + * + * @asciidoclet + */ + @WithDefault("auto") + FlushMode mode(); + } + } diff --git a/extensions/hibernate-reactive/runtime/src/main/java/io/quarkus/hibernate/reactive/runtime/FastBootHibernateReactivePersistenceProvider.java b/extensions/hibernate-reactive/runtime/src/main/java/io/quarkus/hibernate/reactive/runtime/FastBootHibernateReactivePersistenceProvider.java index c00a346cd72b7..51294feb0bd6d 100644 --- a/extensions/hibernate-reactive/runtime/src/main/java/io/quarkus/hibernate/reactive/runtime/FastBootHibernateReactivePersistenceProvider.java +++ b/extensions/hibernate-reactive/runtime/src/main/java/io/quarkus/hibernate/reactive/runtime/FastBootHibernateReactivePersistenceProvider.java @@ -18,6 +18,7 @@ import org.hibernate.boot.registry.StandardServiceRegistry; import org.hibernate.boot.registry.internal.StandardServiceRegistryImpl; import org.hibernate.cfg.AvailableSettings; +import org.hibernate.jpa.HibernateHints; import org.hibernate.jpa.boot.spi.EntityManagerFactoryBuilder; import org.hibernate.jpa.boot.spi.PersistenceUnitDescriptor; import org.hibernate.reactive.provider.service.ReactiveGenerationTarget; @@ -361,6 +362,9 @@ private static void injectRuntimeConfiguration(HibernateOrmRuntimeConfigPersiste runtimeSettingsBuilder.put(AvailableSettings.LOG_SLOW_QUERY, persistenceUnitConfig.log().queriesSlowerThanMs().get()); } + + runtimeSettingsBuilder.put(HibernateHints.HINT_FLUSH_MODE, + persistenceUnitConfig.flush().mode()); } @Override