Commit b0579c1c authored by Dave Syer's avatar Dave Syer

Ensure JPA vendor properties are period separated

If you bind to Map<String,Object> you get a nested Map instead
of period-separated keys. This change just makes JpaProperties
expose a Map<String,String> so the keys are sane.

Fixes gh-988
parent 2f653f06
...@@ -137,7 +137,7 @@ public class EntityManagerFactoryBuilder { ...@@ -137,7 +137,7 @@ public class EntityManagerFactoryBuilder {
* @param properties the properties to use * @param properties the properties to use
* @return the builder for fluent usage * @return the builder for fluent usage
*/ */
public Builder properties(Map<String, Object> properties) { public Builder properties(Map<String, String> properties) {
this.properties.putAll(properties); this.properties.putAll(properties);
return this; return this;
} }
......
...@@ -71,13 +71,13 @@ public class HibernateJpaAutoConfiguration extends JpaBaseConfiguration { ...@@ -71,13 +71,13 @@ public class HibernateJpaAutoConfiguration extends JpaBaseConfiguration {
} }
@Override @Override
protected Map<String, Object> getVendorProperties() { protected Map<String, String> getVendorProperties() {
return this.properties.getInitialHibernateProperties(this.dataSource); return this.properties.getInitialHibernateProperties(this.dataSource);
} }
@Override @Override
protected EntityManagerFactoryBeanCallback getVendorCallback() { protected EntityManagerFactoryBeanCallback getVendorCallback() {
final Map<String, Object> map = this.properties final Map<String, String> map = this.properties
.getHibernateProperties(this.dataSource); .getHibernateProperties(this.dataSource);
return new EntityManagerFactoryBeanCallback() { return new EntityManagerFactoryBeanCallback() {
@Override @Override
...@@ -93,18 +93,18 @@ public class HibernateJpaAutoConfiguration extends JpaBaseConfiguration { ...@@ -93,18 +93,18 @@ public class HibernateJpaAutoConfiguration extends JpaBaseConfiguration {
private static class DeferredSchemaAction implements private static class DeferredSchemaAction implements
ApplicationListener<ContextRefreshedEvent> { ApplicationListener<ContextRefreshedEvent> {
private Map<String, Object> map; private Map<String, String> map;
private LocalContainerEntityManagerFactoryBean factory; private LocalContainerEntityManagerFactoryBean factory;
public DeferredSchemaAction(LocalContainerEntityManagerFactoryBean factory, public DeferredSchemaAction(LocalContainerEntityManagerFactoryBean factory,
Map<String, Object> map) { Map<String, String> map) {
this.factory = factory; this.factory = factory;
this.map = map; this.map = map;
} }
@Override @Override
public void onApplicationEvent(ContextRefreshedEvent event) { public void onApplicationEvent(ContextRefreshedEvent event) {
String ddlAuto = (String) this.map.get("hibernate.hbm2ddl.auto"); String ddlAuto = this.map.get("hibernate.hbm2ddl.auto");
if (ddlAuto == null || "none".equals(ddlAuto)) { if (ddlAuto == null || "none".equals(ddlAuto)) {
return; return;
} }
......
...@@ -105,7 +105,7 @@ public abstract class JpaBaseConfiguration implements BeanFactoryAware { ...@@ -105,7 +105,7 @@ public abstract class JpaBaseConfiguration implements BeanFactoryAware {
protected abstract AbstractJpaVendorAdapter createJpaVendorAdapter(); protected abstract AbstractJpaVendorAdapter createJpaVendorAdapter();
protected abstract Map<String, Object> getVendorProperties(); protected abstract Map<String, String> getVendorProperties();
protected abstract EntityManagerFactoryBuilder.EntityManagerFactoryBeanCallback getVendorCallback(); protected abstract EntityManagerFactoryBuilder.EntityManagerFactoryBeanCallback getVendorCallback();
......
...@@ -40,7 +40,7 @@ public class JpaProperties { ...@@ -40,7 +40,7 @@ public class JpaProperties {
private static final Log logger = LogFactory.getLog(JpaProperties.class); private static final Log logger = LogFactory.getLog(JpaProperties.class);
private Map<String, Object> properties = new HashMap<String, Object>(); private Map<String, String> properties = new HashMap<String, String>();
private String databasePlatform; private String databasePlatform;
...@@ -52,11 +52,11 @@ public class JpaProperties { ...@@ -52,11 +52,11 @@ public class JpaProperties {
private Hibernate hibernate = new Hibernate(); private Hibernate hibernate = new Hibernate();
public Map<String, Object> getProperties() { public Map<String, String> getProperties() {
return this.properties; return this.properties;
} }
public void setProperties(Map<String, Object> properties) { public void setProperties(Map<String, String> properties) {
this.properties = properties; this.properties = properties;
} }
...@@ -107,7 +107,7 @@ public class JpaProperties { ...@@ -107,7 +107,7 @@ public class JpaProperties {
* @param dataSource the DataSource in case it is needed to determine the properties * @param dataSource the DataSource in case it is needed to determine the properties
* @return some Hibernate properties for configuration * @return some Hibernate properties for configuration
*/ */
public Map<String, Object> getInitialHibernateProperties(DataSource dataSource) { public Map<String, String> getInitialHibernateProperties(DataSource dataSource) {
return this.hibernate.getAdditionalProperties(this.properties); return this.hibernate.getAdditionalProperties(this.properties);
} }
...@@ -116,7 +116,7 @@ public class JpaProperties { ...@@ -116,7 +116,7 @@ public class JpaProperties {
* @param dataSource the DataSource in case it is needed to determine the properties * @param dataSource the DataSource in case it is needed to determine the properties
* @return some Hibernate properties for configuration * @return some Hibernate properties for configuration
*/ */
public Map<String, Object> getHibernateProperties(DataSource dataSource) { public Map<String, String> getHibernateProperties(DataSource dataSource) {
return this.hibernate return this.hibernate
.getDeferredAdditionalProperties(this.properties, dataSource); .getDeferredAdditionalProperties(this.properties, dataSource);
} }
...@@ -158,7 +158,7 @@ public class JpaProperties { ...@@ -158,7 +158,7 @@ public class JpaProperties {
return this.deferDdl; return this.deferDdl;
} }
private String getDeferredDdlAuto(Map<String, Object> existing, private String getDeferredDdlAuto(Map<String, String> existing,
DataSource dataSource) { DataSource dataSource) {
if (!this.deferDdl) { if (!this.deferDdl) {
return "none"; return "none";
...@@ -169,7 +169,7 @@ public class JpaProperties { ...@@ -169,7 +169,7 @@ public class JpaProperties {
return ddlAuto; return ddlAuto;
} }
if (isAlreadyProvided(existing, "hbm2ddl.auto")) { if (isAlreadyProvided(existing, "hbm2ddl.auto")) {
return (String) existing.get("hibernate.hbm2ddl.auto"); return existing.get("hibernate.hbm2ddl.auto");
} }
return "none"; return "none";
} }
...@@ -178,16 +178,16 @@ public class JpaProperties { ...@@ -178,16 +178,16 @@ public class JpaProperties {
this.ddlAuto = ddlAuto; this.ddlAuto = ddlAuto;
} }
private Map<String, Object> getDeferredAdditionalProperties( private Map<String, String> getDeferredAdditionalProperties(
Map<String, Object> properties, DataSource dataSource) { Map<String, String> properties, DataSource dataSource) {
Map<String, Object> deferred = getAdditionalProperties(properties); Map<String, String> deferred = getAdditionalProperties(properties);
deferred.put("hibernate.hbm2ddl.auto", deferred.put("hibernate.hbm2ddl.auto",
getDeferredDdlAuto(properties, dataSource)); getDeferredDdlAuto(properties, dataSource));
return deferred; return deferred;
} }
private Map<String, Object> getAdditionalProperties(Map<String, Object> existing) { private Map<String, String> getAdditionalProperties(Map<String, String> existing) {
Map<String, Object> result = new HashMap<String, Object>(); Map<String, String> result = new HashMap<String, String>();
if (!isAlreadyProvided(existing, "ejb.naming_strategy") if (!isAlreadyProvided(existing, "ejb.naming_strategy")
&& this.namingStrategy != null) { && this.namingStrategy != null) {
result.put("hibernate.ejb.naming_strategy", this.namingStrategy.getName()); result.put("hibernate.ejb.naming_strategy", this.namingStrategy.getName());
...@@ -205,7 +205,7 @@ public class JpaProperties { ...@@ -205,7 +205,7 @@ public class JpaProperties {
return result; return result;
} }
private boolean isAlreadyProvided(Map<String, Object> existing, String key) { private boolean isAlreadyProvided(Map<String, String> existing, String key) {
return existing.containsKey("hibernate." + key); return existing.containsKey("hibernate." + key);
} }
......
...@@ -139,7 +139,7 @@ public abstract class AbstractJpaAutoConfigurationTests { ...@@ -139,7 +139,7 @@ public abstract class AbstractJpaAutoConfigurationTests {
@Test @Test
public void customJpaProperties() throws Exception { public void customJpaProperties() throws Exception {
EnvironmentTestUtils.addEnvironment(this.context, "spring.jpa.properties.a:b", EnvironmentTestUtils.addEnvironment(this.context, "spring.jpa.properties.a:b",
"spring.jpa.properties.c:d"); "spring.jpa.properties.a.b:c", "spring.jpa.properties.c:d");
setupTestConfiguration(); setupTestConfiguration();
this.context.refresh(); this.context.refresh();
LocalContainerEntityManagerFactoryBean bean = this.context LocalContainerEntityManagerFactoryBean bean = this.context
...@@ -147,6 +147,7 @@ public abstract class AbstractJpaAutoConfigurationTests { ...@@ -147,6 +147,7 @@ public abstract class AbstractJpaAutoConfigurationTests {
Map<String, Object> map = bean.getJpaPropertyMap(); Map<String, Object> map = bean.getJpaPropertyMap();
assertThat(map.get("a"), equalTo((Object) "b")); assertThat(map.get("a"), equalTo((Object) "b"));
assertThat(map.get("c"), equalTo((Object) "d")); assertThat(map.get("c"), equalTo((Object) "d"));
assertThat(map.get("a.b"), equalTo((Object) "c"));
} }
@Test @Test
......
...@@ -61,7 +61,7 @@ public class CustomHibernateJpaAutoConfigurationTests { ...@@ -61,7 +61,7 @@ public class CustomHibernateJpaAutoConfigurationTests {
this.context.refresh(); this.context.refresh();
JpaProperties bean = this.context.getBean(JpaProperties.class); JpaProperties bean = this.context.getBean(JpaProperties.class);
DataSource dataSource = this.context.getBean(DataSource.class); DataSource dataSource = this.context.getBean(DataSource.class);
String actual = (String) bean.getHibernateProperties(dataSource).get( String actual = bean.getHibernateProperties(dataSource).get(
"hibernate.hbm2ddl.auto"); "hibernate.hbm2ddl.auto");
// Default is generic and safe // Default is generic and safe
assertThat(actual, equalTo("none")); assertThat(actual, equalTo("none"));
...@@ -78,7 +78,7 @@ public class CustomHibernateJpaAutoConfigurationTests { ...@@ -78,7 +78,7 @@ public class CustomHibernateJpaAutoConfigurationTests {
this.context.refresh(); this.context.refresh();
JpaProperties bean = this.context.getBean(JpaProperties.class); JpaProperties bean = this.context.getBean(JpaProperties.class);
DataSource dataSource = this.context.getBean(DataSource.class); DataSource dataSource = this.context.getBean(DataSource.class);
String actual = (String) bean.getHibernateProperties(dataSource).get( String actual = bean.getHibernateProperties(dataSource).get(
"hibernate.hbm2ddl.auto"); "hibernate.hbm2ddl.auto");
assertThat(actual, equalTo("create-drop")); assertThat(actual, equalTo("create-drop"));
} }
......
...@@ -47,7 +47,7 @@ public class EntityManagerFactoryBuilderTests { ...@@ -47,7 +47,7 @@ public class EntityManagerFactoryBuilderTests {
new HibernateJpaVendorAdapter(), this.properties, null); new HibernateJpaVendorAdapter(), this.properties, null);
LocalContainerEntityManagerFactoryBean result1 = factory LocalContainerEntityManagerFactoryBean result1 = factory
.dataSource(this.dataSource1) .dataSource(this.dataSource1)
.properties(Collections.singletonMap("foo", (Object) "spam")).build(); .properties(Collections.singletonMap("foo", "spam")).build();
assertFalse(result1.getJpaPropertyMap().isEmpty()); assertFalse(result1.getJpaPropertyMap().isEmpty());
assertTrue(this.properties.getProperties().isEmpty()); assertTrue(this.properties.getProperties().isEmpty());
} }
...@@ -58,7 +58,7 @@ public class EntityManagerFactoryBuilderTests { ...@@ -58,7 +58,7 @@ public class EntityManagerFactoryBuilderTests {
new HibernateJpaVendorAdapter(), this.properties, null); new HibernateJpaVendorAdapter(), this.properties, null);
LocalContainerEntityManagerFactoryBean result1 = factory LocalContainerEntityManagerFactoryBean result1 = factory
.dataSource(this.dataSource1) .dataSource(this.dataSource1)
.properties(Collections.singletonMap("foo", (Object) "spam")).build(); .properties(Collections.singletonMap("foo", "spam")).build();
assertFalse(result1.getJpaPropertyMap().isEmpty()); assertFalse(result1.getJpaPropertyMap().isEmpty());
LocalContainerEntityManagerFactoryBean result2 = factory.dataSource( LocalContainerEntityManagerFactoryBean result2 = factory.dataSource(
this.dataSource2).build(); this.dataSource2).build();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment