Skip to content

Commit 273f8e2

Browse files
committed
Remove references to s.j.generate-ddl in favor of s.j.hibernate.ddl-auto
1 parent a08fe0d commit 273f8e2

File tree

4 files changed

+95
-16
lines changed

4 files changed

+95
-16
lines changed

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

+84
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import org.hibernate.engine.transaction.jta.platform.spi.JtaPlatform;
4747
import org.hibernate.internal.SessionFactoryImpl;
4848
import org.hibernate.jpa.HibernatePersistenceProvider;
49+
import org.junit.jupiter.api.Disabled;
4950
import org.junit.jupiter.api.Test;
5051

5152
import org.springframework.aot.hint.MemberCategory;
@@ -75,6 +76,7 @@
7576
import org.springframework.context.annotation.Bean;
7677
import org.springframework.context.annotation.Configuration;
7778
import org.springframework.context.event.ContextRefreshedEvent;
79+
import org.springframework.jdbc.core.JdbcTemplate;
7880
import org.springframework.orm.jpa.JpaTransactionManager;
7981
import org.springframework.orm.jpa.JpaVendorAdapter;
8082
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
@@ -508,6 +510,88 @@ void registersHintsForNamingClasses() {
508510
}
509511
}
510512

513+
@Test
514+
@Disabled("gh-40177")
515+
void whenSpringJpaGenerateDdlIsNotSetThenTableIsNotCreated() {
516+
// spring.jpa.generated-ddl defaults to false but this test still fails because
517+
// we're using an embedded database which means that HibernateProperties defaults
518+
// hibernate.hbm2ddl.auto to create-drop, replacing the
519+
// hibernate.hbm2ddl.auto=none that comes from generate-ddl being false.
520+
contextRunner().run((context) -> assertThat(tablesFrom(context)).doesNotContain("CITY"));
521+
}
522+
523+
@Test
524+
void whenSpringJpaGenerateDdlIsTrueThenTableIsCreated() {
525+
contextRunner().withPropertyValues("spring.jpa.generate-ddl=true")
526+
.run((context) -> assertThat(tablesFrom(context)).contains("CITY"));
527+
}
528+
529+
@Test
530+
@Disabled("gh-40177")
531+
void whenSpringJpaGenerateDdlIsFalseThenTableIsNotCreated() {
532+
// This test fails because we're using an embedded database which means that
533+
// HibernateProperties defaults hibernate.hbm2ddl.auto to create-drop, replacing
534+
// the hibernate.hbm2ddl.auto=none that comes from setting generate-ddl to false.
535+
contextRunner().withPropertyValues("spring.jpa.generate-ddl=false")
536+
.run((context) -> assertThat(tablesFrom(context)).doesNotContain("CITY"));
537+
}
538+
539+
@Test
540+
void whenHbm2DdlAutoIsNoneThenTableIsNotCreated() {
541+
contextRunner().withPropertyValues("spring.jpa.properties.hibernate.hbm2ddl.auto=none")
542+
.run((context) -> assertThat(tablesFrom(context)).doesNotContain("CITY"));
543+
}
544+
545+
@Test
546+
void whenSpringJpaHibernateDdlAutoIsNoneThenTableIsNotCreated() {
547+
contextRunner().withPropertyValues("spring.jpa.hibernate.ddl-auto=none")
548+
.run((context) -> assertThat(tablesFrom(context)).doesNotContain("CITY"));
549+
}
550+
551+
@Test
552+
@Disabled("gh-40177")
553+
void whenSpringJpaGenerateDdlIsTrueAndSpringJpaHibernateDdlAutoIsNoneThenTableIsNotCreated() {
554+
// This test fails because when ddl-auto is set to none, we remove
555+
// hibernate.hbm2ddl.auto from Hibernate properties. This then allows
556+
// spring.jpa.generate-ddl to set it to create-drop
557+
contextRunner().withPropertyValues("spring.jpa.generate-ddl=true", "spring.jpa.hibernate.ddl-auto=none")
558+
.run((context) -> assertThat(tablesFrom(context)).doesNotContain("CITY"));
559+
}
560+
561+
@Test
562+
void whenSpringJpaGenerateDdlIsTrueAndSpringJpaHibernateDdlAutoIsDropThenTableIsNotCreated() {
563+
contextRunner().withPropertyValues("spring.jpa.generate-ddl=true", "spring.jpa.hibernate.ddl-auto=drop")
564+
.run((context) -> assertThat(tablesFrom(context)).doesNotContain("CITY"));
565+
}
566+
567+
@Test
568+
void whenSpringJpaGenerateDdlIsTrueAndJakartaSchemaGenerationIsNoneThenTableIsNotCreated() {
569+
contextRunner()
570+
.withPropertyValues("spring.jpa.generate-ddl=true",
571+
"spring.jpa.properties.jakarta.persistence.schema-generation.database.action=none")
572+
.run((context) -> {
573+
assertThat(tablesFrom(context)).doesNotContain("CITY");
574+
});
575+
}
576+
577+
@Test
578+
void whenSpringJpaGenerateDdlIsTrueSpringJpaHibernateDdlAutoIsCreateAndJakartaSchemaGenerationIsNoneThenTableIsNotCreated() {
579+
contextRunner()
580+
.withPropertyValues("spring.jpa.generate-ddl=true", "spring.jpa.hibernate.ddl-auto=create",
581+
"spring.jpa.properties.jakarta.persistence.schema-generation.database.action=none")
582+
.run((context) -> {
583+
assertThat(tablesFrom(context)).doesNotContain("CITY");
584+
});
585+
}
586+
587+
private List<String> tablesFrom(AssertableApplicationContext context) {
588+
DataSource dataSource = context.getBean(DataSource.class);
589+
JdbcTemplate jdbc = new JdbcTemplate(dataSource);
590+
List<String> tables = jdbc.query("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES",
591+
(results, row) -> results.getString(1));
592+
return tables;
593+
}
594+
511595
@Configuration(proxyBeanMethods = false)
512596
@TestAutoConfigurationPackage(City.class)
513597
@DependsOnDatabaseInitialization

