Raise the minimum supported version of Hibernate to 5.2.x
Closes gh-7586
This commit is contained in:
@@ -1,54 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2016 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.autoconfigure.orm.jpa;
|
||||
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
/**
|
||||
* Supported Hibernate versions.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
enum HibernateVersion {
|
||||
|
||||
/**
|
||||
* Version 4.
|
||||
*/
|
||||
V4,
|
||||
|
||||
/**
|
||||
* Version 5.
|
||||
*/
|
||||
V5;
|
||||
|
||||
private static final String HIBERNATE_5_CLASS = "org.hibernate.boot.model."
|
||||
+ "naming.PhysicalNamingStrategy";
|
||||
|
||||
private static HibernateVersion running;
|
||||
|
||||
public static HibernateVersion getRunning() {
|
||||
if (running == null) {
|
||||
setRunning(ClassUtils.isPresent(HIBERNATE_5_CLASS, null) ? V5 : V4);
|
||||
}
|
||||
return running;
|
||||
}
|
||||
|
||||
static void setRunning(HibernateVersion running) {
|
||||
HibernateVersion.running = running;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -140,8 +140,7 @@ public class JpaProperties {
|
||||
/**
|
||||
* Use Hibernate's newer IdentifierGenerator for AUTO, TABLE and SEQUENCE. This is
|
||||
* actually a shortcut for the "hibernate.id.new_generator_mappings" property.
|
||||
* When not specified will default to "false" with Hibernate 5 for back
|
||||
* compatibility.
|
||||
* When not specified will default to "false" for backwards compatibility.
|
||||
*/
|
||||
private Boolean useNewIdGeneratorMappings;
|
||||
|
||||
@@ -172,7 +171,7 @@ public class JpaProperties {
|
||||
DataSource dataSource) {
|
||||
Map<String, String> result = new HashMap<String, String>(existing);
|
||||
applyNewIdGeneratorMappings(result);
|
||||
getNaming().applyNamingStrategy(result);
|
||||
getNaming().applyNamingStrategies(result);
|
||||
String ddlAuto = getOrDeduceDdlAuto(existing, dataSource);
|
||||
if (StringUtils.hasText(ddlAuto) && !"none".equals(ddlAuto)) {
|
||||
result.put("hibernate.hbm2ddl.auto", ddlAuto);
|
||||
@@ -188,8 +187,7 @@ public class JpaProperties {
|
||||
result.put(USE_NEW_ID_GENERATOR_MAPPINGS,
|
||||
this.useNewIdGeneratorMappings.toString());
|
||||
}
|
||||
else if (HibernateVersion.getRunning() == HibernateVersion.V5
|
||||
&& !result.containsKey(USE_NEW_ID_GENERATOR_MAPPINGS)) {
|
||||
else if (!result.containsKey(USE_NEW_ID_GENERATOR_MAPPINGS)) {
|
||||
result.put(USE_NEW_ID_GENERATOR_MAPPINGS, "false");
|
||||
}
|
||||
}
|
||||
@@ -219,28 +217,20 @@ public class JpaProperties {
|
||||
|
||||
public static class Naming {
|
||||
|
||||
private static final String DEFAULT_HIBERNATE4_STRATEGY = "org.springframework.boot.orm.jpa.hibernate.SpringNamingStrategy";
|
||||
|
||||
private static final String DEFAULT_PHYSICAL_STRATEGY = "org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy";
|
||||
|
||||
private static final String DEFAULT_IMPLICIT_STRATEGY = "org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy";
|
||||
|
||||
/**
|
||||
* Hibernate 5 implicit naming strategy fully qualified name.
|
||||
* Fully qualfied name of the implicit naming strategy.
|
||||
*/
|
||||
private String implicitStrategy;
|
||||
|
||||
/**
|
||||
* Hibernate 5 physical naming strategy fully qualified name.
|
||||
* Fully qualified name of the physical naming strategy.
|
||||
*/
|
||||
private String physicalStrategy;
|
||||
|
||||
/**
|
||||
* Hibernate 4 naming strategy fully qualified name. Not supported with Hibernate
|
||||
* 5.
|
||||
*/
|
||||
private String strategy;
|
||||
|
||||
public String getImplicitStrategy() {
|
||||
return this.implicitStrategy;
|
||||
}
|
||||
@@ -257,36 +247,15 @@ public class JpaProperties {
|
||||
this.physicalStrategy = physicalStrategy;
|
||||
}
|
||||
|
||||
public String getStrategy() {
|
||||
return this.strategy;
|
||||
private void applyNamingStrategies(Map<String, String> properties) {
|
||||
applyNamingStrategy(properties, "hibernate.implicit_naming_strategy",
|
||||
this.implicitStrategy, DEFAULT_IMPLICIT_STRATEGY);
|
||||
applyNamingStrategy(properties, "hibernate.physical_naming_strategy",
|
||||
this.physicalStrategy, DEFAULT_PHYSICAL_STRATEGY);
|
||||
}
|
||||
|
||||
public void setStrategy(String strategy) {
|
||||
this.strategy = strategy;
|
||||
}
|
||||
|
||||
private void applyNamingStrategy(Map<String, String> properties) {
|
||||
switch (HibernateVersion.getRunning()) {
|
||||
case V4:
|
||||
applyHibernate4NamingStrategy(properties);
|
||||
break;
|
||||
case V5:
|
||||
applyHibernate5NamingStrategy(properties);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void applyHibernate5NamingStrategy(Map<String, String> properties) {
|
||||
applyHibernate5NamingStrategy(properties,
|
||||
"hibernate.implicit_naming_strategy", this.implicitStrategy,
|
||||
DEFAULT_IMPLICIT_STRATEGY);
|
||||
applyHibernate5NamingStrategy(properties,
|
||||
"hibernate.physical_naming_strategy", this.physicalStrategy,
|
||||
DEFAULT_PHYSICAL_STRATEGY);
|
||||
}
|
||||
|
||||
private void applyHibernate5NamingStrategy(Map<String, String> properties,
|
||||
String key, String strategy, String defaultStrategy) {
|
||||
private void applyNamingStrategy(Map<String, String> properties, String key,
|
||||
String strategy, String defaultStrategy) {
|
||||
if (strategy != null) {
|
||||
properties.put(key, strategy);
|
||||
}
|
||||
@@ -295,21 +264,6 @@ public class JpaProperties {
|
||||
}
|
||||
}
|
||||
|
||||
private void applyHibernate4NamingStrategy(Map<String, String> properties) {
|
||||
if (!properties.containsKey("hibernate.ejb.naming_strategy_delegator")) {
|
||||
properties.put("hibernate.ejb.naming_strategy",
|
||||
getHibernate4NamingStrategy(properties));
|
||||
}
|
||||
}
|
||||
|
||||
private String getHibernate4NamingStrategy(Map<String, String> existing) {
|
||||
if (!existing.containsKey("hibernate.ejb.naming_strategy")
|
||||
&& this.strategy != null) {
|
||||
return this.strategy;
|
||||
}
|
||||
return DEFAULT_HIBERNATE4_STRATEGY;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -26,7 +26,6 @@ import javax.transaction.TransactionManager;
|
||||
import javax.transaction.UserTransaction;
|
||||
|
||||
import org.hibernate.engine.transaction.jta.platform.spi.JtaPlatform;
|
||||
import org.junit.After;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
@@ -41,6 +40,7 @@ import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* Tests for {@link HibernateJpaAutoConfiguration}.
|
||||
@@ -55,11 +55,6 @@ public class HibernateJpaAutoConfigurationTests
|
||||
@Rule
|
||||
public ExpectedException thrown = ExpectedException.none();
|
||||
|
||||
@After
|
||||
public void cleanup() {
|
||||
HibernateVersion.setRunning(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Class<?> getAutoConfigureClass() {
|
||||
return HibernateJpaAutoConfiguration.class;
|
||||
@@ -90,38 +85,6 @@ public class HibernateJpaAutoConfigurationTests
|
||||
.queryForObject("SELECT COUNT(*) from CITY", Integer.class)).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCustomNamingStrategy() throws Exception {
|
||||
HibernateVersion.setRunning(HibernateVersion.V4);
|
||||
EnvironmentTestUtils.addEnvironment(this.context,
|
||||
"spring.jpa.hibernate.naming.strategy:"
|
||||
+ "org.hibernate.cfg.EJB3NamingStrategy");
|
||||
setupTestConfiguration();
|
||||
this.context.refresh();
|
||||
LocalContainerEntityManagerFactoryBean bean = this.context
|
||||
.getBean(LocalContainerEntityManagerFactoryBean.class);
|
||||
String actual = (String) bean.getJpaPropertyMap()
|
||||
.get("hibernate.ejb.naming_strategy");
|
||||
assertThat(actual).isEqualTo("org.hibernate.cfg.EJB3NamingStrategy");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCustomNamingStrategyViaJpaProperties() throws Exception {
|
||||
HibernateVersion.setRunning(HibernateVersion.V4);
|
||||
EnvironmentTestUtils.addEnvironment(this.context,
|
||||
"spring.jpa.properties.hibernate.ejb.naming_strategy:"
|
||||
+ "org.hibernate.cfg.EJB3NamingStrategy");
|
||||
setupTestConfiguration();
|
||||
this.context.refresh();
|
||||
LocalContainerEntityManagerFactoryBean bean = this.context
|
||||
.getBean(LocalContainerEntityManagerFactoryBean.class);
|
||||
String actual = (String) bean.getJpaPropertyMap()
|
||||
.get("hibernate.ejb.naming_strategy");
|
||||
// You can't override this one from spring.jpa.properties because it has an
|
||||
// opinionated default
|
||||
assertThat(actual).isNotEqualTo("org.hibernate.cfg.EJB3NamingStrategy");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFlywayPlusValidation() throws Exception {
|
||||
EnvironmentTestUtils.addEnvironment(this.context,
|
||||
@@ -175,7 +138,7 @@ public class HibernateJpaAutoConfigurationTests
|
||||
|
||||
@Override
|
||||
public TransactionManager retrieveTransactionManager() {
|
||||
throw new UnsupportedOperationException();
|
||||
return mock(TransactionManager.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -27,7 +27,6 @@ import org.junit.Test;
|
||||
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy;
|
||||
import org.springframework.boot.orm.jpa.hibernate.SpringNamingStrategy;
|
||||
import org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy;
|
||||
import org.springframework.boot.test.util.EnvironmentTestUtils;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
@@ -49,54 +48,14 @@ public class JpaPropertiesTests {
|
||||
|
||||
@After
|
||||
public void close() {
|
||||
HibernateVersion.setRunning(null);
|
||||
if (this.context != null) {
|
||||
this.context.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void hibernate4NoCustomNamingStrategy() throws Exception {
|
||||
JpaProperties properties = load(HibernateVersion.V4);
|
||||
Map<String, String> hibernateProperties = properties
|
||||
.getHibernateProperties(mockStandaloneDataSource());
|
||||
assertThat(hibernateProperties).contains(entry("hibernate.ejb.naming_strategy",
|
||||
SpringNamingStrategy.class.getName()));
|
||||
assertThat(hibernateProperties).doesNotContainKeys(
|
||||
"hibernate.implicit_naming_strategy",
|
||||
"hibernate.physical_naming_strategy");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void hibernate4CustomNamingStrategy() throws Exception {
|
||||
JpaProperties properties = load(HibernateVersion.V4,
|
||||
"spring.jpa.hibernate.naming.strategy:"
|
||||
+ "org.hibernate.cfg.EJB3NamingStrategy");
|
||||
Map<String, String> hibernateProperties = properties
|
||||
.getHibernateProperties(mockStandaloneDataSource());
|
||||
assertThat(hibernateProperties).contains(entry("hibernate.ejb.naming_strategy",
|
||||
"org.hibernate.cfg.EJB3NamingStrategy"));
|
||||
assertThat(hibernateProperties).doesNotContainKeys(
|
||||
"hibernate.implicit_naming_strategy",
|
||||
"hibernate.physical_naming_strategy");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void hibernate4CustomNamingStrategyViaJpaProperties() throws Exception {
|
||||
JpaProperties properties = load(HibernateVersion.V4,
|
||||
"spring.jpa.properties.hibernate.ejb.naming_strategy:"
|
||||
+ "org.hibernate.cfg.EJB3NamingStrategy");
|
||||
Map<String, String> hibernateProperties = properties
|
||||
.getHibernateProperties(mockStandaloneDataSource());
|
||||
String actual = hibernateProperties.get("hibernate.ejb.naming_strategy");
|
||||
// You can't override this one from spring.jpa.properties because it has an
|
||||
// opinionated default
|
||||
assertThat(actual).isNotEqualTo("org.hibernate.cfg.EJB3NamingStrategy");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void hibernate5NoCustomNamingStrategy() throws Exception {
|
||||
JpaProperties properties = load(HibernateVersion.V5);
|
||||
public void noCustomNamingStrategy() throws Exception {
|
||||
JpaProperties properties = load();
|
||||
Map<String, String> hibernateProperties = properties
|
||||
.getHibernateProperties(mockStandaloneDataSource());
|
||||
assertThat(hibernateProperties)
|
||||
@@ -111,7 +70,7 @@ public class JpaPropertiesTests {
|
||||
|
||||
@Test
|
||||
public void hibernate5CustomNamingStrategies() throws Exception {
|
||||
JpaProperties properties = load(HibernateVersion.V5,
|
||||
JpaProperties properties = load(
|
||||
"spring.jpa.hibernate.naming.implicit-strategy:com.example.Implicit",
|
||||
"spring.jpa.hibernate.naming.physical-strategy:com.example.Physical");
|
||||
Map<String, String> hibernateProperties = properties
|
||||
@@ -125,7 +84,7 @@ public class JpaPropertiesTests {
|
||||
|
||||
@Test
|
||||
public void hibernate5CustomNamingStrategiesViaJpaProperties() throws Exception {
|
||||
JpaProperties properties = load(HibernateVersion.V5,
|
||||
JpaProperties properties = load(
|
||||
"spring.jpa.properties.hibernate.implicit_naming_strategy:com.example.Implicit",
|
||||
"spring.jpa.properties.hibernate.physical_naming_strategy:com.example.Physical");
|
||||
Map<String, String> hibernateProperties = properties
|
||||
@@ -139,17 +98,8 @@ public class JpaPropertiesTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void useNewIdGeneratorMappingsDefaultHibernate4() throws Exception {
|
||||
JpaProperties properties = load(HibernateVersion.V4);
|
||||
Map<String, String> hibernateProperties = properties
|
||||
.getHibernateProperties(mockStandaloneDataSource());
|
||||
assertThat(hibernateProperties)
|
||||
.doesNotContainKey(AvailableSettings.USE_NEW_ID_GENERATOR_MAPPINGS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void useNewIdGeneratorMappingsDefaultHibernate5() throws Exception {
|
||||
JpaProperties properties = load(HibernateVersion.V5);
|
||||
public void useNewIdGeneratorMappingsDefault() throws Exception {
|
||||
JpaProperties properties = load();
|
||||
Map<String, String> hibernateProperties = properties
|
||||
.getHibernateProperties(mockStandaloneDataSource());
|
||||
assertThat(hibernateProperties)
|
||||
@@ -158,7 +108,7 @@ public class JpaPropertiesTests {
|
||||
|
||||
@Test
|
||||
public void useNewIdGeneratorMappingsTrue() throws Exception {
|
||||
JpaProperties properties = load(HibernateVersion.V5,
|
||||
JpaProperties properties = load(
|
||||
"spring.jpa.hibernate.use-new-id-generator-mappings:true");
|
||||
Map<String, String> hibernateProperties = properties
|
||||
.getHibernateProperties(mockStandaloneDataSource());
|
||||
@@ -173,8 +123,7 @@ public class JpaPropertiesTests {
|
||||
return ds;
|
||||
}
|
||||
|
||||
private JpaProperties load(HibernateVersion hibernateVersion, String... environment) {
|
||||
HibernateVersion.setRunning(hibernateVersion);
|
||||
private JpaProperties load(String... environment) {
|
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
||||
EnvironmentTestUtils.addEnvironment(ctx, environment);
|
||||
ctx.register(TestConfiguration.class);
|
||||
|
||||
@@ -34,7 +34,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
@RunWith(ModifiedClassPathRunner.class)
|
||||
@ClassPathExclusions("tomcat-embed-el-*.jar")
|
||||
@ClassPathExclusions({ "tomcat-embed-el-*.jar", "el-api-*.jar" })
|
||||
public class ValidationAutoConfigurationWithHibernateValidatorMissingElImplTests {
|
||||
|
||||
private AnnotationConfigApplicationContext context;
|
||||
|
||||
@@ -3,6 +3,8 @@ databaseChangeLog:
|
||||
id: 1
|
||||
author: dsyer
|
||||
changes:
|
||||
- createSequence:
|
||||
sequenceName: hibernate_sequence
|
||||
- createTable:
|
||||
tableName: city
|
||||
columns:
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
CREATE SEQUENCE HIBERNATE_SEQUENCE;
|
||||
|
||||
CREATE TABLE CITY (
|
||||
id BIGINT GENERATED BY DEFAULT AS IDENTITY,
|
||||
name VARCHAR(30),
|
||||
state VARCHAR(30),
|
||||
country VARCHAR(30),
|
||||
map VARCHAR(30)
|
||||
);
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user