Skip to content

Commit

Permalink
Expose property quarkus.hibernate-orm.flush.mode
Browse files Browse the repository at this point in the history
  • Loading branch information
yrodiere committed Sep 12, 2024
1 parent 62039a3 commit 9246ebb
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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", "<default>")
// 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);
}

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

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

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

0 comments on commit 9246ebb

Please sign in to comment.