boolean shallow) throws HibernateException {
if ( collection == null ) {
throw new NullPointerException( "null collection passed to filter" );
}
CollectionEntry entry = persistenceContext.getCollectionEntryOrNull( collection );
final CollectionPersister roleBeforeFlush = (entry == null) ? null : entry.getLoadedPersister();
FilterQueryPlan plan = null;
if ( roleBeforeFlush == null ) {
// if it was previously unreferenced, we need to flush in order to
// get its state into the database in order to execute query
flush();
entry = persistenceContext.getCollectionEntryOrNull( collection );
CollectionPersister roleAfterFlush = (entry == null) ? null : entry.getLoadedPersister();
if ( roleAfterFlush == null ) {
throw new QueryException( "The collection was unreferenced" );
}
plan = factory.getQueryPlanCache().getFilterQueryPlan( filter, roleAfterFlush.getRole(), shallow, getEnabledFilters() );
}
else {
// otherwise, we only need to flush if there are in-memory changes
// to the queried tables
plan = factory.getQueryPlanCache().getFilterQueryPlan( filter, roleBeforeFlush.getRole(), shallow, getEnabledFilters() );
if ( autoFlushIfRequired( plan.getQuerySpaces() ) ) {
// might need to run a different filter entirely after the flush
// because the collection role may have changed
entry = persistenceContext.getCollectionEntryOrNull( collection );
CollectionPersister roleAfterFlush = (entry == null) ? null : entry.getLoadedPersister();
if ( roleBeforeFlush != roleAfterFlush ) {
if ( roleAfterFlush == null ) {
throw new QueryException( "The collection was dereferenced" );
}
plan = factory.getQueryPlanCache().getFilterQueryPlan( filter, roleAfterFlush.getRole(), shallow, getEnabledFilters() );
}
}
}
if ( parameters != null ) {
parameters.getPositionalParameterValues()[0] = entry.getLoadedKey();
parameters.getPositionalParameterTypes()[0] = entry.getLoadedPersister().getKeyType();
}
return plan;
}