|
46 | 46 | import org.hibernate.engine.transaction.jta.platform.spi.JtaPlatform;
|
47 | 47 | import org.hibernate.internal.SessionFactoryImpl;
|
48 | 48 | import org.hibernate.jpa.HibernatePersistenceProvider;
|
| 49 | +import org.junit.jupiter.api.Disabled; |
49 | 50 | import org.junit.jupiter.api.Test;
|
50 | 51 |
|
51 | 52 | import org.springframework.aot.hint.MemberCategory;
|
|
75 | 76 | import org.springframework.context.annotation.Bean;
|
76 | 77 | import org.springframework.context.annotation.Configuration;
|
77 | 78 | import org.springframework.context.event.ContextRefreshedEvent;
|
| 79 | +import org.springframework.jdbc.core.JdbcTemplate; |
78 | 80 | import org.springframework.orm.jpa.JpaTransactionManager;
|
79 | 81 | import org.springframework.orm.jpa.JpaVendorAdapter;
|
80 | 82 | import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
@@ -508,6 +510,88 @@ void registersHintsForNamingClasses() {
|
508 | 510 | }
|
509 | 511 | }
|
510 | 512 |
|
| 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 | + |
511 | 595 | @Configuration(proxyBeanMethods = false)
|
512 | 596 | @TestAutoConfigurationPackage(City.class)
|
513 | 597 | @DependsOnDatabaseInitialization
|
|
0 commit comments