HHH-17121 Nullability improvements to LoadQueryInfluencers

This commit is contained in:
Christian Beikov 2023-10-19 11:54:28 +02:00
parent 40a5c0059e
commit ffee08d853
5 changed files with 12 additions and 37 deletions

View File

@ -23,7 +23,6 @@ import org.hibernate.graph.GraphSemantic;
import org.hibernate.graph.spi.RootGraphImplementor; import org.hibernate.graph.spi.RootGraphImplementor;
import org.hibernate.internal.FilterImpl; import org.hibernate.internal.FilterImpl;
import org.hibernate.internal.SessionCreationOptions; import org.hibernate.internal.SessionCreationOptions;
import org.hibernate.internal.util.NullnessUtil;
import org.hibernate.loader.ast.spi.CascadingFetchProfile; import org.hibernate.loader.ast.spi.CascadingFetchProfile;
import org.hibernate.persister.collection.CollectionPersister; import org.hibernate.persister.collection.CollectionPersister;
import org.hibernate.persister.entity.EntityPersister; import org.hibernate.persister.entity.EntityPersister;
@ -44,17 +43,8 @@ import static org.hibernate.engine.FetchStyle.SUBSELECT;
* @author Steve Ebersole * @author Steve Ebersole
*/ */
public class LoadQueryInfluencers implements Serializable { public class LoadQueryInfluencers implements Serializable {
/**
* Static reference useful for cases where we are creating load SQL
* outside the context of any influencers. One such example is
* anything created by the session factory.
*
* @deprecated use {@link #LoadQueryInfluencers(SessionFactoryImplementor)}
*/
@Deprecated(forRemoval = true)
public static final LoadQueryInfluencers NONE = new LoadQueryInfluencers();
private final @Nullable SessionFactoryImplementor sessionFactory; private final SessionFactoryImplementor sessionFactory;
private CascadingFetchProfile enabledCascadingFetchProfile; private CascadingFetchProfile enabledCascadingFetchProfile;
@ -72,10 +62,6 @@ public class LoadQueryInfluencers implements Serializable {
private Boolean readOnly; private Boolean readOnly;
public LoadQueryInfluencers() {
this.sessionFactory = null;
}
public LoadQueryInfluencers(SessionFactoryImplementor sessionFactory) { public LoadQueryInfluencers(SessionFactoryImplementor sessionFactory) {
this.sessionFactory = sessionFactory; this.sessionFactory = sessionFactory;
batchSize = sessionFactory.getSessionFactoryOptions().getDefaultBatchFetchSize(); batchSize = sessionFactory.getSessionFactoryOptions().getDefaultBatchFetchSize();
@ -99,7 +85,7 @@ public class LoadQueryInfluencers implements Serializable {
return effectiveEntityGraph; return effectiveEntityGraph;
} }
public @Nullable SessionFactoryImplementor getSessionFactory() { public SessionFactoryImplementor getSessionFactory() {
return sessionFactory; return sessionFactory;
} }
@ -135,7 +121,6 @@ public class LoadQueryInfluencers implements Serializable {
* Set the effective {@linkplain #getEnabledCascadingFetchProfile() cascading fetch-profile} * Set the effective {@linkplain #getEnabledCascadingFetchProfile() cascading fetch-profile}
*/ */
public void setEnabledCascadingFetchProfile(CascadingFetchProfile enabledCascadingFetchProfile) { public void setEnabledCascadingFetchProfile(CascadingFetchProfile enabledCascadingFetchProfile) {
checkMutability();
this.enabledCascadingFetchProfile = enabledCascadingFetchProfile; this.enabledCascadingFetchProfile = enabledCascadingFetchProfile;
} }
@ -199,8 +184,7 @@ public class LoadQueryInfluencers implements Serializable {
} }
public Filter enableFilter(String filterName) { public Filter enableFilter(String filterName) {
checkMutability(); FilterImpl filter = new FilterImpl( sessionFactory.getFilterDefinition( filterName ) );
FilterImpl filter = new FilterImpl( NullnessUtil.castNonNull( sessionFactory ).getFilterDefinition( filterName ) );
if ( enabledFilters == null ) { if ( enabledFilters == null ) {
this.enabledFilters = new HashMap<>(); this.enabledFilters = new HashMap<>();
} }
@ -250,7 +234,7 @@ public class LoadQueryInfluencers implements Serializable {
} }
private void checkFetchProfileName(String name) { private void checkFetchProfileName(String name) {
if ( sessionFactory != null && !sessionFactory.containsFetchProfileDefinition( name ) ) { if ( !sessionFactory.containsFetchProfileDefinition( name ) ) {
throw new UnknownProfileException( name ); throw new UnknownProfileException( name );
} }
} }
@ -261,7 +245,6 @@ public class LoadQueryInfluencers implements Serializable {
} }
public void enableFetchProfile(String name) throws UnknownProfileException { public void enableFetchProfile(String name) throws UnknownProfileException {
checkMutability();
checkFetchProfileName( name ); checkFetchProfileName( name );
if ( enabledFetchProfileNames == null ) { if ( enabledFetchProfileNames == null ) {
this.enabledFetchProfileNames = new HashSet<>(); this.enabledFetchProfileNames = new HashSet<>();
@ -368,14 +351,6 @@ public class LoadQueryInfluencers implements Serializable {
return false; return false;
} }
private void checkMutability() {
if ( sessionFactory == null ) {
// that's the signal that this is the immutable, context-less
// variety
throw new IllegalStateException( "Cannot modify context-less LoadQueryInfluencers" );
}
}
public boolean hasSubselectLoadableCollections(EntityPersister persister) { public boolean hasSubselectLoadableCollections(EntityPersister persister) {
return persister.hasSubselectLoadableCollections() return persister.hasSubselectLoadableCollections()
|| subselectFetchEnabled && persister.hasCollections() || subselectFetchEnabled && persister.hasCollections()

View File

@ -58,13 +58,13 @@ public class LoadPlanBuilderTest {
final SingleIdLoadPlan<?> loadPlan = loader.resolveLoadPlan( final SingleIdLoadPlan<?> loadPlan = loader.resolveLoadPlan(
LockOptions.READ, LockOptions.READ,
LoadQueryInfluencers.NONE, new LoadQueryInfluencers( sessionFactory ),
sessionFactory sessionFactory
); );
final List<DomainResult<?>> domainResults = loadPlan.getJdbcSelect() final List<DomainResult<?>> domainResults = loadPlan.getJdbcSelect()
.getJdbcValuesMappingProducer() .getJdbcValuesMappingProducer()
.resolve( null, LoadQueryInfluencers.NONE, sessionFactory ) .resolve( null, new LoadQueryInfluencers( sessionFactory ), sessionFactory )
.getDomainResults(); .getDomainResults();
assertThat( domainResults ).hasSize( 1 ); assertThat( domainResults ).hasSize( 1 );
@ -91,7 +91,7 @@ public class LoadPlanBuilderTest {
final SingleIdEntityLoaderStandardImpl<?> loader = new SingleIdEntityLoaderStandardImpl<>( entityDescriptor, sessionFactory ); final SingleIdEntityLoaderStandardImpl<?> loader = new SingleIdEntityLoaderStandardImpl<>( entityDescriptor, sessionFactory );
final LoadQueryInfluencers influencers = new LoadQueryInfluencers() { final LoadQueryInfluencers influencers = new LoadQueryInfluencers( sessionFactory ) {
@Override @Override
public CascadingFetchProfile getEnabledCascadingFetchProfile() { public CascadingFetchProfile getEnabledCascadingFetchProfile() {
return CascadingFetchProfile.MERGE; return CascadingFetchProfile.MERGE;
@ -105,7 +105,7 @@ public class LoadPlanBuilderTest {
); );
final List<DomainResult<?>> domainResults = loadPlan.getJdbcSelect() final List<DomainResult<?>> domainResults = loadPlan.getJdbcSelect()
.getJdbcValuesMappingProducer() .getJdbcValuesMappingProducer()
.resolve( null, LoadQueryInfluencers.NONE, sessionFactory ) .resolve( null, new LoadQueryInfluencers( sessionFactory ), sessionFactory )
.getDomainResults(); .getDomainResults();
assertThat( domainResults ).hasSize( 1 ); assertThat( domainResults ).hasSize( 1 );
@ -135,7 +135,7 @@ public class LoadPlanBuilderTest {
final CollectionLoaderSingleKey loader = new CollectionLoaderSingleKey( final CollectionLoaderSingleKey loader = new CollectionLoaderSingleKey(
messages, messages,
LoadQueryInfluencers.NONE, new LoadQueryInfluencers( sessionFactory ),
sessionFactory sessionFactory
); );

View File

@ -73,7 +73,7 @@ public class MappedFetchTests {
rootEntityDescriptor.getIdentifierMapping(), rootEntityDescriptor.getIdentifierMapping(),
null, null,
1, 1,
LoadQueryInfluencers.NONE, new LoadQueryInfluencers( sessionFactory ),
LockOptions.NONE, LockOptions.NONE,
jdbcParameter -> { jdbcParameter -> {
}, },

View File

@ -195,7 +195,7 @@ public class EntityJoinTest {
QueryOptions.NONE, QueryOptions.NONE,
DomainParameterXref.empty(), DomainParameterXref.empty(),
QueryParameterBindings.NO_PARAM_BINDINGS, QueryParameterBindings.NO_PARAM_BINDINGS,
LoadQueryInfluencers.NONE, new LoadQueryInfluencers( factory ),
factory, factory,
true true
) )

View File

@ -35,7 +35,7 @@ public class AbstractResultTests {
QueryOptions.NONE, QueryOptions.NONE,
DomainParameterXref.from( sqm ), DomainParameterXref.from( sqm ),
parameterBindings, parameterBindings,
LoadQueryInfluencers.NONE, new LoadQueryInfluencers( sessionFactory ),
sessionFactory, sessionFactory,
true true
) )