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; * List<Document> documents;
* </pre> * </pre>
* <p> * <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 * applied when loading a collection of that entity type. This behavior is
* controlled by: * controlled by:
* <ol> * <ol>
@ -84,7 +84,7 @@ public @interface Where {
* <p> * <p>
* By default, the restriction is not applied unless the property * By default, the restriction is not applied unless the property
* {@value org.hibernate.cfg.AvailableSettings#USE_ENTITY_WHERE_CLAUSE_FOR_COLLECTIONS} * {@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 * @return {@code true} if the restriction should be applied even
* if the configuration property is not enabled * 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 * {@link jakarta.persistence.ManyToMany many-to-many} association whose target
* type defines the restriction. * type defines the restriction.
* <p> * <p>
* By default, the restriction is not applied. When this setting is enabled, the * By default, the restriction is applied. When this setting is disabled, the
* restriction is applied. * restriction is not applied.
* <p> * <p>
* The setting has no effect on a collection of {@link jakarta.persistence.Embeddable * The setting has no effect on a collection of {@link jakarta.persistence.Embeddable
* embeddable} values containing a {@link jakarta.persistence.ManyToOne many-to-one} * embeddable} values containing a {@link jakarta.persistence.ManyToOne many-to-one}

View File

@ -576,41 +576,55 @@ public class LoaderSelectBuilder {
TableGroup tableGroup, TableGroup tableGroup,
PluralAttributeMapping pluralAttributeMapping, PluralAttributeMapping pluralAttributeMapping,
SqlAstCreationState astCreationState) { SqlAstCreationState astCreationState) {
pluralAttributeMapping.applyBaseRestrictions( final NavigablePath parentNavigablePath = tableGroup.getNavigablePath().getParent();
querySpec::applyPredicate, if ( parentNavigablePath == null ) {
tableGroup, pluralAttributeMapping.applyBaseRestrictions(
true, querySpec::applyPredicate,
loadQueryInfluencers.getEnabledFilters(), tableGroup,
null, true,
astCreationState 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( assert pluralTableGroupJoin != null;
(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; pluralAttributeMapping.applyBaseRestrictions(
pluralTableGroupJoin.applyPredicate( filterPredicate ); pluralTableGroupJoin::applyPredicate,
} tableGroup,
}, true,
tableGroup, loadQueryInfluencers.getEnabledFilters(),
true, null,
loadQueryInfluencers.getEnabledFilters(), astCreationState
null, );
astCreationState pluralAttributeMapping.applyBaseManyToManyRestrictions(
); pluralTableGroupJoin::applyPredicate,
tableGroup,
true,
loadQueryInfluencers.getEnabledFilters(),
null,
astCreationState
);
}
} }
private void applyFiltering( private void applyFiltering(