Skip to content

Commit

Permalink
HHH-16830 Add applyToLoadByKey filters also to to-one join predicates
Browse files Browse the repository at this point in the history
  • Loading branch information
beikov committed May 28, 2024
1 parent 3fa8ec6 commit 303979c
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -687,27 +687,25 @@ private void applyFiltering(
TableGroup tableGroup,
PluralAttributeMapping pluralAttributeMapping,
SqlAstCreationState astCreationState) {
final NavigablePath parentNavigablePath = tableGroup.getNavigablePath().getParent();
if ( parentNavigablePath == null ) {
// Only apply restrictions for root table groups,
// because for table group joins the restriction is applied via PluralAttributeMappingImpl.createTableGroupJoin
pluralAttributeMapping.applyBaseRestrictions(
querySpec::applyPredicate,
tableGroup,
true,
loadQueryInfluencers.getEnabledFilters(),
null,
astCreationState
);
pluralAttributeMapping.applyBaseManyToManyRestrictions(
querySpec::applyPredicate,
tableGroup,
true,
loadQueryInfluencers.getEnabledFilters(),
null,
astCreationState
);
}
// Only apply restrictions for root table groups,
// because for table group joins the restriction is applied via PluralAttributeMappingImpl.createTableGroupJoin
assert tableGroup.getNavigablePath().getParent() == null;
pluralAttributeMapping.applyBaseRestrictions(
querySpec::applyPredicate,
tableGroup,
true,
loadQueryInfluencers.getEnabledFilters(),
null,
astCreationState
);
pluralAttributeMapping.applyBaseManyToManyRestrictions(
querySpec::applyPredicate,
tableGroup,
true,
loadQueryInfluencers.getEnabledFilters(),
null,
astCreationState
);
}

private void applyFiltering(
Expand Down Expand Up @@ -967,35 +965,14 @@ else if ( fetchDepth > maximumFetchDepth + 1 ) {
if ( fetch.getTiming() == FetchTiming.IMMEDIATE && joined ) {
if ( isFetchablePluralAttributeMapping ) {
final PluralAttributeMapping pluralAttributeMapping = (PluralAttributeMapping) fetchable;
final TableGroup joinTableGroup = creationState.getFromClauseAccess()
.getTableGroup( fetchablePath );
final QuerySpec querySpec = creationState.getInflightQueryPart().getFirstQuerySpec();
applyFiltering(
querySpec,
joinTableGroup,
pluralAttributeMapping,
creationState
);
applyOrdering(
querySpec,
fetchablePath,
pluralAttributeMapping,
creationState
);
}
else if ( fetchable instanceof ToOneAttributeMapping ) {
final EntityMappingType entityType = ( (ToOneAttributeMapping) fetchable ).getEntityMappingType();
final FromClauseAccess fromClauseAccess = creationState.getFromClauseAccess();
final TableGroup joinTableGroup = fromClauseAccess.getTableGroup( fetchablePath );
final TableGroupJoin join = fromClauseAccess.getTableGroup( fetchParent.getNavigablePath() )
.findTableGroupJoin( joinTableGroup );
applyFiltering(
join,
joinTableGroup,
entityType,
creationState
);
}
}

fetches.add( fetch );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ protected SingleIdEntityLoaderStandardImpl(
public T load(Object key, LockOptions lockOptions, Boolean readOnly, SharedSessionContractImplementor session) {
final SingleIdLoadPlan<T> loadPlan = resolveLoadPlan(
lockOptions,
session.getLoadQueryInfluencers(),
session.getLoadQueryInfluencers().copyForLoadByKey(),
session.getFactory()
);
return loadPlan.load( key, readOnly, true, session );
Expand All @@ -83,7 +83,7 @@ public T load(
SharedSessionContractImplementor session) {
final SingleIdLoadPlan<T> loadPlan = resolveLoadPlan(
lockOptions,
session.getLoadQueryInfluencers(),
session.getLoadQueryInfluencers().copyForLoadByKey(),
session.getFactory()
);
return loadPlan.load( key, entityInstance, readOnly, false, session );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2096,7 +2096,18 @@ public TableGroupJoin createTableGroupJoin(
creationState
) );

// Note specifically we DO NOT apply `@Filter` restrictions
// Note specifically we only apply `@Filter` restrictions that are applyToLoadByKey = true
// to make the behavior consistent with lazy loading of an association
if ( getAssociatedEntityMappingType().getEntityPersister().hasFilterForLoadByKey() ) {
getAssociatedEntityMappingType().applyBaseRestrictions(
join::applyPredicate,
tableGroup,
true,
creationState.getLoadQueryInfluencers().copyForLoadByKey().getEnabledFilters(),
null,
creationState
);
}
getAssociatedEntityMappingType().applyWhereRestrictions(
join::applyPredicate,
tableGroup,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,21 @@ public void testApplyToLoadByKeyAssociationFiltering() {
// Account with id 1 does not exist
assertTrue( exception.getMessage().contains( "`1`" ) );
});
doInJPA(this::entityManagerFactory, entityManager -> {
entityManager.unwrap(Session.class)
.enableFilter("accountType")
.setParameter("type", "DEBIT");

FetchNotFoundException exception = assertThrows(
FetchNotFoundException.class,
() -> entityManager.createQuery(
"select a from Account a left join fetch a.parentAccount where a.id = 2",
Account.class
).getResultList()
);
// Account with id 1 does not exist
assertTrue( exception.getMessage().contains( "`1`" ) );
});
}

public enum AccountType {
Expand Down

0 comments on commit 303979c

Please sign in to comment.