Remove references to s.j.generate-ddl in favor of s.j.hibernate.ddl-auto

This commit is contained in:
Andy Wilkinson
2024-04-24 21:24:47 +01:00
parent a08fe0d75b
commit 273f8e20c2
4 changed files with 95 additions and 16 deletions

View File

@@ -46,6 +46,7 @@ import org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform;
import org.hibernate.engine.transaction.jta.platform.spi.JtaPlatform;
import org.hibernate.internal.SessionFactoryImpl;
import org.hibernate.jpa.HibernatePersistenceProvider;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.springframework.aot.hint.MemberCategory;
@@ -75,6 +76,7 @@ import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
@@ -508,6 +510,88 @@ class HibernateJpaAutoConfigurationTests extends AbstractJpaAutoConfigurationTes
}
}
@Test
@Disabled("gh-40177")
void whenSpringJpaGenerateDdlIsNotSetThenTableIsNotCreated() {
// spring.jpa.generated-ddl defaults to false but this test still fails because
// we're using an embedded database which means that HibernateProperties defaults
// hibernate.hbm2ddl.auto to create-drop, replacing the
// hibernate.hbm2ddl.auto=none that comes from generate-ddl being false.
contextRunner().run((context) -> assertThat(tablesFrom(context)).doesNotContain("CITY"));
}
@Test
void whenSpringJpaGenerateDdlIsTrueThenTableIsCreated() {
contextRunner().withPropertyValues("spring.jpa.generate-ddl=true")
.run((context) -> assertThat(tablesFrom(context)).contains("CITY"));
}
@Test
@Disabled("gh-40177")
void whenSpringJpaGenerateDdlIsFalseThenTableIsNotCreated() {
// This test fails because we're using an embedded database which means that
// HibernateProperties defaults hibernate.hbm2ddl.auto to create-drop, replacing
// the hibernate.hbm2ddl.auto=none that comes from setting generate-ddl to false.
contextRunner().withPropertyValues("spring.jpa.generate-ddl=false")
.run((context) -> assertThat(tablesFrom(context)).doesNotContain("CITY"));
}
@Test
void whenHbm2DdlAutoIsNoneThenTableIsNotCreated() {
contextRunner().withPropertyValues("spring.jpa.properties.hibernate.hbm2ddl.auto=none")
.run((context) -> assertThat(tablesFrom(context)).doesNotContain("CITY"));
}
@Test
void whenSpringJpaHibernateDdlAutoIsNoneThenTableIsNotCreated() {
contextRunner().withPropertyValues("spring.jpa.hibernate.ddl-auto=none")
.run((context) -> assertThat(tablesFrom(context)).doesNotContain("CITY"));
}
@Test
@Disabled("gh-40177")
void whenSpringJpaGenerateDdlIsTrueAndSpringJpaHibernateDdlAutoIsNoneThenTableIsNotCreated() {
// This test fails because when ddl-auto is set to none, we remove
// hibernate.hbm2ddl.auto from Hibernate properties. This then allows
// spring.jpa.generate-ddl to set it to create-drop
contextRunner().withPropertyValues("spring.jpa.generate-ddl=true", "spring.jpa.hibernate.ddl-auto=none")
.run((context) -> assertThat(tablesFrom(context)).doesNotContain("CITY"));
}
@Test
void whenSpringJpaGenerateDdlIsTrueAndSpringJpaHibernateDdlAutoIsDropThenTableIsNotCreated() {
contextRunner().withPropertyValues("spring.jpa.generate-ddl=true", "spring.jpa.hibernate.ddl-auto=drop")
.run((context) -> assertThat(tablesFrom(context)).doesNotContain("CITY"));
}
@Test
void whenSpringJpaGenerateDdlIsTrueAndJakartaSchemaGenerationIsNoneThenTableIsNotCreated() {
contextRunner()
.withPropertyValues("spring.jpa.generate-ddl=true",
"spring.jpa.properties.jakarta.persistence.schema-generation.database.action=none")
.run((context) -> {
assertThat(tablesFrom(context)).doesNotContain("CITY");
});
}
@Test
void whenSpringJpaGenerateDdlIsTrueSpringJpaHibernateDdlAutoIsCreateAndJakartaSchemaGenerationIsNoneThenTableIsNotCreated() {
contextRunner()
.withPropertyValues("spring.jpa.generate-ddl=true", "spring.jpa.hibernate.ddl-auto=create",
"spring.jpa.properties.jakarta.persistence.schema-generation.database.action=none")
.run((context) -> {
assertThat(tablesFrom(context)).doesNotContain("CITY");
});
}
private List<String> tablesFrom(AssertableApplicationContext context) {
DataSource dataSource = context.getBean(DataSource.class);
JdbcTemplate jdbc = new JdbcTemplate(dataSource);
List<String> tables = jdbc.query("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES",
(results, row) -> results.getString(1));
return tables;
}
@Configuration(proxyBeanMethods = false)
@TestAutoConfigurationPackage(City.class)
@DependsOnDatabaseInitialization