Skip to content

Commit 79c3f03

Browse files
committed
Merge branch '3.1.x' into 3.2.x
Closes gh-40522
2 parents c267179 + 27418ed commit 79c3f03

File tree

4 files changed

+91
-16
lines changed

4 files changed

+91
-16
lines changed

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

+80
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import org.hibernate.engine.transaction.jta.platform.spi.JtaPlatform;
4848
import org.hibernate.internal.SessionFactoryImpl;
4949
import org.hibernate.jpa.HibernatePersistenceProvider;
50+
import org.junit.jupiter.api.Disabled;
5051
import org.junit.jupiter.api.Test;
5152

5253
import org.springframework.aot.hint.MemberCategory;
@@ -76,6 +77,7 @@
7677
import org.springframework.context.annotation.Bean;
7778
import org.springframework.context.annotation.Configuration;
7879
import org.springframework.context.event.ContextRefreshedEvent;
80+
import org.springframework.jdbc.core.JdbcTemplate;
7981
import org.springframework.orm.jpa.JpaTransactionManager;
8082
import org.springframework.orm.jpa.JpaVendorAdapter;
8183
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
@@ -512,6 +514,84 @@ void registersHintsForNamingClasses() {
512514
}
513515
}
514516

517+
@Test
518+
@Disabled("gh-40177")
519+
void whenSpringJpaGenerateDdlIsNotSetThenTableIsNotCreated() {
520+
// spring.jpa.generated-ddl defaults to false but this test still fails because
521+
// we're using an embedded database which means that HibernateProperties defaults
522+
// hibernate.hbm2ddl.auto to create-drop, replacing the
523+
// hibernate.hbm2ddl.auto=none that comes from generate-ddl being false.
524+
contextRunner().run((context) -> assertThat(tablesFrom(context)).doesNotContain("CITY"));
525+
}
526+
527+
@Test
528+
void whenSpringJpaGenerateDdlIsTrueThenTableIsCreated() {
529+
contextRunner().withPropertyValues("spring.jpa.generate-ddl=true")
530+
.run((context) -> assertThat(tablesFrom(context)).contains("CITY"));
531+
}
532+
533+
@Test
534+
@Disabled("gh-40177")
535+
void whenSpringJpaGenerateDdlIsFalseThenTableIsNotCreated() {
536+
// This test fails because we're using an embedded database which means that
537+
// HibernateProperties defaults hibernate.hbm2ddl.auto to create-drop, replacing
538+
// the hibernate.hbm2ddl.auto=none that comes from setting generate-ddl to false.
539+
contextRunner().withPropertyValues("spring.jpa.generate-ddl=false")
540+
.run((context) -> assertThat(tablesFrom(context)).doesNotContain("CITY"));
541+
}
542+
543+
@Test
544+
void whenHbm2DdlAutoIsNoneThenTableIsNotCreated() {
545+
contextRunner().withPropertyValues("spring.jpa.properties.hibernate.hbm2ddl.auto=none")
546+
.run((context) -> assertThat(tablesFrom(context)).doesNotContain("CITY"));
547+
}
548+
549+
@Test
550+
void whenSpringJpaHibernateDdlAutoIsNoneThenTableIsNotCreated() {
551+
contextRunner().withPropertyValues("spring.jpa.hibernate.ddl-auto=none")
552+
.run((context) -> assertThat(tablesFrom(context)).doesNotContain("CITY"));
553+
}
554+
555+
@Test
556+
@Disabled("gh-40177")
557+
void whenSpringJpaGenerateDdlIsTrueAndSpringJpaHibernateDdlAutoIsNoneThenTableIsNotCreated() {
558+
// This test fails because when ddl-auto is set to none, we remove
559+
// hibernate.hbm2ddl.auto from Hibernate properties. This then allows
560+
// spring.jpa.generate-ddl to set it to create-drop
561+
contextRunner().withPropertyValues("spring.jpa.generate-ddl=true", "spring.jpa.hibernate.ddl-auto=none")
562+
.run((context) -> assertThat(tablesFrom(context)).doesNotContain("CITY"));
563+
}
564+
565+
@Test
566+
void whenSpringJpaGenerateDdlIsTrueAndSpringJpaHibernateDdlAutoIsDropThenTableIsNotCreated() {
567+
contextRunner().withPropertyValues("spring.jpa.generate-ddl=true", "spring.jpa.hibernate.ddl-auto=drop")
568+
.run((context) -> assertThat(tablesFrom(context)).doesNotContain("CITY"));
569+
}
570+
571+
@Test
572+
void whenSpringJpaGenerateDdlIsTrueAndJakartaSchemaGenerationIsNoneThenTableIsNotCreated() {
573+
contextRunner()
574+
.withPropertyValues("spring.jpa.generate-ddl=true",
575+
"spring.jpa.properties.jakarta.persistence.schema-generation.database.action=none")
576+
.run((context) -> assertThat(tablesFrom(context)).doesNotContain("CITY"));
577+
}
578+
579+
@Test
580+
void whenSpringJpaGenerateDdlIsTrueSpringJpaHibernateDdlAutoIsCreateAndJakartaSchemaGenerationIsNoneThenTableIsNotCreated() {
581+
contextRunner()
582+
.withPropertyValues("spring.jpa.generate-ddl=true", "spring.jpa.hibernate.ddl-auto=create",
583+
"spring.jpa.properties.jakarta.persistence.schema-generation.database.action=none")
584+
.run((context) -> assertThat(tablesFrom(context)).doesNotContain("CITY"));
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+
515595
@Configuration(proxyBeanMethods = false)
516596
@TestAutoConfigurationPackage(City.class)
517597
@DependsOnDatabaseInitialization

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

+3
Original file line numberDiff line numberDiff line change
@@ -1046,3 +1046,6 @@ features.testing.testcontainers.at-development-time.devtools=features.testcontai
10461046

10471047
# gh-39125
10481048
actuator.observability.common-key-values=actuator.observability.common-tags
1049+
1050+
# gh-40503
1051+
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
@@ -293,7 +293,6 @@ The following line shows an example of setting JPA properties for Hibernate:
293293
The line in the preceding example passes a value of `true` for the `hibernate.globally_quoted_identifiers` property to the Hibernate entity manager.
294294

295295
By default, the DDL execution (or validation) is deferred until the `ApplicationContext` has started.
296-
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.
297296

298297

299298

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 to one of the standard Hibernate property values which 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, while 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)