spring-boot-project/spring-boot-docs/src/docs/asciidoc/anchor-rewrite.properties

+3
Original file line numberDiff line numberDiff line change
@@ -1041,3 +1041,6 @@ features.testing.testcontainers.at-development-time=features.testcontainers.at-d
10411041
features.testing.testcontainers.at-development-time.dynamic-properties=features.testcontainers.at-development-time.dynamic-properties
10421042
features.testing.testcontainers.at-development-time.importing-container-declarations=features.testcontainers.at-development-time.importing-container-declarations
10431043
features.testing.testcontainers.at-development-time.devtools=features.testcontainers.at-development-time.devtools
1044+
1045+
# gh-40503
1046+
howto.data-initialization.using-jpa=howto.data-initialization.using-hibernate

spring-boot-project/spring-boot-docs/src/docs/asciidoc/data/sql.adoc

-1
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,6 @@ The following line shows an example of setting JPA properties for Hibernate:
282282
The line in the preceding example passes a value of `true` for the `hibernate.globally_quoted_identifiers` property to the Hibernate entity manager.
283283

284284
By default, the DDL execution (or validation) is deferred until the `ApplicationContext` has started.
285-
There is also a `spring.jpa.generate-ddl` flag, but it is not used if Hibernate auto-configuration is active, because the `ddl-auto` settings are more fine-grained.
286285

287286

288287

spring-boot-project/spring-boot-docs/src/docs/asciidoc/howto/data-initialization.adoc

+8-15
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,17 @@ It is recommended to use a single mechanism for schema generation.
66

77

88

9-
[[howto.data-initialization.using-jpa]]
10-
=== Initialize a Database Using JPA
11-
JPA has features for DDL generation, and these can be set up to run on startup against the database.
12-
This is controlled through two external properties:
13-
14-
* `spring.jpa.generate-ddl` (boolean) switches the feature on and off and is vendor independent.
15-
* `spring.jpa.hibernate.ddl-auto` (enum) is a Hibernate feature that controls the behavior in a more fine-grained way.
16-
This feature is described in more detail later in this guide.
17-
18-
199

2010
[[howto.data-initialization.using-hibernate]]
2111
=== Initialize a Database Using Hibernate
22-
You can set `spring.jpa.hibernate.ddl-auto` explicitly and the standard Hibernate property values are `none`, `validate`, `update`, `create`, and `create-drop`.
23-
Spring Boot chooses a default value for you based on whether it thinks your database is embedded.
24-
It defaults to `create-drop` if no schema manager has been detected or `none` in all other cases.
25-
An embedded database is detected by looking at the `Connection` type and JDBC url.
26-
`hsqldb`, `h2`, and `derby` are candidates, and others are not.
12+
You can set configprop:spring.jpa.hibernate.ddl-auto[] to control Hibernate's database initialization.
13+
Supported values are `none`, `validate`, `update`, `create`, and `create-drop`.
14+
Spring Boot chooses a default value for you based on whether you are using an embedded database.
15+
An embedded database is identified by looking at the `Connection` type and JDBC url.
16+
`hsqldb`, `h2`, or `derby` are embedded databases and others are not.
17+
If an embedded database is identified and no schema manager (Flyway or Liquibase) has been detected, `ddl-auto` defaults to `create-drop`.
18+
In all other cases, it defaults to `none`.
19+
2720
Be careful when switching from in-memory to a '`real`' database that you do not make assumptions about the existence of the tables and data in the new platform.
2821
You either have to set `ddl-auto` explicitly or use one of the other mechanisms to initialize the database.
2922

0 commit comments

Comments
 (0)