Skip to content

Commit c2f452a

Browse files
committed
Polish "Take JPA database action into account when setting ddlAuto"
See gh-25129
1 parent 674b01c commit c2f452a

File tree

2 files changed

+51
-30
lines changed

2 files changed

+51
-30
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/orm/jpa/HibernateProperties.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,14 +130,17 @@ private void applyScanner(Map<String, Object> result) {
130130
}
131131

132132
private String determineDdlAuto(Map<String, String> existing, Supplier<String> defaultDdlAuto) {
133-
if (existing.get(AvailableSettings.HBM2DDL_DATABASE_ACTION) != null) {
134-
return null;
135-
}
136133
String ddlAuto = existing.get(AvailableSettings.HBM2DDL_AUTO);
137134
if (ddlAuto != null) {
138135
return ddlAuto;
139136
}
140-
return (this.ddlAuto != null) ? this.ddlAuto : defaultDdlAuto.get();
137+
if (this.ddlAuto != null) {
138+
return this.ddlAuto;
139+
}
140+
if (existing.get(AvailableSettings.HBM2DDL_DATABASE_ACTION) != null) {
141+
return null;
142+
}
143+
return defaultDdlAuto.get();
141144
}
142145

143146
public static class Naming {

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfigurationTests.java

Lines changed: 44 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -373,47 +373,65 @@ void hibernatePropertiesCustomizerCanDisableBeanContainer() {
373373
}
374374

375375
@Test
376-
void dataSourceSchemaCreatedEventFiredWhenDdlAutoPropertyIsSet() {
377-
dataSourceSchemaCreatedEventFired("spring.jpa.hibernate.ddl-auto:create-drop", true);
376+
void vendorPropertiesWithEmbeddedDatabaseAndNoDdlProperty() {
377+
contextRunner().run(vendorProperties((vendorProperties) -> {
378+
assertThat(vendorProperties).doesNotContainKeys(AvailableSettings.HBM2DDL_DATABASE_ACTION);
379+
assertThat(vendorProperties.get(AvailableSettings.HBM2DDL_AUTO)).isEqualTo("create-drop");
380+
}));
378381
}
379382

380383
@Test
381-
void dataSourceSchemaCreatedEventNotFiredWhenDdlAutoPropertyIsSetToNone() {
382-
dataSourceSchemaCreatedEventFired("spring.jpa.hibernate.ddl-auto:none", false);
384+
void vendorPropertiesWithDdlAutoPropertyIsSet() {
385+
contextRunner().withPropertyValues("spring.jpa.hibernate.ddl-auto=update")
386+
.run(vendorProperties((vendorProperties) -> {
387+
assertThat(vendorProperties).doesNotContainKeys(AvailableSettings.HBM2DDL_DATABASE_ACTION);
388+
assertThat(vendorProperties.get(AvailableSettings.HBM2DDL_AUTO)).isEqualTo("update");
389+
}));
383390
}
384391

385392
@Test
386-
void dataSourceSchemaCreatedEventFiredWhenHibernateSpecificDdlAutoPropertyIsSet() {
387-
dataSourceSchemaCreatedEventFired("spring.jpa.properties.hibernate.hbm2ddl.auto=create", true);
393+
void vendorPropertiesWithDdlAutoPropertyAndHibernatePropertiesAreSet() {
394+
contextRunner()
395+
.withPropertyValues("spring.jpa.hibernate.ddl-auto=update",
396+
"spring.jpa.properties.hibernate.hbm2ddl.auto=create-drop")
397+
.run(vendorProperties((vendorProperties) -> {
398+
assertThat(vendorProperties).doesNotContainKeys(AvailableSettings.HBM2DDL_DATABASE_ACTION);
399+
assertThat(vendorProperties.get(AvailableSettings.HBM2DDL_AUTO)).isEqualTo("create-drop");
400+
}));
388401
}
389402

390403
@Test
391-
void dataSourceSchemaCreatedEventNotFiredWhenHibernateSpecificDdlAutoPropertyIsSetToNone() {
392-
dataSourceSchemaCreatedEventFired("spring.jpa.properties.hibernate.hbm2ddl.auto=none", false);
404+
void vendorPropertiesWithDdlAutoPropertyIsSetToNone() {
405+
contextRunner().withPropertyValues("spring.jpa.hibernate.ddl-auto=none")
406+
.run(vendorProperties((vendorProperties) -> assertThat(vendorProperties).doesNotContainKeys(
407+
AvailableSettings.HBM2DDL_DATABASE_ACTION, AvailableSettings.HBM2DDL_AUTO)));
393408
}
394409

395410
@Test
396-
void dataSourceSchemaCreatedEventFiredWhenJpaDbActionPropertyIsSet() {
397-
dataSourceSchemaCreatedEventFired(
398-
"spring.jpa.properties.javax.persistence.schema-generation.database.action=drop-and-create", true);
411+
void vendorPropertiesWhenJpaDdlActionIsSet() {
412+
contextRunner()
413+
.withPropertyValues("spring.jpa.properties.javax.persistence.schema-generation.database.action=create")
414+
.run(vendorProperties((vendorProperties) -> {
415+
assertThat(vendorProperties.get(AvailableSettings.HBM2DDL_DATABASE_ACTION)).isEqualTo("create");
416+
assertThat(vendorProperties).doesNotContainKeys(AvailableSettings.HBM2DDL_AUTO);
417+
}));
399418
}
400419

401420
@Test
402-
void dataSourceSchemaCreatedEventNotFiredWhenJpaDbActionPropertyIsSetToNone() {
403-
dataSourceSchemaCreatedEventFired(
404-
"spring.jpa.properties.javax.persistence.schema-generation.database.action=none", false);
405-
}
406-
407-
private void dataSourceSchemaCreatedEventFired(String schemaGenerationPropertyWithValue,
408-
boolean expectEventToBeFired) {
409-
contextRunner().withUserConfiguration(JpaUsingApplicationListenerConfiguration.class)
410-
.withPropertyValues("spring.datasource.initialization-mode=never", schemaGenerationPropertyWithValue)
411-
.run((context) -> {
412-
assertThat(context).hasNotFailed();
413-
assertThat(context.getBean(EventCapturingApplicationListener.class).events.stream()
414-
.filter(DataSourceSchemaCreatedEvent.class::isInstance))
415-
.hasSize(expectEventToBeFired ? 1 : 0);
416-
});
421+
void vendorPropertiesWhenBothDdlAutoPropertiesAreSet() {
422+
contextRunner()
423+
.withPropertyValues("spring.jpa.properties.javax.persistence.schema-generation.database.action=create",
424+
"spring.jpa.hibernate.ddl-auto=create-only")
425+
.run(vendorProperties((vendorProperties) -> {
426+
assertThat(vendorProperties.get(AvailableSettings.HBM2DDL_DATABASE_ACTION)).isEqualTo("create");
427+
assertThat(vendorProperties.get(AvailableSettings.HBM2DDL_AUTO)).isEqualTo("create-only");
428+
}));
429+
}
430+
431+
private ContextConsumer<AssertableApplicationContext> vendorProperties(
432+
Consumer<Map<String, Object>> vendorProperties) {
433+
return (context) -> vendorProperties
434+
.accept(context.getBean(HibernateJpaConfiguration.class).getVendorProperties());
417435
}
418436

419437
@Test

0 commit comments

Comments
 (0)