Allow hibernate naming strategy to be specified

Issue: #53028397
pull/50/head
Phillip Webb 11 years ago
parent 9d9b005c9f
commit 94b182cc81

@ -63,6 +63,9 @@ public class HibernateJpaAutoConfiguration extends JpaBaseConfiguration {
@Value("${spring.jpa.ddlAuto:${spring.jpa.ddl_auto:none}}")
private String ddlAuto; // e.g. none, validate, update, create, create-drop
@Value("${spring.jpa.hibernate.namingstrategy:org.hibernate.cfg.ImprovedNamingStrategy}")
private String namingStrategy;
@Bean
@Override
public JpaVendorAdapter jpaVendorAdapter() {
@ -81,8 +84,13 @@ public class HibernateJpaAutoConfiguration extends JpaBaseConfiguration {
Map<String, Object> properties = entityManagerFactoryBean.getJpaPropertyMap();
properties.put("hibernate.cache.provider_class",
"org.hibernate.cache.HashtableCacheProvider");
properties.put("hibernate.ejb.naming_strategy",
ImprovedNamingStrategy.class.getName());
if (StringUtils.hasLength(this.namingStrategy)) {
properties.put("hibernate.ejb.naming_strategy", this.namingStrategy);
}
else {
properties.put("hibernate.ejb.naming_strategy",
ImprovedNamingStrategy.class.getName());
}
if (StringUtils.hasLength(this.ddlAuto) && !"none".equals(this.ddlAuto)) {
properties.put("hibernate.hbm2ddl.auto", this.ddlAuto);
}

@ -25,7 +25,6 @@ import org.springframework.boot.autoconfigure.ComponentScanDetectorConfiguration
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDatabaseConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.test.City;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
@ -33,12 +32,15 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter;
import org.springframework.orm.jpa.support.OpenEntityManagerInViewInterceptor;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
/**
@ -118,6 +120,25 @@ public class HibernateJpaAutoConfigurationTests {
assertEquals(0, getInterceptorBeans().length);
}
@Test
public void testCustomNamingStrategy() throws Exception {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
TestUtils
.addEnviroment(context,
"spring.jpa.hibernate.namingstrategy:org.hibernate.cfg.EJB3NamingStrategy");
context.register(TestConfiguration.class,
ComponentScanDetectorConfiguration.class,
EmbeddedDatabaseConfiguration.class, HibernateJpaAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
this.context = context;
this.context.refresh();
LocalContainerEntityManagerFactoryBean bean = this.context
.getBean(LocalContainerEntityManagerFactoryBean.class);
String actual = (String) bean.getJpaPropertyMap().get(
"hibernate.ejb.naming_strategy");
assertThat(actual, equalTo("org.hibernate.cfg.EJB3NamingStrategy"));
}
private String[] getInterceptorBeans() {
return this.context.getBeanNamesForType(OpenEntityManagerInViewInterceptor.class);
}

Loading…
Cancel
Save