diff --git a/documentation/src/main/asciidoc/userguide/chapters/multitenancy/MultiTenancy.adoc b/documentation/src/main/asciidoc/userguide/chapters/multitenancy/MultiTenancy.adoc index 5f9af10b71..9d4af85a61 100644 --- a/documentation/src/main/asciidoc/userguide/chapters/multitenancy/MultiTenancy.adoc +++ b/documentation/src/main/asciidoc/userguide/chapters/multitenancy/MultiTenancy.adoc @@ -73,26 +73,6 @@ include::{sourcedir}/AbstractMultiTenancyTest.java[tags=multitenacy-hibernate-se ---- ==== -Additionally, when specifying the configuration, an `org.hibernate.MultiTenancyStrategy` should be named using the `hibernate.multiTenancy` setting. -Hibernate will perform validations based on the type of strategy you specify. -The strategy here correlates with the isolation approach discussed above. - -NONE:: - (the default) No multitenancy is expected. - In fact, it is considered an error if a tenant identifier is specified when opening a session using this strategy. -SCHEMA:: - Correlates to the separate schema approach. - It is an error to attempt to open a session without a tenant identifier using this strategy. - Additionally, a `MultiTenantConnectionProvider` must be specified. -DATABASE:: - Correlates to the separate database approach. - It is an error to attempt to open a session without a tenant identifier using this strategy. - Additionally, a `MultiTenantConnectionProvider` must be specified. -DISCRIMINATOR:: - Correlates to the partitioned (discriminator) approach. - It is an error to attempt to open a session without a tenant identifier using this strategy. - This strategy is not yet implemented and you can follow its progress via the https://hibernate.atlassian.net/browse/HHH-6054[HHH-6054 Jira issue]. - [[multitenacy-hibernate-MultiTenantConnectionProvider]] ==== MultiTenantConnectionProvider diff --git a/documentation/src/test/java/org/hibernate/userguide/multitenancy/AbstractMultiTenancyTest.java b/documentation/src/test/java/org/hibernate/userguide/multitenancy/AbstractMultiTenancyTest.java index 5020b52f24..fed9d096d2 100644 --- a/documentation/src/test/java/org/hibernate/userguide/multitenancy/AbstractMultiTenancyTest.java +++ b/documentation/src/test/java/org/hibernate/userguide/multitenancy/AbstractMultiTenancyTest.java @@ -16,7 +16,6 @@ import java.util.function.Consumer; import jakarta.persistence.Entity; import jakarta.persistence.Id; -import org.hibernate.MultiTenancyStrategy; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; @@ -63,7 +62,6 @@ public abstract class AbstractMultiTenancyTest extends BaseUnitTestCase { Map settings = new HashMap<>( ); - settings.put( AvailableSettings.MULTI_TENANT, multiTenancyStrategy() ); settings.put( AvailableSettings.MULTI_TENANT_CONNECTION_PROVIDER, new ConfigurableMultiTenantConnectionProvider( connectionProviderMap ) ); @@ -115,8 +113,6 @@ public abstract class AbstractMultiTenancyTest extends BaseUnitTestCase { //end::multitenacy-multitenacy-hibernate-same-entity-example[] } - protected abstract MultiTenancyStrategy multiTenancyStrategy(); - protected Properties properties() { Properties properties = new Properties( ); URL propertiesURL = Thread.currentThread().getContextClassLoader().getResource( "hibernate.properties" ); diff --git a/documentation/src/test/java/org/hibernate/userguide/multitenancy/DatabaseMultiTenancyTest.java b/documentation/src/test/java/org/hibernate/userguide/multitenancy/DatabaseMultiTenancyTest.java index 40f73c8fdc..73d9bfa575 100644 --- a/documentation/src/test/java/org/hibernate/userguide/multitenancy/DatabaseMultiTenancyTest.java +++ b/documentation/src/test/java/org/hibernate/userguide/multitenancy/DatabaseMultiTenancyTest.java @@ -6,7 +6,6 @@ */ package org.hibernate.userguide.multitenancy; -import org.hibernate.MultiTenancyStrategy; import org.hibernate.dialect.H2Dialect; import org.hibernate.testing.RequiresDialect; @@ -17,11 +16,6 @@ import org.hibernate.testing.RequiresDialect; @RequiresDialect(H2Dialect.class) public class DatabaseMultiTenancyTest extends AbstractMultiTenancyTest { - @Override - protected MultiTenancyStrategy multiTenancyStrategy() { - return MultiTenancyStrategy.DATABASE; - } - @Override protected String tenantUrl(String originalUrl, String tenantIdentifier) { return originalUrl.replace( "db1", tenantIdentifier ); diff --git a/documentation/src/test/java/org/hibernate/userguide/multitenancy/DatabaseTimeZoneMultiTenancyTest.java b/documentation/src/test/java/org/hibernate/userguide/multitenancy/DatabaseTimeZoneMultiTenancyTest.java index e2ba0baab0..9b8a3690d1 100644 --- a/documentation/src/test/java/org/hibernate/userguide/multitenancy/DatabaseTimeZoneMultiTenancyTest.java +++ b/documentation/src/test/java/org/hibernate/userguide/multitenancy/DatabaseTimeZoneMultiTenancyTest.java @@ -21,7 +21,6 @@ import jakarta.persistence.Column; import jakarta.persistence.Entity; import jakarta.persistence.Id; -import org.hibernate.MultiTenancyStrategy; import org.hibernate.Session; import org.hibernate.SessionBuilder; import org.hibernate.SessionFactory; @@ -79,7 +78,6 @@ public class DatabaseTimeZoneMultiTenancyTest extends BaseUnitTestCase { Map settings = new HashMap<>(); - settings.put( AvailableSettings.MULTI_TENANT, MultiTenancyStrategy.DATABASE ); settings.put( AvailableSettings.MULTI_TENANT_CONNECTION_PROVIDER, new ConfigurableMultiTenantConnectionProvider( connectionProviderMap ) diff --git a/documentation/src/test/java/org/hibernate/userguide/multitenancy/SchemaMultiTenancyTest.java b/documentation/src/test/java/org/hibernate/userguide/multitenancy/SchemaMultiTenancyTest.java index b066f28d9d..4fddec1891 100644 --- a/documentation/src/test/java/org/hibernate/userguide/multitenancy/SchemaMultiTenancyTest.java +++ b/documentation/src/test/java/org/hibernate/userguide/multitenancy/SchemaMultiTenancyTest.java @@ -6,7 +6,6 @@ */ package org.hibernate.userguide.multitenancy; -import org.hibernate.MultiTenancyStrategy; import org.hibernate.dialect.H2Dialect; import org.hibernate.testing.RequiresDialect; @@ -19,11 +18,6 @@ public class SchemaMultiTenancyTest extends AbstractMultiTenancyTest { public static final String SCHEMA_TOKEN = ";INIT=CREATE SCHEMA IF NOT EXISTS %1$s\\;SET SCHEMA %1$s"; - @Override - protected MultiTenancyStrategy multiTenancyStrategy() { - return MultiTenancyStrategy.SCHEMA; - } - @Override protected String tenantUrl(String originalUrl, String tenantIdentifier) { return originalUrl + String.format( SCHEMA_TOKEN, tenantIdentifier ); diff --git a/hibernate-core/src/main/java/org/hibernate/MultiTenancyStrategy.java b/hibernate-core/src/main/java/org/hibernate/MultiTenancyStrategy.java deleted file mode 100644 index 6dd80723d1..0000000000 --- a/hibernate-core/src/main/java/org/hibernate/MultiTenancyStrategy.java +++ /dev/null @@ -1,82 +0,0 @@ -/* - * 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; - -import java.util.Locale; -import java.util.Map; - -import org.hibernate.cfg.Environment; -import org.hibernate.internal.CoreMessageLogger; - -import org.jboss.logging.Logger; - -/** - * Describes the methods for multi-tenancy understood by Hibernate. - * - * @author Steve Ebersole - */ -public enum MultiTenancyStrategy { - /** - * Multi-tenancy implemented by use of discriminator columns. - */ - DISCRIMINATOR, - /** - * Multi-tenancy implemented as separate schemas. - */ - SCHEMA, - /** - * Multi-tenancy implemented as separate databases. - */ - DATABASE, - /** - * No multi-tenancy. - */ - NONE; - - private static final CoreMessageLogger LOG = Logger.getMessageLogger( - CoreMessageLogger.class, - MultiTenancyStrategy.class.getName() - ); - - /** - * Does this strategy indicate a requirement for the specialized - * {@link org.hibernate.engine.jdbc.connections.spi.MultiTenantConnectionProvider}, rather than the - * traditional {@link org.hibernate.engine.jdbc.connections.spi.ConnectionProvider}? - * - * @return {@code true} indicates a MultiTenantConnectionProvider is required; {@code false} indicates it is not. - */ - public boolean requiresMultiTenantConnectionProvider() { - return this == DATABASE || this == SCHEMA; - } - - /** - * Extract the MultiTenancyStrategy from the setting map. - * - * @param properties The map of settings. - * - * @return The selected strategy. {@link #NONE} is always the default. - */ - public static MultiTenancyStrategy determineMultiTenancyStrategy(Map properties) { - final Object strategy = properties.get( Environment.MULTI_TENANT ); - if ( strategy == null ) { - return MultiTenancyStrategy.NONE; - } - - if ( MultiTenancyStrategy.class.isInstance( strategy ) ) { - return (MultiTenancyStrategy) strategy; - } - - final String strategyName = strategy.toString(); - try { - return MultiTenancyStrategy.valueOf( strategyName.toUpperCase(Locale.ROOT) ); - } - catch ( RuntimeException e ) { - LOG.warn( "Unknown multi tenancy strategy [ " +strategyName +" ], using MultiTenancyStrategy.NONE." ); - return MultiTenancyStrategy.NONE; - } - } -} diff --git a/hibernate-core/src/main/java/org/hibernate/boot/SessionFactoryBuilder.java b/hibernate-core/src/main/java/org/hibernate/boot/SessionFactoryBuilder.java index ec1814e52f..64042ba74b 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/SessionFactoryBuilder.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/SessionFactoryBuilder.java @@ -13,7 +13,6 @@ import org.hibernate.ConnectionReleaseMode; import org.hibernate.CustomEntityDirtinessStrategy; import org.hibernate.EntityNameResolver; import org.hibernate.Interceptor; -import org.hibernate.MultiTenancyStrategy; import org.hibernate.SessionFactory; import org.hibernate.SessionFactoryObserver; import org.hibernate.cache.spi.TimestampsCacheFactory; @@ -354,13 +353,11 @@ public interface SessionFactoryBuilder { /** * Apply the form of multi-tenancy used by the application * - * @param strategy The form of multi-tenancy in use. + * @param enabled True if multi-tenancy in use. * * @return {@code this}, for method chaining - * - * @see org.hibernate.cfg.AvailableSettings#MULTI_TENANT */ - SessionFactoryBuilder applyMultiTenancyStrategy(MultiTenancyStrategy strategy); + SessionFactoryBuilder applyMultiTenancy(boolean enabled); /** * Specifies a strategy for resolving the notion of a "current" tenant-identifier when using multi-tenancy diff --git a/hibernate-core/src/main/java/org/hibernate/boot/internal/MetadataBuilderImpl.java b/hibernate-core/src/main/java/org/hibernate/boot/internal/MetadataBuilderImpl.java index b845708d9b..d9ec8c44ff 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/internal/MetadataBuilderImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/internal/MetadataBuilderImpl.java @@ -12,7 +12,6 @@ import java.util.List; import java.util.concurrent.Callable; import org.hibernate.HibernateException; -import org.hibernate.MultiTenancyStrategy; import org.hibernate.TimeZoneStorageStrategy; import org.hibernate.annotations.CacheConcurrencyStrategy; import org.hibernate.annotations.TimeZoneStorageType; @@ -533,7 +532,7 @@ public class MetadataBuilderImpl implements MetadataBuilderImplementor, TypeCont private SharedCacheMode sharedCacheMode; private AccessType defaultCacheAccessType; - private MultiTenancyStrategy multiTenancyStrategy; + private boolean multiTenancyEnabled; private boolean explicitDiscriminatorsForJoinedInheritanceSupported; private boolean implicitDiscriminatorsForJoinedInheritanceSupported; private boolean implicitlyForceDiscriminatorInSelect; @@ -556,7 +555,7 @@ public class MetadataBuilderImpl implements MetadataBuilderImplementor, TypeCont this.mappingDefaults = new MappingDefaultsImpl( serviceRegistry ); this.defaultTimezoneStorage = resolveTimeZoneStorageStrategy( serviceRegistry, configService ); - this.multiTenancyStrategy = MultiTenancyStrategy.determineMultiTenancyStrategy( configService.getSettings() ); + this.multiTenancyEnabled = configService.getSettings().containsKey( AvailableSettings.MULTI_TENANT_CONNECTION_PROVIDER ); this.xmlMappingEnabled = configService.getSetting( AvailableSettings.XML_MAPPING_ENABLED, @@ -794,8 +793,8 @@ public class MetadataBuilderImpl implements MetadataBuilderImplementor, TypeCont } @Override - public MultiTenancyStrategy getMultiTenancyStrategy() { - return multiTenancyStrategy; + public boolean isMultiTenancyEnabled() { + return multiTenancyEnabled; } @Override diff --git a/hibernate-core/src/main/java/org/hibernate/boot/internal/SessionFactoryBuilderImpl.java b/hibernate-core/src/main/java/org/hibernate/boot/internal/SessionFactoryBuilderImpl.java index 077fdcc4dd..375112466c 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/internal/SessionFactoryBuilderImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/internal/SessionFactoryBuilderImpl.java @@ -13,7 +13,6 @@ import org.hibernate.ConnectionReleaseMode; import org.hibernate.CustomEntityDirtinessStrategy; import org.hibernate.EntityNameResolver; import org.hibernate.Interceptor; -import org.hibernate.MultiTenancyStrategy; import org.hibernate.SessionFactory; import org.hibernate.SessionFactoryObserver; import org.hibernate.boot.SessionFactoryBuilder; @@ -251,8 +250,8 @@ public class SessionFactoryBuilderImpl implements SessionFactoryBuilderImplement } @Override - public SessionFactoryBuilder applyMultiTenancyStrategy(MultiTenancyStrategy strategy) { - this.optionsBuilder.applyMultiTenancyStrategy( strategy ); + public SessionFactoryBuilder applyMultiTenancy(boolean enabled) { + this.optionsBuilder.applyMultiTenancy(enabled); return this; } 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 c6b253dc01..ec0f7266a3 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 @@ -24,7 +24,6 @@ import org.hibernate.EmptyInterceptor; import org.hibernate.EntityNameResolver; import org.hibernate.HibernateException; import org.hibernate.Interceptor; -import org.hibernate.MultiTenancyStrategy; import org.hibernate.SessionEventListener; import org.hibernate.SessionFactoryObserver; import org.hibernate.TimeZoneStorageStrategy; @@ -110,6 +109,7 @@ import static org.hibernate.cfg.AvailableSettings.JPA_CALLBACKS_ENABLED; import static org.hibernate.cfg.AvailableSettings.JTA_TRACK_BY_THREAD; import static org.hibernate.cfg.AvailableSettings.LOG_SESSION_METRICS; import static org.hibernate.cfg.AvailableSettings.MAX_FETCH_DEPTH; +import static org.hibernate.cfg.AvailableSettings.MULTI_TENANT_CONNECTION_PROVIDER; import static org.hibernate.cfg.AvailableSettings.MULTI_TENANT_IDENTIFIER_RESOLVER; import static org.hibernate.cfg.AvailableSettings.NATIVE_EXCEPTION_HANDLING_51_COMPLIANCE; import static org.hibernate.cfg.AvailableSettings.OMIT_JOIN_OF_SUPERCLASS_TABLES; @@ -212,7 +212,7 @@ public class SessionFactoryOptionsBuilder implements SessionFactoryOptions { private boolean callbacksEnabled; // multi-tenancy - private MultiTenancyStrategy multiTenancyStrategy; + private boolean multiTenancyEnabled; private CurrentTenantIdentifierResolver currentTenantIdentifierResolver; // Queries @@ -367,7 +367,7 @@ public class SessionFactoryOptionsBuilder implements SessionFactoryOptions { this.checkNullability = cfgService.getSetting( CHECK_NULLABILITY, BOOLEAN, true ); this.initializeLazyStateOutsideTransactions = cfgService.getSetting( ENABLE_LAZY_LOAD_NO_TRANS, BOOLEAN, false ); - this.multiTenancyStrategy = MultiTenancyStrategy.determineMultiTenancyStrategy( configurationSettings ); + this.multiTenancyEnabled = configurationSettings.containsKey( MULTI_TENANT_CONNECTION_PROVIDER ); this.currentTenantIdentifierResolver = strategySelector.resolveStrategy( CurrentTenantIdentifierResolver.class, configurationSettings.get( MULTI_TENANT_IDENTIFIER_RESOLVER ) @@ -1051,8 +1051,8 @@ public class SessionFactoryOptionsBuilder implements SessionFactoryOptions { } @Override - public MultiTenancyStrategy getMultiTenancyStrategy() { - return multiTenancyStrategy; + public boolean isMultiTenancyEnabled() { + return multiTenancyEnabled; } @Override @@ -1424,8 +1424,8 @@ public class SessionFactoryOptionsBuilder implements SessionFactoryOptions { this.postInsertIdentifierDelayed = enabled; } - public void applyMultiTenancyStrategy(MultiTenancyStrategy strategy) { - this.multiTenancyStrategy = strategy; + public void applyMultiTenancy(boolean enabled) { + this.multiTenancyEnabled = enabled; } public void applyCurrentTenantIdentifierResolver(CurrentTenantIdentifierResolver resolver) { diff --git a/hibernate-core/src/main/java/org/hibernate/boot/spi/AbstractDelegatingMetadataBuildingOptions.java b/hibernate-core/src/main/java/org/hibernate/boot/spi/AbstractDelegatingMetadataBuildingOptions.java index c34b9ad5d4..5a847b97f0 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/spi/AbstractDelegatingMetadataBuildingOptions.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/spi/AbstractDelegatingMetadataBuildingOptions.java @@ -8,9 +8,7 @@ package org.hibernate.boot.spi; import java.util.List; import jakarta.persistence.SharedCacheMode; - import org.hibernate.HibernateException; -import org.hibernate.MultiTenancyStrategy; import org.hibernate.TimeZoneStorageStrategy; import org.hibernate.annotations.common.reflection.ReflectionManager; import org.hibernate.boot.CacheRegionDefinition; @@ -24,7 +22,6 @@ import org.hibernate.boot.registry.StandardServiceRegistry; import org.hibernate.cache.spi.access.AccessType; import org.hibernate.cfg.MetadataSourceType; import org.hibernate.type.spi.TypeConfiguration; - import org.jboss.jandex.IndexView; /** @@ -127,8 +124,8 @@ public abstract class AbstractDelegatingMetadataBuildingOptions implements Metad } @Override - public MultiTenancyStrategy getMultiTenancyStrategy() { - return delegate.getMultiTenancyStrategy(); + public boolean isMultiTenancyEnabled() { + return delegate.isMultiTenancyEnabled(); } @Override diff --git a/hibernate-core/src/main/java/org/hibernate/boot/spi/AbstractDelegatingSessionFactoryBuilder.java b/hibernate-core/src/main/java/org/hibernate/boot/spi/AbstractDelegatingSessionFactoryBuilder.java index 29c6bf28e5..f577e2a39f 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/spi/AbstractDelegatingSessionFactoryBuilder.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/spi/AbstractDelegatingSessionFactoryBuilder.java @@ -13,7 +13,6 @@ import org.hibernate.ConnectionReleaseMode; import org.hibernate.CustomEntityDirtinessStrategy; import org.hibernate.EntityNameResolver; import org.hibernate.Interceptor; -import org.hibernate.MultiTenancyStrategy; import org.hibernate.SessionFactory; import org.hibernate.SessionFactoryObserver; import org.hibernate.boot.SessionFactoryBuilder; @@ -216,8 +215,8 @@ public abstract class AbstractDelegatingSessionFactoryBuilder (SessionFactoryImplementor) SessionFactoryRegistry.INSTANCE.findSessionFactory( + return () -> (SessionFactoryImplementor) SessionFactoryRegistry.INSTANCE.findSessionFactory( uuid, name );