From b6ee633c53ff17cb288f7708e559198f0f3f91f2 Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Mon, 9 Apr 2018 16:43:01 +0100 Subject: [PATCH] HHH-12454 - o.h.boot.spi.SessionFactoryOptions#getJpaCompliance() returns an immutable copy --- .../boot/internal/BootstrapContextImpl.java | 4 +- .../SessionFactoryOptionsBuilder.java | 2 +- .../jpa/internal/JpaComplianceImpl.java | 138 +++++++++-------- .../internal/MutableJpaComplianceImpl.java | 143 ++++++++++++++++++ .../jpa/spi/MutableJpaCompliance.java | 2 + 5 files changed, 219 insertions(+), 70 deletions(-) create mode 100644 hibernate-core/src/main/java/org/hibernate/jpa/internal/MutableJpaComplianceImpl.java diff --git a/hibernate-core/src/main/java/org/hibernate/boot/internal/BootstrapContextImpl.java b/hibernate-core/src/main/java/org/hibernate/boot/internal/BootstrapContextImpl.java index f7362a7453..0004d04995 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/internal/BootstrapContextImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/internal/BootstrapContextImpl.java @@ -36,7 +36,7 @@ import org.hibernate.cfg.AvailableSettings; import org.hibernate.cfg.annotations.reflection.JPAMetadataProvider; import org.hibernate.dialect.function.SQLFunction; import org.hibernate.engine.config.spi.ConfigurationService; -import org.hibernate.jpa.internal.JpaComplianceImpl; +import org.hibernate.jpa.internal.MutableJpaComplianceImpl; import org.hibernate.jpa.spi.MutableJpaCompliance; import org.hibernate.type.spi.TypeConfiguration; @@ -91,7 +91,7 @@ public class BootstrapContextImpl implements BootstrapContext { final StrategySelector strategySelector = serviceRegistry.getService( StrategySelector.class ); final ConfigurationService configService = serviceRegistry.getService( ConfigurationService.class ); - this.jpaCompliance = new JpaComplianceImpl( configService.getSettings(), false ); + this.jpaCompliance = new MutableJpaComplianceImpl( configService.getSettings(), false ); this.scanOptions = new StandardScanOptions( (String) configService.getSettings().get( AvailableSettings.SCANNER_DISCOVERY ), false diff --git a/hibernate-core/src/main/java/org/hibernate/boot/internal/SessionFactoryOptionsBuilder.java b/hibernate-core/src/main/java/org/hibernate/boot/internal/SessionFactoryOptionsBuilder.java index 3fa5849660..6c18a71d82 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/internal/SessionFactoryOptionsBuilder.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/internal/SessionFactoryOptionsBuilder.java @@ -975,7 +975,7 @@ public class SessionFactoryOptionsBuilder implements SessionFactoryOptions { @Override public JpaCompliance getJpaCompliance() { - return jpaCompliance; + return jpaCompliance.immutableCopy(); } // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/hibernate-core/src/main/java/org/hibernate/jpa/internal/JpaComplianceImpl.java b/hibernate-core/src/main/java/org/hibernate/jpa/internal/JpaComplianceImpl.java index c58c406962..30cf871735 100644 --- a/hibernate-core/src/main/java/org/hibernate/jpa/internal/JpaComplianceImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/jpa/internal/JpaComplianceImpl.java @@ -6,64 +6,35 @@ */ package org.hibernate.jpa.internal; -import java.util.Map; - -import org.hibernate.cfg.AvailableSettings; -import org.hibernate.internal.util.config.ConfigurationHelper; -import org.hibernate.jpa.spi.MutableJpaCompliance; +import org.hibernate.jpa.spi.JpaCompliance; /** - * @author Steve Ebersole + * @author Andrea Boriero */ -public class JpaComplianceImpl implements MutableJpaCompliance { +public class JpaComplianceImpl implements JpaCompliance { private boolean queryCompliance; private boolean transactionCompliance; private boolean listCompliance; private boolean closedCompliance; private boolean proxyCompliance; private boolean cachingCompliance; - private final boolean globalGeneratorNameScopeCompliance; + private boolean globalGeneratorNameScopeCompliance; - - @SuppressWarnings("ConstantConditions") - public JpaComplianceImpl(Map configurationSettings, boolean jpaByDefault) { - final Object legacyQueryCompliance = configurationSettings.get( AvailableSettings.JPAQL_STRICT_COMPLIANCE ); - - queryCompliance = ConfigurationHelper.getBoolean( - AvailableSettings.JPA_QUERY_COMPLIANCE, - configurationSettings, - ConfigurationHelper.toBoolean( legacyQueryCompliance, jpaByDefault ) - ); - transactionCompliance = ConfigurationHelper.getBoolean( - AvailableSettings.JPA_TRANSACTION_COMPLIANCE, - configurationSettings, - jpaByDefault - ); - listCompliance = ConfigurationHelper.getBoolean( - AvailableSettings.JPA_LIST_COMPLIANCE, - configurationSettings, - jpaByDefault - ); - closedCompliance = ConfigurationHelper.getBoolean( - AvailableSettings.JPA_CLOSED_COMPLIANCE, - configurationSettings, - jpaByDefault - ); - proxyCompliance = ConfigurationHelper.getBoolean( - AvailableSettings.JPA_PROXY_COMPLIANCE, - configurationSettings, - jpaByDefault - ); - cachingCompliance = ConfigurationHelper.getBoolean( - AvailableSettings.JPA_CACHING_COMPLIANCE, - configurationSettings, - jpaByDefault - ); - globalGeneratorNameScopeCompliance = ConfigurationHelper.getBoolean( - AvailableSettings.JPA_ID_GENERATOR_GLOBAL_SCOPE_COMPLIANCE, - configurationSettings, - jpaByDefault - ); + private JpaComplianceImpl( + boolean queryCompliance, + boolean transactionCompliance, + boolean listCompliance, + boolean closedCompliance, + boolean proxyCompliance, + boolean cachingCompliance, + boolean globalGeneratorNameScopeCompliance) { + this.queryCompliance = queryCompliance; + this.transactionCompliance = transactionCompliance; + this.listCompliance = listCompliance; + this.closedCompliance = closedCompliance; + this.proxyCompliance = proxyCompliance; + this.cachingCompliance = cachingCompliance; + this.globalGeneratorNameScopeCompliance = globalGeneratorNameScopeCompliance; } @Override @@ -101,30 +72,63 @@ public class JpaComplianceImpl implements MutableJpaCompliance { return globalGeneratorNameScopeCompliance; } - // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - // Mutators + public static class JpaComplianceBuilder { + private boolean queryCompliance; + private boolean transactionCompliance; + private boolean listCompliance; + private boolean closedCompliance; + private boolean proxyCompliance; + private boolean cachingCompliance; + private boolean globalGeneratorNameScopeCompliance; - public void setQueryCompliance(boolean queryCompliance) { - this.queryCompliance = queryCompliance; - } + public JpaComplianceBuilder() { + } - public void setTransactionCompliance(boolean transactionCompliance) { - this.transactionCompliance = transactionCompliance; - } + public JpaComplianceBuilder setQueryCompliance(boolean queryCompliance) { + this.queryCompliance = queryCompliance; + return this; + } - public void setListCompliance(boolean listCompliance) { - this.listCompliance = listCompliance; - } + public JpaComplianceBuilder setTransactionCompliance(boolean transactionCompliance) { + this.transactionCompliance = transactionCompliance; + return this; + } - public void setClosedCompliance(boolean closedCompliance) { - this.closedCompliance = closedCompliance; - } + public JpaComplianceBuilder setListCompliance(boolean listCompliance) { + this.listCompliance = listCompliance; + return this; + } - public void setProxyCompliance(boolean proxyCompliance) { - this.proxyCompliance = proxyCompliance; - } + public JpaComplianceBuilder setClosedCompliance(boolean closedCompliance) { + this.closedCompliance = closedCompliance; + return this; + } - public void setCachingCompliance(boolean cachingCompliance) { - this.cachingCompliance = cachingCompliance; + public JpaComplianceBuilder setProxyCompliance(boolean proxyCompliance) { + this.proxyCompliance = proxyCompliance; + return this; + } + + public JpaComplianceBuilder setCachingCompliance(boolean cachingCompliance) { + this.cachingCompliance = cachingCompliance; + return this; + } + + public JpaComplianceBuilder setGlobalGeneratorNameCompliance(boolean globalGeneratorNameCompliance) { + this.globalGeneratorNameScopeCompliance = globalGeneratorNameCompliance; + return this; + } + + JpaCompliance createJpaCompliance() { + return new JpaComplianceImpl( + queryCompliance, + transactionCompliance, + listCompliance, + closedCompliance, + proxyCompliance, + cachingCompliance, + globalGeneratorNameScopeCompliance + ); + } } } diff --git a/hibernate-core/src/main/java/org/hibernate/jpa/internal/MutableJpaComplianceImpl.java b/hibernate-core/src/main/java/org/hibernate/jpa/internal/MutableJpaComplianceImpl.java new file mode 100644 index 0000000000..cf26d2e096 --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/jpa/internal/MutableJpaComplianceImpl.java @@ -0,0 +1,143 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.jpa.internal; + +import java.util.Map; + +import org.hibernate.cfg.AvailableSettings; +import org.hibernate.internal.util.config.ConfigurationHelper; +import org.hibernate.jpa.spi.JpaCompliance; +import org.hibernate.jpa.spi.MutableJpaCompliance; + +/** + * @author Steve Ebersole + */ +public class MutableJpaComplianceImpl implements MutableJpaCompliance { + private boolean queryCompliance; + private boolean transactionCompliance; + private boolean listCompliance; + private boolean closedCompliance; + private boolean proxyCompliance; + private boolean cachingCompliance; + private final boolean globalGeneratorNameScopeCompliance; + + @SuppressWarnings("ConstantConditions") + public MutableJpaComplianceImpl(Map configurationSettings, boolean jpaByDefault) { + final Object legacyQueryCompliance = configurationSettings.get( AvailableSettings.JPAQL_STRICT_COMPLIANCE ); + + queryCompliance = ConfigurationHelper.getBoolean( + AvailableSettings.JPA_QUERY_COMPLIANCE, + configurationSettings, + ConfigurationHelper.toBoolean( legacyQueryCompliance, jpaByDefault ) + ); + transactionCompliance = ConfigurationHelper.getBoolean( + AvailableSettings.JPA_TRANSACTION_COMPLIANCE, + configurationSettings, + jpaByDefault + ); + listCompliance = ConfigurationHelper.getBoolean( + AvailableSettings.JPA_LIST_COMPLIANCE, + configurationSettings, + jpaByDefault + ); + closedCompliance = ConfigurationHelper.getBoolean( + AvailableSettings.JPA_CLOSED_COMPLIANCE, + configurationSettings, + jpaByDefault + ); + proxyCompliance = ConfigurationHelper.getBoolean( + AvailableSettings.JPA_PROXY_COMPLIANCE, + configurationSettings, + jpaByDefault + ); + cachingCompliance = ConfigurationHelper.getBoolean( + AvailableSettings.JPA_CACHING_COMPLIANCE, + configurationSettings, + jpaByDefault + ); + globalGeneratorNameScopeCompliance = ConfigurationHelper.getBoolean( + AvailableSettings.JPA_ID_GENERATOR_GLOBAL_SCOPE_COMPLIANCE, + configurationSettings, + jpaByDefault + ); + } + + @Override + public boolean isJpaQueryComplianceEnabled() { + return queryCompliance; + } + + @Override + public boolean isJpaTransactionComplianceEnabled() { + return transactionCompliance; + } + + @Override + public boolean isJpaListComplianceEnabled() { + return listCompliance; + } + + @Override + public boolean isJpaClosedComplianceEnabled() { + return closedCompliance; + } + + @Override + public boolean isJpaProxyComplianceEnabled() { + return proxyCompliance; + } + + @Override + public boolean isJpaCacheComplianceEnabled() { + return cachingCompliance; + } + + @Override + public boolean isGlobalGeneratorScopeEnabled() { + return globalGeneratorNameScopeCompliance; + } + + // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + // Mutators + + public void setQueryCompliance(boolean queryCompliance) { + this.queryCompliance = queryCompliance; + } + + public void setTransactionCompliance(boolean transactionCompliance) { + this.transactionCompliance = transactionCompliance; + } + + public void setListCompliance(boolean listCompliance) { + this.listCompliance = listCompliance; + } + + public void setClosedCompliance(boolean closedCompliance) { + this.closedCompliance = closedCompliance; + } + + public void setProxyCompliance(boolean proxyCompliance) { + this.proxyCompliance = proxyCompliance; + } + + public void setCachingCompliance(boolean cachingCompliance) { + this.cachingCompliance = cachingCompliance; + } + + @Override + public JpaCompliance immutableCopy() { + JpaComplianceImpl.JpaComplianceBuilder builder = new JpaComplianceImpl.JpaComplianceBuilder(); + builder.setQueryCompliance( queryCompliance ) + .setTransactionCompliance( transactionCompliance ) + .setListCompliance( listCompliance ) + .setClosedCompliance( closedCompliance ) + .setProxyCompliance( proxyCompliance ) + .setCachingCompliance( cachingCompliance ) + .setGlobalGeneratorNameCompliance( globalGeneratorNameScopeCompliance ); + return builder.createJpaCompliance(); + } +} diff --git a/hibernate-core/src/main/java/org/hibernate/jpa/spi/MutableJpaCompliance.java b/hibernate-core/src/main/java/org/hibernate/jpa/spi/MutableJpaCompliance.java index daa351b726..37f068d7f6 100644 --- a/hibernate-core/src/main/java/org/hibernate/jpa/spi/MutableJpaCompliance.java +++ b/hibernate-core/src/main/java/org/hibernate/jpa/spi/MutableJpaCompliance.java @@ -21,4 +21,6 @@ public interface MutableJpaCompliance extends JpaCompliance { void setProxyCompliance(boolean proxyCompliance); void setCachingCompliance(boolean cachingCompliance); + + JpaCompliance immutableCopy(); }