Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct document about spring.jpa.generate-ddl and add tests #40185

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -37,9 +37,11 @@
import jakarta.transaction.Transaction;
import jakarta.transaction.TransactionManager;
import jakarta.transaction.UserTransaction;
import org.h2.tools.DeleteDbFiles;
import org.hibernate.boot.model.naming.CamelCaseToUnderscoresNamingStrategy;
import org.hibernate.boot.model.naming.ImplicitNamingStrategy;
import org.hibernate.boot.model.naming.PhysicalNamingStrategy;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.ManagedBeanSettings;
import org.hibernate.cfg.SchemaToolingSettings;
import org.hibernate.dialect.H2Dialect;
Expand Down Expand Up @@ -76,6 +78,7 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.orm.jpa.AbstractEntityManagerFactoryBean;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
Expand All @@ -98,6 +101,7 @@
* @author Stephane Nicoll
* @author Chris Bono
* @author Moritz Halbritter
* @author Yanming Zhou
*/
class HibernateJpaAutoConfigurationTests extends AbstractJpaAutoConfigurationTests {

Expand Down Expand Up @@ -182,6 +186,46 @@ void hibernateDialectIsSetWhenDatabasePlatformIsSet() {
.contains(entry("hibernate.dialect", databasePlatform))));
}

@Test
void jpaGenerateDdlIsUsedIfHibernateHbm2ddlAutoIsNotSet() {
try {
contextRunner().withPropertyValues("spring.jpa.generate-ddl=true", "spring.datasource.url=jdbc:h2:./test")
// simulate non-embedded database
// see HibernateDefaultDdlAutoProvider.getDefaultDdlAuto(DataSource)
.run(assertEntityManagerFactoryBean((adapter) -> assertThat(adapter.getJpaPropertyMap())
.containsEntry(AvailableSettings.HBM2DDL_AUTO, "update")));
}
finally {
DeleteDbFiles.execute(".", "test", true);
}
}

@Test
void jpaGenerateDdlIsNotUsedIfHibernateHbm2ddlAutoIsSetToNone() {
contextRunner()
.withPropertyValues("spring.jpa.generate-ddl=true", "spring.jpa.properties.hibernate.hbm2ddl.auto=none")
.run(assertEntityManagerFactoryBean((adapter) -> assertThat(adapter.getJpaPropertyMap())
.containsEntry(AvailableSettings.HBM2DDL_AUTO, "none")));
}

@Test
void jpaGenerateDdlIsNotUsedIfHibernateHbm2ddlAutoIsSetToOtherThanNone() {
contextRunner()
.withPropertyValues("spring.jpa.generate-ddl=true", "spring.jpa.properties.hibernate.hbm2ddl.auto=create")
.run(assertEntityManagerFactoryBean((adapter) -> assertThat(adapter.getJpaPropertyMap())
.containsEntry(AvailableSettings.HBM2DDL_AUTO, "create")));
}

private ContextConsumer<AssertableApplicationContext> assertEntityManagerFactoryBean(
Consumer<AbstractEntityManagerFactoryBean> adapter) {
return (context) -> {
assertThat(context).hasSingleBean(JpaVendorAdapter.class);
assertThat(context).hasSingleBean(HibernateJpaVendorAdapter.class);
assertThat(context).hasSingleBean(AbstractEntityManagerFactoryBean.class);
adapter.accept(context.getBean(AbstractEntityManagerFactoryBean.class));
};
}

private ContextConsumer<AssertableApplicationContext> assertJpaVendorAdapter(
Consumer<HibernateJpaVendorAdapter> adapter) {
return (context) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ spring:
The line in the preceding example passes a value of `true` for the `hibernate.globally_quoted_identifiers` property to the Hibernate entity manager.

By default, the DDL execution (or validation) is deferred until the `ApplicationContext` has started.
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.
There is also a `spring.jpa.generate-ddl` flag, but it is not used if `spring.jpa.hibernate.ddl-auto` or `spring.jpa.properties.hibernate.hbm2ddl.auto` is present, because they are more fine-grained.



Expand Down