Skip to content

Commit bb4e559

Browse files
committed
Let Hibernate detect the dialect to use
Closes gh-16172
1 parent 5f6d8e1 commit bb4e559

File tree

6 files changed

+57
-34
lines changed

6 files changed

+57
-34
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/orm/jpa/JpaBaseConfiguration.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,12 @@ public PlatformTransactionManager transactionManager(
105105
public JpaVendorAdapter jpaVendorAdapter() {
106106
AbstractJpaVendorAdapter adapter = createJpaVendorAdapter();
107107
adapter.setShowSql(this.properties.isShowSql());
108-
adapter.setDatabase(this.properties.determineDatabase(this.dataSource));
109-
adapter.setDatabasePlatform(this.properties.getDatabasePlatform());
108+
if (this.properties.getDatabase() != null) {
109+
adapter.setDatabase(this.properties.getDatabase());
110+
}
111+
if (this.properties.getDatabasePlatform() != null) {
112+
adapter.setDatabasePlatform(this.properties.getDatabasePlatform());
113+
}
110114
adapter.setGenerateDdl(this.properties.isGenerateDdl());
111115
return adapter;
112116
}

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/orm/jpa/JpaProperties.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2018 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -134,7 +134,10 @@ public void setOpenInView(Boolean openInView) {
134134
* {@link DataSource}.
135135
* @param dataSource the auto-configured data source
136136
* @return {@code Database}
137+
* @deprecated since 2.2.0 in favor of letting the JPA container detect the database
138+
* to use.
137139
*/
140+
@Deprecated
138141
public Database determineDatabase(DataSource dataSource) {
139142
if (this.database != null) {
140143
return this.database;

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,14 @@ public void hibernatePropertiesCustomizersAreAppliedInOrder() {
110110
}
111111

112112
@Test
113-
public void defaultDatabaseForH2() {
113+
public void defaultDatabaseIsSet() {
114114
this.contextRunner.withPropertyValues("spring.datasource.url:jdbc:h2:mem:testdb",
115115
"spring.datasource.initialization-mode:never").run((context) -> {
116116
HibernateJpaVendorAdapter bean = context
117117
.getBean(HibernateJpaVendorAdapter.class);
118118
Database database = (Database) ReflectionTestUtils.getField(bean,
119119
"database");
120-
assertThat(database).isEqualTo(Database.H2);
120+
assertThat(database).isEqualTo(Database.DEFAULT);
121121
});
122122
}
123123

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

+1-20
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2018 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -44,25 +44,6 @@ public class HibernateDefaultDdlAutoProviderTests {
4444
HibernateJpaAutoConfiguration.class))
4545
.withPropertyValues("spring.datasource.initialization-mode:never");
4646

47-
@Test
48-
public void defaultDdlAutoForMysql() {
49-
// Set up environment so we get a MySQL database but don't require server to be
50-
// running...
51-
this.contextRunner.withPropertyValues(
52-
"spring.datasource.type:"
53-
+ org.apache.tomcat.jdbc.pool.DataSource.class.getName(),
54-
"spring.datasource.database:mysql",
55-
"spring.datasource.url:jdbc:mysql://localhost/nonexistent",
56-
"spring.jpa.database:MYSQL").run((context) -> {
57-
HibernateDefaultDdlAutoProvider ddlAutoProvider = new HibernateDefaultDdlAutoProvider(
58-
Collections.emptyList());
59-
assertThat(ddlAutoProvider
60-
.getDefaultDdlAuto(context.getBean(DataSource.class)))
61-
.isEqualTo("none");
62-
63-
});
64-
}
65-
6647
@Test
6748
public void defaultDDlAutoForEmbedded() {
6849
this.contextRunner.run((context) -> {

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

+42
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.util.Enumeration;
2626
import java.util.List;
2727
import java.util.Map;
28+
import java.util.function.Consumer;
2829

2930
import javax.persistence.EntityManager;
3031
import javax.persistence.EntityManagerFactory;
@@ -37,6 +38,7 @@
3738
import org.hibernate.boot.model.naming.ImplicitNamingStrategy;
3839
import org.hibernate.boot.model.naming.PhysicalNamingStrategy;
3940
import org.hibernate.cfg.AvailableSettings;
41+
import org.hibernate.dialect.H2Dialect;
4042
import org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform;
4143
import org.hibernate.engine.transaction.jta.platform.spi.JtaPlatform;
4244
import org.hibernate.internal.SessionFactoryImpl;
@@ -65,7 +67,9 @@
6567
import org.springframework.context.annotation.Bean;
6668
import org.springframework.context.annotation.Configuration;
6769
import org.springframework.orm.jpa.JpaTransactionManager;
70+
import org.springframework.orm.jpa.JpaVendorAdapter;
6871
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
72+
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
6973
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
7074

7175
import static org.assertj.core.api.Assertions.assertThat;
@@ -155,6 +159,40 @@ public void testLiquibasePlusValidation() {
155159
.run((context) -> assertThat(context).hasNotFailed());
156160
}
157161

162+
@Test
163+
public void hibernateDialectIsNotSetByDefault() {
164+
contextRunner().run(assertJpaVendorAdapter(
165+
(adapter) -> assertThat(adapter.getJpaPropertyMap())
166+
.doesNotContainKeys("hibernate.dialect")));
167+
}
168+
169+
@Test
170+
public void hibernateDialectIsSetWhenDatabaseIsSet() {
171+
contextRunner().withPropertyValues("spring.jpa.database=H2")
172+
.run(assertJpaVendorAdapter(
173+
(adapter) -> assertThat(adapter.getJpaPropertyMap()).contains(
174+
entry("hibernate.dialect", H2Dialect.class.getName()))));
175+
}
176+
177+
@Test
178+
public void hibernateDialectIsSetWhenDatabasePlatformIsSet() {
179+
String databasePlatform = TestH2Dialect.class.getName();
180+
contextRunner()
181+
.withPropertyValues("spring.jpa.database-platform=" + databasePlatform)
182+
.run(assertJpaVendorAdapter(
183+
(adapter) -> assertThat(adapter.getJpaPropertyMap())
184+
.contains(entry("hibernate.dialect", databasePlatform))));
185+
}
186+
187+
private ContextConsumer<AssertableApplicationContext> assertJpaVendorAdapter(
188+
Consumer<HibernateJpaVendorAdapter> adapter) {
189+
return (context) -> {
190+
assertThat(context).hasSingleBean(JpaVendorAdapter.class);
191+
assertThat(context).hasSingleBean(HibernateJpaVendorAdapter.class);
192+
adapter.accept(context.getBean(HibernateJpaVendorAdapter.class));
193+
};
194+
}
195+
158196
@Test
159197
public void jtaDefaultPlatform() {
160198
contextRunner()
@@ -597,4 +635,8 @@ public EntityManagerFactoryBuilderCustomizer asyncBootstrappingCustomizer(
597635

598636
}
599637

638+
public static class TestH2Dialect extends H2Dialect {
639+
640+
}
641+
600642
}

spring-boot-project/spring-boot-docs/src/main/asciidoc/howto.adoc

+2-9
Original file line numberDiff line numberDiff line change
@@ -1926,15 +1926,8 @@ conditions, it has different defaults. If an embedded database is used and no sc
19261926
manager (such as Liquibase or Flyway) is handling the `DataSource`, it defaults to
19271927
`create-drop`. In all other cases, it defaults to `none`.
19281928

1929-
The dialect to use is also automatically detected based on the current `DataSource`, but
1930-
you can set `spring.jpa.database` yourself if you want to be explicit and bypass that
1931-
check on startup.
1932-
1933-
NOTE: Specifying a `database` leads to the configuration of a well-defined Hibernate
1934-
dialect. Several databases have more than one `Dialect`, and this may not suit your needs.
1935-
In that case, you can either set `spring.jpa.database` to `default` to let Hibernate
1936-
figure things out or set the dialect by setting the `spring.jpa.database-platform`
1937-
property.
1929+
The dialect to use is detected by the JPA provider. If you prefer to set the dialect
1930+
yourself, set the `spring.jpa.database-platform` property.
19381931

19391932
The most common options to set are shown in the following example:
19401933

0 commit comments

Comments
 (0)