Commit 6ee8cde5 authored by Stephane Nicoll's avatar Stephane Nicoll

Merge pull request #15565 from ayudovin

* pr/15565:
  Polish "Disable Hibernate entity scanning for default JPA setup"
  Disable Hibernate entity scanning for default JPA setup
parents 33547569 d0811b48
......@@ -27,6 +27,7 @@ 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.ClassUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
......@@ -40,6 +41,8 @@ import org.springframework.util.StringUtils;
@ConfigurationProperties("spring.jpa.hibernate")
public class HibernateProperties {
private static final String DISABLED_SCANNER_CLASS = "org.hibernate.boot.archive.scan.internal.DisabledScanner";
private final Naming naming = new Naming();
/**
......@@ -95,6 +98,7 @@ public class HibernateProperties {
HibernateSettings settings) {
Map<String, Object> result = new HashMap<>(existing);
applyNewIdGeneratorMappings(result);
applyScanner(result);
getNaming().applyNamingStrategies(result);
String ddlAuto = determineDdlAuto(existing, settings::getDdlAuto);
if (StringUtils.hasText(ddlAuto) && !"none".equals(ddlAuto)) {
......@@ -121,6 +125,13 @@ public class HibernateProperties {
}
}
private void applyScanner(Map<String, Object> result) {
if (!result.containsKey(AvailableSettings.SCANNER)
&& ClassUtils.isPresent(DISABLED_SCANNER_CLASS, null)) {
result.put(AvailableSettings.SCANNER, DISABLED_SCANNER_CLASS);
}
}
private String determineDdlAuto(Map<String, String> existing,
Supplier<String> defaultDdlAuto) {
String ddlAuto = existing.get(AvailableSettings.HBM2DDL_AUTO);
......
......@@ -43,6 +43,7 @@ import static org.mockito.Mockito.verify;
* Tests for {@link HibernateProperties}.
*
* @author Stephane Nicoll
* @author Artsiom Yudovin
*/
public class HibernatePropertiesTests {
......@@ -123,6 +124,23 @@ public class HibernatePropertiesTests {
"false")));
}
@Test
public void scannerUsesDisabledScannerByDefault() {
this.contextRunner.run(assertHibernateProperties(
(hibernateProperties) -> assertThat(hibernateProperties).containsEntry(
AvailableSettings.SCANNER,
"org.hibernate.boot.archive.scan.internal.DisabledScanner")));
}
@Test
public void scannerCanBeCustomized() {
this.contextRunner.withPropertyValues(
"spring.jpa.properties.hibernate.archive.scanner:org.hibernate.boot.archive.scan.internal.StandardScanner")
.run(assertHibernateProperties((hibernateProperties) -> assertThat(
hibernateProperties).containsEntry(AvailableSettings.SCANNER,
"org.hibernate.boot.archive.scan.internal.StandardScanner")));
}
@Test
public void defaultDdlAutoIsNotInvokedIfPropertyIsSet() {
this.contextRunner.withPropertyValues("spring.jpa.hibernate.ddl-auto=validate")
......
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