Skip to content

Fix #1994 (many-to-many filter in hql) #1995

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jun 9, 2019
Merged

Conversation

bahusoid
Copy link
Member

@bahusoid bahusoid commented Jan 27, 2019

Filter doesn't work properly for many-to-many association in hql queries #1994

QueryOver generated query (works correctly):

s.QueryOver<Asset>().Fetch(SelectMode.Fetch, x => x.Documents).List<Asset>();
SELECT this_.pdoid AS pdoid1_0_1_
	,this_.IsDeleted AS isdeleted2_0_1_
	,documents2_.AssetId AS assetid1_2_3_ -- COLLECTION PERSISTER
	,document3_.pdoid AS documentid2_2_3_ -- COLLECTION PERSISTER.  REPLACED documents2_.DocumentID. In LINQ query it's still documents2_.DocumentID
	,document3_.pdoid AS pdoid1_0_0_
	,document3_.IsDeleted AS isdeleted2_0_0_
FROM Asset this_
LEFT OUTER JOIN asset_to_document documents2_ ON this_.pdoid = documents2_.AssetId
LEFT OUTER JOIN Document document3_ ON documents2_.DocumentId = document3_.pdoid
	AND document3_.IsDeleted = @p0
WHERE this_.IsDeleted = @p0

Linq/hql generated query (wrong):

s.Query<Asset>().FetchMany(x => x.Documents).ToList();
SELECT asset0_.pdoid AS pdoid1_0_0_
	,document2_.pdoid AS pdoid1_0_1_-- ENTITY PERISTER, NULL if filtered
	,asset0_.IsDeleted AS isdeleted2_0_0_
	,document2_.IsDeleted AS isdeleted2_0_1_
	,documents1_.AssetId AS assetid1_2_0__  -- COLLECTION PERSISTER
	,documents1_.DocumentId AS documentid2_2_0__  -- COLLECTION PERSISTER, NOT NULL even if filtered  so collection still tries to load filtered element
FROM Asset asset0_
LEFT OUTER JOIN asset_to_document documents1_ ON asset0_.pdoid = documents1_.AssetId
LEFT OUTER JOIN Document document2_ ON documents1_.DocumentId = document2_.pdoid
	AND document2_.IsDeleted = @p0
WHERE asset0_.IsDeleted = @p0

As you can see in QueryOver case all document related fields in select statement are only from Document table with document3 alias. And in query case documents1_.DocumentId is added from bridge table which I believe leads to this bug.

Implemented fix: substitute alias for collection persister with entity perister for filtered many-to-many collections. So documents for collection are populated from filtered entity table instead of bridge table.

Fixes #1994

@hazzik

This comment has been minimized.

@bahusoid

This comment has been minimized.

@hazzik

This comment has been minimized.

@hazzik

This comment has been minimized.

@hazzik

This comment has been minimized.

@bahusoid

This comment has been minimized.

@bahusoid

This comment has been minimized.

@fredericDelaporte

This comment has been minimized.

@bahusoid

This comment has been minimized.

@fredericDelaporte

This comment has been minimized.

@bahusoid

This comment has been minimized.

@fredericDelaporte

This comment has been minimized.

@bahusoid

This comment has been minimized.

@fredericDelaporte

This comment has been minimized.

@bahusoid bahusoid changed the title Test case for #1994 (many-to-many filter in hql) Fix #1994 (many-to-many filter in hql) Apr 25, 2019
@bahusoid
Copy link
Member Author

bahusoid commented Apr 25, 2019

Found an easy fix for this issue: do not mess with SQL generation as it's done for QueryOver/Entity Loader and instead substitute aliases for collection persister with entity persister for filtered many-to-many collection.

@bahusoid bahusoid added t: Fix and removed t: Tests labels Apr 25, 2019
@hazzik
Copy link
Member

hazzik commented May 31, 2019

@bahusoid I made some changes, could you please verify?

@bahusoid
Copy link
Member Author

@hazzik looks good to me

@hazzik hazzik merged commit c4b806c into nhibernate:master Jun 9, 2019
@hazzik hazzik added the r: Fixed label Jun 9, 2019
@hazzik hazzik added this to the 5.3 milestone Jun 9, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Extra Select for every "outfiltered" Element
3 participants