Skip to content

Commit c87194c

Browse files
Narosdreab8
authored andcommitted
HHH-11410 - Fixed ManyToMany with an active Filter to apply the condition on the join.
1 parent 10fe238 commit c87194c

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

hibernate-core/src/main/java/org/hibernate/loader/plan/exec/internal/LoadQueryJoinAndFetchProcessor.java

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
* </ol>
6161
*
6262
* @author Steve Ebersole
63+
* @author Chris Cranford
6364
*/
6465
public class LoadQueryJoinAndFetchProcessor {
6566
private static final Logger LOG = CoreLogging.logger( LoadQueryJoinAndFetchProcessor.class );
@@ -182,7 +183,8 @@ private void renderEntityJoin(Join join, JoinFragment joinFragment) {
182183
addJoins(
183184
join,
184185
joinFragment,
185-
joinable
186+
joinable,
187+
null
186188
);
187189
}
188190

@@ -223,8 +225,8 @@ else if ( StringHelper.isNotEmpty( withClause ) && StringHelper.isNotEmpty( filt
223225
private void addJoins(
224226
Join join,
225227
JoinFragment joinFragment,
226-
Joinable joinable) {
227-
228+
Joinable joinable,
229+
String joinConditions) {
228230
final String rhsTableAlias = aliasResolutionContext.resolveSqlTableAliasFromQuerySpaceUid(
229231
join.getRightHandSide().getUid()
230232
);
@@ -239,10 +241,18 @@ private void addJoins(
239241
throw new IllegalStateException( "QuerySpace with that UID was not yet registered in the AliasResolutionContext" );
240242
}
241243

244+
String otherConditions = join.getAnyAdditionalJoinConditions( rhsTableAlias );
245+
if ( !StringHelper.isEmpty( otherConditions ) && !StringHelper.isEmpty( joinConditions ) ) {
246+
otherConditions += " and " + joinConditions;
247+
}
248+
else if ( !StringHelper.isEmpty( joinConditions ) ) {
249+
otherConditions = joinConditions;
250+
}
251+
242252
// add join fragments from the collection table -> element entity table ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
243253
final String additionalJoinConditions = resolveAdditionalJoinCondition(
244254
rhsTableAlias,
245-
join.getAnyAdditionalJoinConditions( rhsTableAlias ),
255+
otherConditions,
246256
joinable,
247257
getJoinedAssociationTypeOrNull( join )
248258
);
@@ -354,7 +364,8 @@ private void renderCollectionJoin(Join join, JoinFragment joinFragment) {
354364
addJoins(
355365
join,
356366
joinFragment,
357-
(Joinable) rightHandSide.getCollectionPersister()
367+
(Joinable) rightHandSide.getCollectionPersister(),
368+
null
358369
);
359370
}
360371

@@ -378,21 +389,26 @@ private void renderManyToManyJoin(
378389
if ( StringHelper.isEmpty( entityTableAlias ) ) {
379390
throw new IllegalStateException( "Collection element (many-to-many) table alias cannot be empty" );
380391
}
392+
393+
final String manyToManyFilter;
381394
if ( JoinDefinedByMetadata.class.isInstance( join ) &&
382395
CollectionPropertyNames.COLLECTION_ELEMENTS.equals( ( (JoinDefinedByMetadata) join ).getJoinedPropertyName() ) ) {
383396
final CollectionQuerySpace leftHandSide = (CollectionQuerySpace) join.getLeftHandSide();
384397
final CollectionPersister persister = leftHandSide.getCollectionPersister();
385-
final String manyToManyFilter = persister.getManyToManyFilterFragment(
398+
manyToManyFilter = persister.getManyToManyFilterFragment(
386399
entityTableAlias,
387400
buildingParameters.getQueryInfluencers().getEnabledFilters()
388401
);
389-
joinFragment.addCondition( manyToManyFilter );
402+
}
403+
else {
404+
manyToManyFilter = null;
390405
}
391406

392407
addJoins(
393408
join,
394409
joinFragment,
395-
(Joinable) entityPersister
410+
(Joinable) entityPersister,
411+
manyToManyFilter
396412
);
397413
}
398414

0 commit comments

Comments
 (0)