Skip to content
Closed
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
Expand Up @@ -128,7 +128,8 @@ private boolean isInitializingDatabase(DataSource dataSource) {
: "none");
Map<String, Object> hibernate = this.hibernateProperties.determineHibernateProperties(
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");
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am curious as to why? Should we do the same for the db-action property?

I found out what relies on the removed key. I have added the db-action property to this check to ensure we have consistent behavior when the user is setting only the db-action property to drive db schema creation. I also added tests to verify the cases.

}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
* Configuration properties for Hibernate.
*
* @author Stephane Nicoll
* @author Chris Bono
* @since 2.1.0
* @see JpaProperties
*/
Expand Down Expand Up @@ -129,6 +130,9 @@ private void applyScanner(Map<String, Object> result) {
}

private String determineDdlAuto(Map<String, String> existing, Supplier<String> defaultDdlAuto) {
if (existing.get(AvailableSettings.HBM2DDL_DATABASE_ACTION) != null) {
return null;
}
String ddlAuto = existing.get(AvailableSettings.HBM2DDL_AUTO);
if (ddlAuto != null) {
return ddlAuto;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
* @author Andy Wilkinson
* @author Kazuki Shimizu
* @author Stephane Nicoll
* @author Chris Bono
*/
class HibernateJpaAutoConfigurationTests extends AbstractJpaAutoConfigurationTests {

Expand Down Expand Up @@ -371,6 +372,50 @@ void hibernatePropertiesCustomizerCanDisableBeanContainer() {
.run((context) -> assertThat(context).doesNotHaveBean(City.class));
}

@Test
void dataSourceSchemaCreatedEventFiredWhenDdlAutoPropertyIsSet() {
dataSourceSchemaCreatedEventFired("spring.jpa.hibernate.ddl-auto:create-drop", true);
}

@Test
void dataSourceSchemaCreatedEventNotFiredWhenDdlAutoPropertyIsSetToNone() {
dataSourceSchemaCreatedEventFired("spring.jpa.hibernate.ddl-auto:none", false);
}

@Test
void dataSourceSchemaCreatedEventFiredWhenHibernateSpecificDdlAutoPropertyIsSet() {
dataSourceSchemaCreatedEventFired("spring.jpa.properties.hibernate.hbm2ddl.auto=create", true);
}

@Test
void dataSourceSchemaCreatedEventNotFiredWhenHibernateSpecificDdlAutoPropertyIsSetToNone() {
dataSourceSchemaCreatedEventFired("spring.jpa.properties.hibernate.hbm2ddl.auto=none", false);
}

@Test
void dataSourceSchemaCreatedEventFiredWhenJpaDbActionPropertyIsSet() {
dataSourceSchemaCreatedEventFired(
"spring.jpa.properties.javax.persistence.schema-generation.database.action=drop-and-create", true);
}

@Test
void dataSourceSchemaCreatedEventNotFiredWhenJpaDbActionPropertyIsSetToNone() {
dataSourceSchemaCreatedEventFired(
"spring.jpa.properties.javax.persistence.schema-generation.database.action=none", false);
}

private void dataSourceSchemaCreatedEventFired(String schemaGenerationPropertyWithValue,
boolean expectEventToBeFired) {
contextRunner().withUserConfiguration(JpaUsingApplicationListenerConfiguration.class)
.withPropertyValues("spring.datasource.initialization-mode=never", schemaGenerationPropertyWithValue)
.run((context) -> {
assertThat(context).hasNotFailed();
assertThat(context.getBean(EventCapturingApplicationListener.class).events.stream()
.filter(DataSourceSchemaCreatedEvent.class::isInstance))
.hasSize(expectEventToBeFired ? 1 : 0);
});
}

@Test
void withSyncBootstrappingAnApplicationListenerThatUsesJpaDoesNotTriggerABeanCurrentlyInCreationException() {
contextRunner().withUserConfiguration(JpaUsingApplicationListenerConfiguration.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
*
* @author Stephane Nicoll
* @author Artsiom Yudovin
* @author Chris Bono
*/
class HibernatePropertiesTests {

Expand Down Expand Up @@ -135,6 +136,19 @@ void defaultDdlAutoIsNotInvokedIfHibernateSpecificPropertyIsSet() {
.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) {
return assertHibernateProperties((hibernateProperties) -> {
assertThat(hibernateProperties).containsEntry(AvailableSettings.HBM2DDL_AUTO, expectedDdlAuto);
Expand Down