Commit 2ecae2df authored by Stephane Nicoll's avatar Stephane Nicoll

Merge branch '2.3.x' into 2.4.x

Closes gh-25754
parents 6f860b44 2b29b999
...@@ -129,7 +129,8 @@ class DataSourceInitializedPublisher implements BeanPostProcessor { ...@@ -129,7 +129,8 @@ class DataSourceInitializedPublisher implements BeanPostProcessor {
: "none"); : "none");
Map<String, Object> hibernate = this.hibernateProperties.determineHibernateProperties( Map<String, Object> hibernate = this.hibernateProperties.determineHibernateProperties(
this.jpaProperties.getProperties(), new HibernateSettings().ddlAuto(defaultDdlAuto)); this.jpaProperties.getProperties(), new HibernateSettings().ddlAuto(defaultDdlAuto));
return hibernate.containsKey("hibernate.hbm2ddl.auto"); return hibernate.containsKey("hibernate.hbm2ddl.auto") || !hibernate
.getOrDefault("javax.persistence.schema-generation.database.action", "none").equals("none");
} }
/** /**
......
...@@ -35,6 +35,7 @@ import org.springframework.util.StringUtils; ...@@ -35,6 +35,7 @@ import org.springframework.util.StringUtils;
* Configuration properties for Hibernate. * Configuration properties for Hibernate.
* *
* @author Stephane Nicoll * @author Stephane Nicoll
* @author Chris Bono
* @since 2.1.0 * @since 2.1.0
* @see JpaProperties * @see JpaProperties
*/ */
...@@ -133,7 +134,13 @@ public class HibernateProperties { ...@@ -133,7 +134,13 @@ public class HibernateProperties {
if (ddlAuto != null) { if (ddlAuto != null) {
return ddlAuto; return ddlAuto;
} }
return (this.ddlAuto != null) ? this.ddlAuto : defaultDdlAuto.get(); if (this.ddlAuto != null) {
return this.ddlAuto;
}
if (existing.get(AvailableSettings.HBM2DDL_DATABASE_ACTION) != null) {
return null;
}
return defaultDdlAuto.get();
} }
public static class Naming { public static class Naming {
......
...@@ -91,6 +91,7 @@ import static org.mockito.Mockito.mock; ...@@ -91,6 +91,7 @@ import static org.mockito.Mockito.mock;
* @author Andy Wilkinson * @author Andy Wilkinson
* @author Kazuki Shimizu * @author Kazuki Shimizu
* @author Stephane Nicoll * @author Stephane Nicoll
* @author Chris Bono
*/ */
class HibernateJpaAutoConfigurationTests extends AbstractJpaAutoConfigurationTests { class HibernateJpaAutoConfigurationTests extends AbstractJpaAutoConfigurationTests {
...@@ -371,6 +372,68 @@ class HibernateJpaAutoConfigurationTests extends AbstractJpaAutoConfigurationTes ...@@ -371,6 +372,68 @@ class HibernateJpaAutoConfigurationTests extends AbstractJpaAutoConfigurationTes
.run((context) -> assertThat(context).doesNotHaveBean(City.class)); .run((context) -> assertThat(context).doesNotHaveBean(City.class));
} }
@Test
void vendorPropertiesWithEmbeddedDatabaseAndNoDdlProperty() {
contextRunner().run(vendorProperties((vendorProperties) -> {
assertThat(vendorProperties).doesNotContainKeys(AvailableSettings.HBM2DDL_DATABASE_ACTION);
assertThat(vendorProperties.get(AvailableSettings.HBM2DDL_AUTO)).isEqualTo("create-drop");
}));
}
@Test
void vendorPropertiesWithDdlAutoPropertyIsSet() {
contextRunner().withPropertyValues("spring.jpa.hibernate.ddl-auto=update")
.run(vendorProperties((vendorProperties) -> {
assertThat(vendorProperties).doesNotContainKeys(AvailableSettings.HBM2DDL_DATABASE_ACTION);
assertThat(vendorProperties.get(AvailableSettings.HBM2DDL_AUTO)).isEqualTo("update");
}));
}
@Test
void vendorPropertiesWithDdlAutoPropertyAndHibernatePropertiesAreSet() {
contextRunner()
.withPropertyValues("spring.jpa.hibernate.ddl-auto=update",
"spring.jpa.properties.hibernate.hbm2ddl.auto=create-drop")
.run(vendorProperties((vendorProperties) -> {
assertThat(vendorProperties).doesNotContainKeys(AvailableSettings.HBM2DDL_DATABASE_ACTION);
assertThat(vendorProperties.get(AvailableSettings.HBM2DDL_AUTO)).isEqualTo("create-drop");
}));
}
@Test
void vendorPropertiesWithDdlAutoPropertyIsSetToNone() {
contextRunner().withPropertyValues("spring.jpa.hibernate.ddl-auto=none")
.run(vendorProperties((vendorProperties) -> assertThat(vendorProperties).doesNotContainKeys(
AvailableSettings.HBM2DDL_DATABASE_ACTION, AvailableSettings.HBM2DDL_AUTO)));
}
@Test
void vendorPropertiesWhenJpaDdlActionIsSet() {
contextRunner()
.withPropertyValues("spring.jpa.properties.javax.persistence.schema-generation.database.action=create")
.run(vendorProperties((vendorProperties) -> {
assertThat(vendorProperties.get(AvailableSettings.HBM2DDL_DATABASE_ACTION)).isEqualTo("create");
assertThat(vendorProperties).doesNotContainKeys(AvailableSettings.HBM2DDL_AUTO);
}));
}
@Test
void vendorPropertiesWhenBothDdlAutoPropertiesAreSet() {
contextRunner()
.withPropertyValues("spring.jpa.properties.javax.persistence.schema-generation.database.action=create",
"spring.jpa.hibernate.ddl-auto=create-only")
.run(vendorProperties((vendorProperties) -> {
assertThat(vendorProperties.get(AvailableSettings.HBM2DDL_DATABASE_ACTION)).isEqualTo("create");
assertThat(vendorProperties.get(AvailableSettings.HBM2DDL_AUTO)).isEqualTo("create-only");
}));
}
private ContextConsumer<AssertableApplicationContext> vendorProperties(
Consumer<Map<String, Object>> vendorProperties) {
return (context) -> vendorProperties
.accept(context.getBean(HibernateJpaConfiguration.class).getVendorProperties());
}
@Test @Test
void withSyncBootstrappingAnApplicationListenerThatUsesJpaDoesNotTriggerABeanCurrentlyInCreationException() { void withSyncBootstrappingAnApplicationListenerThatUsesJpaDoesNotTriggerABeanCurrentlyInCreationException() {
contextRunner().withUserConfiguration(JpaUsingApplicationListenerConfiguration.class) contextRunner().withUserConfiguration(JpaUsingApplicationListenerConfiguration.class)
......
...@@ -44,6 +44,7 @@ import static org.mockito.Mockito.verify; ...@@ -44,6 +44,7 @@ import static org.mockito.Mockito.verify;
* *
* @author Stephane Nicoll * @author Stephane Nicoll
* @author Artsiom Yudovin * @author Artsiom Yudovin
* @author Chris Bono
*/ */
@ExtendWith(MockitoExtension.class) @ExtendWith(MockitoExtension.class)
class HibernatePropertiesTests { class HibernatePropertiesTests {
...@@ -131,6 +132,19 @@ class HibernatePropertiesTests { ...@@ -131,6 +132,19 @@ class HibernatePropertiesTests {
.run(assertDefaultDdlAutoNotInvoked("create")); .run(assertDefaultDdlAutoNotInvoked("create"));
} }
@Test
void defaultDdlAutoIsNotInvokedAndDdlAutoIsNotSetIfJpaDbActionPropertyIsSet() {
this.contextRunner
.withPropertyValues(
"spring.jpa.properties.javax.persistence.schema-generation.database.action=drop-and-create")
.run(assertHibernateProperties((hibernateProperties) -> {
assertThat(hibernateProperties).doesNotContainKey(AvailableSettings.HBM2DDL_AUTO);
assertThat(hibernateProperties).containsEntry(AvailableSettings.HBM2DDL_DATABASE_ACTION,
"drop-and-create");
verify(this.ddlAutoSupplier, never()).get();
}));
}
private ContextConsumer<AssertableApplicationContext> assertDefaultDdlAutoNotInvoked(String expectedDdlAuto) { private ContextConsumer<AssertableApplicationContext> assertDefaultDdlAutoNotInvoked(String expectedDdlAuto) {
return assertHibernateProperties((hibernateProperties) -> { return assertHibernateProperties((hibernateProperties) -> {
assertThat(hibernateProperties).containsEntry(AvailableSettings.HBM2DDL_AUTO, expectedDdlAuto); assertThat(hibernateProperties).containsEntry(AvailableSettings.HBM2DDL_AUTO, expectedDdlAuto);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment