Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose property quarkus.hibernate-orm.flush.mode #43246

Merged
merged 1 commit into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading