HHH-15902 @OneToMany relationship with @Where on child table generates wrong sql

This commit is contained in:
Andrea Boriero 2023-01-11 17:46:54 +01:00 committed by Andrea Boriero
parent 3765837b9e
commit 7b308d048f
3 changed files with 51 additions and 37 deletions

View File

@ -48,7 +48,7 @@ import static java.lang.annotation.RetentionPolicy.RUNTIME;
* List<Document> documents;
* </pre>
* <p>
* By default, {@code @Where} restrictions declared for an entity are not
* By default, {@code @Where} restrictions declared for an entity are
* applied when loading a collection of that entity type. This behavior is
* controlled by:
* <ol>
@ -84,7 +84,7 @@ public @interface Where {
* <p>
* By default, the restriction is not applied unless the property
* {@value org.hibernate.cfg.AvailableSettings#USE_ENTITY_WHERE_CLAUSE_FOR_COLLECTIONS}
* is explicitly enabled.
* is explicitly disabled.
*
* @return {@code true} if the restriction should be applied even
* if the configuration property is not enabled

View File

@ -2041,8 +2041,8 @@ public interface AvailableSettings {
* {@link jakarta.persistence.ManyToMany many-to-many} association whose target
* type defines the restriction.
* <p>
* By default, the restriction is not applied. When this setting is enabled, the
* restriction is applied.
* By default, the restriction is applied. When this setting is disabled, the
* restriction is not applied.
* <p>
* The setting has no effect on a collection of {@link jakarta.persistence.Embeddable
* embeddable} values containing a {@link jakarta.persistence.ManyToOne many-to-one}

View File

@ -576,41 +576,55 @@ public class LoaderSelectBuilder {
TableGroup tableGroup,
PluralAttributeMapping pluralAttributeMapping,
SqlAstCreationState astCreationState) {
pluralAttributeMapping.applyBaseRestrictions(
querySpec::applyPredicate,
tableGroup,
true,
loadQueryInfluencers.getEnabledFilters(),
null,
astCreationState
);
final NavigablePath parentNavigablePath = tableGroup.getNavigablePath().getParent();
if ( parentNavigablePath == null ) {
pluralAttributeMapping.applyBaseRestrictions(
querySpec::applyPredicate,
tableGroup,
true,
loadQueryInfluencers.getEnabledFilters(),
null,
astCreationState
);
pluralAttributeMapping.applyBaseManyToManyRestrictions(
querySpec::applyPredicate,
tableGroup,
true,
loadQueryInfluencers.getEnabledFilters(),
null,
astCreationState
);
}
else {
final TableGroup parentTableGroup = astCreationState.getFromClauseAccess().getTableGroup(
parentNavigablePath );
TableGroupJoin pluralTableGroupJoin = null;
for ( TableGroupJoin nestedTableGroupJoin : parentTableGroup.getTableGroupJoins() ) {
if ( nestedTableGroupJoin.getNavigablePath() == tableGroup.getNavigablePath() ) {
pluralTableGroupJoin = nestedTableGroupJoin;
break;
}
}
pluralAttributeMapping.applyBaseManyToManyRestrictions(
(filterPredicate) -> {
final NavigablePath parentNavigablePath = tableGroup.getNavigablePath().getParent();
if ( parentNavigablePath == null ) {
querySpec.applyPredicate( filterPredicate );
}
else {
final TableGroup parentTableGroup = astCreationState.getFromClauseAccess().getTableGroup( parentNavigablePath );
TableGroupJoin pluralTableGroupJoin = null;
for ( TableGroupJoin nestedTableGroupJoin : parentTableGroup.getTableGroupJoins() ) {
if ( nestedTableGroupJoin.getNavigablePath() == tableGroup.getNavigablePath() ) {
pluralTableGroupJoin = nestedTableGroupJoin;
break;
}
}
assert pluralTableGroupJoin != null;
assert pluralTableGroupJoin != null;
pluralTableGroupJoin.applyPredicate( filterPredicate );
}
},
tableGroup,
true,
loadQueryInfluencers.getEnabledFilters(),
null,
astCreationState
);
pluralAttributeMapping.applyBaseRestrictions(
pluralTableGroupJoin::applyPredicate,
tableGroup,
true,
loadQueryInfluencers.getEnabledFilters(),
null,
astCreationState
);
pluralAttributeMapping.applyBaseManyToManyRestrictions(
pluralTableGroupJoin::applyPredicate,
tableGroup,
true,
loadQueryInfluencers.getEnabledFilters(),
null,
astCreationState
);
}
}
private void applyFiltering(