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
pull/1016/head
Dave Syer 11 years ago
parent 2f653f0616
commit b0579c1cf3

@ -137,7 +137,7 @@ public class EntityManagerFactoryBuilder {
* @param properties the properties to use
* @return the builder for fluent usage
*/
public Builder properties(Map<String, Object> properties) {
public Builder properties(Map<String, String> properties) {
this.properties.putAll(properties);
return this;
}

@ -71,13 +71,13 @@ public class HibernateJpaAutoConfiguration extends JpaBaseConfiguration {
}
@Override
protected Map<String, Object> getVendorProperties() {
protected Map<String, String> getVendorProperties() {
return this.properties.getInitialHibernateProperties(this.dataSource);
}
@Override
protected EntityManagerFactoryBeanCallback getVendorCallback() {
final Map<String, Object> map = this.properties
final Map<String, String> map = this.properties
.getHibernateProperties(this.dataSource);
return new EntityManagerFactoryBeanCallback() {
@Override
@ -93,18 +93,18 @@ public class HibernateJpaAutoConfiguration extends JpaBaseConfiguration {
private static class DeferredSchemaAction implements
ApplicationListener<ContextRefreshedEvent> {
private Map<String, Object> map;
private Map<String, String> map;
private LocalContainerEntityManagerFactoryBean factory;
public DeferredSchemaAction(LocalContainerEntityManagerFactoryBean factory,
Map<String, Object> map) {
Map<String, String> map) {
this.factory = factory;
this.map = map;
}
@Override
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)) {
return;
}

@ -105,7 +105,7 @@ public abstract class JpaBaseConfiguration implements BeanFactoryAware {
protected abstract AbstractJpaVendorAdapter createJpaVendorAdapter();
protected abstract Map<String, Object> getVendorProperties();
protected abstract Map<String, String> getVendorProperties();
protected abstract EntityManagerFactoryBuilder.EntityManagerFactoryBeanCallback getVendorCallback();

@ -40,7 +40,7 @@ public class JpaProperties {
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;
@ -52,11 +52,11 @@ public class JpaProperties {
private Hibernate hibernate = new Hibernate();
public Map<String, Object> getProperties() {
public Map<String, String> getProperties() {
return this.properties;
}
public void setProperties(Map<String, Object> properties) {
public void setProperties(Map<String, String> properties) {
this.properties = properties;
}
@ -107,7 +107,7 @@ public class JpaProperties {
* @param dataSource the DataSource in case it is needed to determine the properties
* @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);
}
@ -116,7 +116,7 @@ public class JpaProperties {
* @param dataSource the DataSource in case it is needed to determine the properties
* @return some Hibernate properties for configuration
*/
public Map<String, Object> getHibernateProperties(DataSource dataSource) {
public Map<String, String> getHibernateProperties(DataSource dataSource) {
return this.hibernate
.getDeferredAdditionalProperties(this.properties, dataSource);
}
@ -158,7 +158,7 @@ public class JpaProperties {
return this.deferDdl;
}
private String getDeferredDdlAuto(Map<String, Object> existing,
private String getDeferredDdlAuto(Map<String, String> existing,
DataSource dataSource) {
if (!this.deferDdl) {
return "none";
@ -169,7 +169,7 @@ public class JpaProperties {
return ddlAuto;
}
if (isAlreadyProvided(existing, "hbm2ddl.auto")) {
return (String) existing.get("hibernate.hbm2ddl.auto");
return existing.get("hibernate.hbm2ddl.auto");
}
return "none";
}
@ -178,16 +178,16 @@ public class JpaProperties {
this.ddlAuto = ddlAuto;
}
private Map<String, Object> getDeferredAdditionalProperties(
Map<String, Object> properties, DataSource dataSource) {
Map<String, Object> deferred = getAdditionalProperties(properties);
private Map<String, String> getDeferredAdditionalProperties(
Map<String, String> properties, DataSource dataSource) {
Map<String, String> deferred = getAdditionalProperties(properties);
deferred.put("hibernate.hbm2ddl.auto",
getDeferredDdlAuto(properties, dataSource));
return deferred;
}
private Map<String, Object> getAdditionalProperties(Map<String, Object> existing) {
Map<String, Object> result = new HashMap<String, Object>();
private Map<String, String> getAdditionalProperties(Map<String, String> existing) {
Map<String, String> result = new HashMap<String, String>();
if (!isAlreadyProvided(existing, "ejb.naming_strategy")
&& this.namingStrategy != null) {
result.put("hibernate.ejb.naming_strategy", this.namingStrategy.getName());
@ -205,7 +205,7 @@ public class JpaProperties {
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);
}

@ -139,7 +139,7 @@ public abstract class AbstractJpaAutoConfigurationTests {
@Test
public void customJpaProperties() throws Exception {
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();
this.context.refresh();
LocalContainerEntityManagerFactoryBean bean = this.context
@ -147,6 +147,7 @@ public abstract class AbstractJpaAutoConfigurationTests {
Map<String, Object> map = bean.getJpaPropertyMap();
assertThat(map.get("a"), equalTo((Object) "b"));
assertThat(map.get("c"), equalTo((Object) "d"));
assertThat(map.get("a.b"), equalTo((Object) "c"));
}
@Test

@ -61,7 +61,7 @@ public class CustomHibernateJpaAutoConfigurationTests {
this.context.refresh();
JpaProperties bean = this.context.getBean(JpaProperties.class);
DataSource dataSource = this.context.getBean(DataSource.class);
String actual = (String) bean.getHibernateProperties(dataSource).get(
String actual = bean.getHibernateProperties(dataSource).get(
"hibernate.hbm2ddl.auto");
// Default is generic and safe
assertThat(actual, equalTo("none"));
@ -78,7 +78,7 @@ public class CustomHibernateJpaAutoConfigurationTests {
this.context.refresh();
JpaProperties bean = this.context.getBean(JpaProperties.class);
DataSource dataSource = this.context.getBean(DataSource.class);
String actual = (String) bean.getHibernateProperties(dataSource).get(
String actual = bean.getHibernateProperties(dataSource).get(
"hibernate.hbm2ddl.auto");
assertThat(actual, equalTo("create-drop"));
}

@ -47,7 +47,7 @@ public class EntityManagerFactoryBuilderTests {
new HibernateJpaVendorAdapter(), this.properties, null);
LocalContainerEntityManagerFactoryBean result1 = factory
.dataSource(this.dataSource1)
.properties(Collections.singletonMap("foo", (Object) "spam")).build();
.properties(Collections.singletonMap("foo", "spam")).build();
assertFalse(result1.getJpaPropertyMap().isEmpty());
assertTrue(this.properties.getProperties().isEmpty());
}
@ -58,7 +58,7 @@ public class EntityManagerFactoryBuilderTests {
new HibernateJpaVendorAdapter(), this.properties, null);
LocalContainerEntityManagerFactoryBean result1 = factory
.dataSource(this.dataSource1)
.properties(Collections.singletonMap("foo", (Object) "spam")).build();
.properties(Collections.singletonMap("foo", "spam")).build();
assertFalse(result1.getJpaPropertyMap().isEmpty());
LocalContainerEntityManagerFactoryBean result2 = factory.dataSource(
this.dataSource2).build();

Loading…
Cancel
Save