Merge pull request #13484 from izeye:hibernate-properties

* pr/13484:
  Polish HibernateProperties and its test
pull/13487/merge
Stephane Nicoll 7 years ago
commit 7ed06f3335

@ -21,7 +21,11 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.function.Supplier; import java.util.function.Supplier;
import org.hibernate.cfg.AvailableSettings;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy;
import org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils; import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
@ -36,9 +40,6 @@ import org.springframework.util.StringUtils;
@ConfigurationProperties("spring.jpa.hibernate") @ConfigurationProperties("spring.jpa.hibernate")
public class HibernateProperties { public class HibernateProperties {
private static final String USE_NEW_ID_GENERATOR_MAPPINGS = "hibernate.id."
+ "new_generator_mappings";
private final Naming naming = new Naming(); private final Naming naming = new Naming();
/** /**
@ -97,10 +98,10 @@ public class HibernateProperties {
getNaming().applyNamingStrategies(result); getNaming().applyNamingStrategies(result);
String ddlAuto = determineDdlAuto(existing, settings::getDdlAuto); String ddlAuto = determineDdlAuto(existing, settings::getDdlAuto);
if (StringUtils.hasText(ddlAuto) && !"none".equals(ddlAuto)) { if (StringUtils.hasText(ddlAuto) && !"none".equals(ddlAuto)) {
result.put("hibernate.hbm2ddl.auto", ddlAuto); result.put(AvailableSettings.HBM2DDL_AUTO, ddlAuto);
} }
else { else {
result.remove("hibernate.hbm2ddl.auto"); result.remove(AvailableSettings.HBM2DDL_AUTO);
} }
Collection<HibernatePropertiesCustomizer> customizers = settings Collection<HibernatePropertiesCustomizer> customizers = settings
.getHibernatePropertiesCustomizers(); .getHibernatePropertiesCustomizers();
@ -112,17 +113,17 @@ public class HibernateProperties {
private void applyNewIdGeneratorMappings(Map<String, Object> result) { private void applyNewIdGeneratorMappings(Map<String, Object> result) {
if (this.useNewIdGeneratorMappings != null) { if (this.useNewIdGeneratorMappings != null) {
result.put(USE_NEW_ID_GENERATOR_MAPPINGS, result.put(AvailableSettings.USE_NEW_ID_GENERATOR_MAPPINGS,
this.useNewIdGeneratorMappings.toString()); this.useNewIdGeneratorMappings.toString());
} }
else if (!result.containsKey(USE_NEW_ID_GENERATOR_MAPPINGS)) { else if (!result.containsKey(AvailableSettings.USE_NEW_ID_GENERATOR_MAPPINGS)) {
result.put(USE_NEW_ID_GENERATOR_MAPPINGS, "true"); result.put(AvailableSettings.USE_NEW_ID_GENERATOR_MAPPINGS, "true");
} }
} }
private String determineDdlAuto(Map<String, String> existing, private String determineDdlAuto(Map<String, String> existing,
Supplier<String> defaultDdlAuto) { Supplier<String> defaultDdlAuto) {
String ddlAuto = existing.get("hibernate.hbm2ddl.auto"); String ddlAuto = existing.get(AvailableSettings.HBM2DDL_AUTO);
if (ddlAuto != null) { if (ddlAuto != null) {
return ddlAuto; return ddlAuto;
} }
@ -131,10 +132,6 @@ public class HibernateProperties {
public static class Naming { public static class Naming {
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";
/** /**
* Fully qualified name of the implicit naming strategy. * Fully qualified name of the implicit naming strategy.
*/ */
@ -162,10 +159,10 @@ public class HibernateProperties {
} }
private void applyNamingStrategies(Map<String, Object> properties) { private void applyNamingStrategies(Map<String, Object> properties) {
applyNamingStrategy(properties, "hibernate.implicit_naming_strategy", applyNamingStrategy(properties, AvailableSettings.IMPLICIT_NAMING_STRATEGY,
this.implicitStrategy, DEFAULT_IMPLICIT_STRATEGY); this.implicitStrategy, SpringImplicitNamingStrategy.class.getName());
applyNamingStrategy(properties, "hibernate.physical_naming_strategy", applyNamingStrategy(properties, AvailableSettings.PHYSICAL_NAMING_STRATEGY,
this.physicalStrategy, DEFAULT_PHYSICAL_STRATEGY); this.physicalStrategy, SpringPhysicalNamingStrategy.class.getName());
} }
private void applyNamingStrategy(Map<String, Object> properties, String key, private void applyNamingStrategy(Map<String, Object> properties, String key,

@ -63,10 +63,10 @@ public class HibernatePropertiesTests {
assertThat(hibernateProperties) assertThat(hibernateProperties)
.doesNotContainKeys("hibernate.ejb.naming_strategy"); .doesNotContainKeys("hibernate.ejb.naming_strategy");
assertThat(hibernateProperties).containsEntry( assertThat(hibernateProperties).containsEntry(
"hibernate.physical_naming_strategy", AvailableSettings.PHYSICAL_NAMING_STRATEGY,
SpringPhysicalNamingStrategy.class.getName()); SpringPhysicalNamingStrategy.class.getName());
assertThat(hibernateProperties).containsEntry( assertThat(hibernateProperties).containsEntry(
"hibernate.implicit_naming_strategy", AvailableSettings.IMPLICIT_NAMING_STRATEGY,
SpringImplicitNamingStrategy.class.getName()); SpringImplicitNamingStrategy.class.getName());
})); }));
} }
@ -78,9 +78,9 @@ public class HibernatePropertiesTests {
"spring.jpa.hibernate.naming.physical-strategy:com.example.Physical") "spring.jpa.hibernate.naming.physical-strategy:com.example.Physical")
.run(assertHibernateProperties((hibernateProperties) -> { .run(assertHibernateProperties((hibernateProperties) -> {
assertThat(hibernateProperties).contains( assertThat(hibernateProperties).contains(
entry("hibernate.implicit_naming_strategy", entry(AvailableSettings.IMPLICIT_NAMING_STRATEGY,
"com.example.Implicit"), "com.example.Implicit"),
entry("hibernate.physical_naming_strategy", entry(AvailableSettings.PHYSICAL_NAMING_STRATEGY,
"com.example.Physical")); "com.example.Physical"));
assertThat(hibernateProperties) assertThat(hibernateProperties)
.doesNotContainKeys("hibernate.ejb.naming_strategy"); .doesNotContainKeys("hibernate.ejb.naming_strategy");
@ -95,9 +95,9 @@ public class HibernatePropertiesTests {
.run(assertHibernateProperties((hibernateProperties) -> { .run(assertHibernateProperties((hibernateProperties) -> {
// You can override them as we don't provide any default // You can override them as we don't provide any default
assertThat(hibernateProperties).contains( assertThat(hibernateProperties).contains(
entry("hibernate.implicit_naming_strategy", entry(AvailableSettings.IMPLICIT_NAMING_STRATEGY,
"com.example.Implicit"), "com.example.Implicit"),
entry("hibernate.physical_naming_strategy", entry(AvailableSettings.PHYSICAL_NAMING_STRATEGY,
"com.example.Physical")); "com.example.Physical"));
assertThat(hibernateProperties) assertThat(hibernateProperties)
.doesNotContainKeys("hibernate.ejb.naming_strategy"); .doesNotContainKeys("hibernate.ejb.naming_strategy");
@ -139,7 +139,7 @@ public class HibernatePropertiesTests {
private ContextConsumer<AssertableApplicationContext> assertDefaultDdlAutoNotInvoked( private ContextConsumer<AssertableApplicationContext> assertDefaultDdlAutoNotInvoked(
String expectedDdlAuto) { String expectedDdlAuto) {
return assertHibernateProperties((hibernateProperties) -> { return assertHibernateProperties((hibernateProperties) -> {
assertThat(hibernateProperties).containsEntry("hibernate.hbm2ddl.auto", assertThat(hibernateProperties).containsEntry(AvailableSettings.HBM2DDL_AUTO,
expectedDdlAuto); expectedDdlAuto);
verify(this.ddlAutoSupplier, never()).get(); verify(this.ddlAutoSupplier, never()).get();
}); });

Loading…
Cancel
Save