diff --git a/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java b/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java index 7bb60f1920..0d2b2b3ed4 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java @@ -701,10 +701,7 @@ public final class SessionFactoryImpl this.typeResolver = metadata.getTypeResolver().scope( this ); this.typeHelper = new TypeLocatorImpl( typeResolver ); - this.filters = new HashMap(); - for ( FilterDefinition filterDefinition : metadata.getFilterDefinitions() ) { - filters.put( filterDefinition.getFilterName(), filterDefinition ); - } + this.filters = Collections.unmodifiableMap( metadata.getFilterDefinitions() ); LOG.debugf( "Session factory constructed with filter configurations : %s", filters ); LOG.debugf( "Instantiating session factory with properties: %s", properties ); diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/Metadata.java b/hibernate-core/src/main/java/org/hibernate/metamodel/Metadata.java index b1826e7173..d57d86d6b3 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/Metadata.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/Metadata.java @@ -114,7 +114,7 @@ public interface Metadata { public Iterable getTypeDefinitions(); - public Iterable getFilterDefinitions(); + public Map getFilterDefinitions(); public Iterable getNamedQueryDefinitions(); diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/Binder.java b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/Binder.java index be4f26a7c5..eea99beb89 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/Binder.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/Binder.java @@ -48,6 +48,7 @@ import org.hibernate.engine.FetchTiming; import org.hibernate.engine.config.spi.ConfigurationService; import org.hibernate.engine.spi.CascadeStyle; import org.hibernate.engine.spi.CascadeStyles; +import org.hibernate.engine.spi.FilterDefinition; import org.hibernate.id.EntityIdentifierNature; import org.hibernate.id.IdentifierGenerator; import org.hibernate.id.IdentityGenerator; @@ -387,15 +388,7 @@ public class Binder { resolveEntityLaziness( entityBinding, entitySource ); if ( entitySource.getFilterSources() != null ) { for ( FilterSource filterSource : entitySource.getFilterSources() ) { - FilterConfiguration filterConfiguration = new FilterConfiguration( - filterSource.getName(), - filterSource.getCondition(), - filterSource.shouldAutoInjectAliases(), - filterSource.getAliasToTableMap(), - filterSource.getAliasToEntityMap(), - entityBinding - ); - entityBinding.addFilterConfiguration( filterConfiguration ); + entityBinding.addFilterConfiguration( createFilterConfiguration( filterSource, entityBinding ) ); } } // Register binding with metadata @@ -403,6 +396,26 @@ public class Binder { return entityBinding; } + private FilterConfiguration createFilterConfiguration(FilterSource filterSource, EntityBinding entityBinding){ + String condition = filterSource.getCondition(); + if(StringHelper.isEmpty( condition )){ + FilterDefinition filterDefinition = metadata.getFilterDefinitions().get( filterSource.getName() ); + if(filterDefinition == null){ + throw bindingContext().makeMappingException( String.format( "Filter[$s] doesn't have a condition", filterSource.getName() ) ); + } + condition = filterDefinition.getDefaultFilterCondition(); + } + FilterConfiguration filterConfiguration = new FilterConfiguration( + filterSource.getName(), + condition, + filterSource.shouldAutoInjectAliases(), + filterSource.getAliasToTableMap(), + filterSource.getAliasToEntityMap(), + entityBinding + ); + return filterConfiguration; + } + private void resolveEntityLaziness( final EntityBinding entityBinding, final EntitySource entitySource) { @@ -1354,15 +1367,7 @@ public class Binder { if ( attributeSource.getFilterSources() != null ) { for ( final FilterSource filterSource : attributeSource.getFilterSources() ) { - FilterConfiguration filterConfiguration = new FilterConfiguration( - filterSource.getName(), - filterSource.getCondition(), - filterSource.shouldAutoInjectAliases(), - filterSource.getAliasToTableMap(), - filterSource.getAliasToEntityMap(), - attributeBindingContainer.seekEntityBinding() - ); - attributeBinding.addFilterConfiguration( filterConfiguration ); + attributeBinding.addFilterConfiguration( createFilterConfiguration( filterSource, attributeBindingContainer.seekEntityBinding() ) ); } } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/MetadataImpl.java b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/MetadataImpl.java index 13b03c5296..f117a31924 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/MetadataImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/MetadataImpl.java @@ -320,9 +320,9 @@ public class MetadataImpl implements MetadataImplementor, Serializable { } filterDefinitionMap.put( filterDefinition.getFilterName(), filterDefinition ); } - - public Iterable getFilterDefinitions() { - return filterDefinitionMap.values(); + @Override + public Map getFilterDefinitions() { + return filterDefinitionMap; }