nhibernate-core icon indicating copy to clipboard operation
nhibernate-core copied to clipboard

problem with join with withClause in case of inheritance

Open georgi-yakimov opened this issue 5 years ago • 16 comments

Fixes #2330

georgi-yakimov avatar Mar 19 '20 08:03 georgi-yakimov

Thanks! Please add tests too.

gliljas avatar Mar 19 '20 08:03 gliljas

hi @gliljas what do I need in order to run the tests locally? I have both nunit 2 and 3 installed. For some reason the tests are not getting started. Where I can read more about the unit tests practices you're following?

georgi-yakimov avatar Mar 19 '20 09:03 georgi-yakimov

NUnit 3 is what is used. Personally I use the ReSharper test runner, but the one built into VS should work too. You should run the ShowBuildMenu.bat file in the project root.

gliljas avatar Mar 19 '20 10:03 gliljas

The issue is not yet finished. I have to add unit tests.

georgi-yakimov avatar Mar 19 '20 20:03 georgi-yakimov

Hi can someone please check what's going on with the travis? It's been in waiting status since the weekend? Thanks in advance

georgi-yakimov avatar Apr 14 '20 14:04 georgi-yakimov

This sometimes happens, and usually pushing new commit solves it. As anyway you have a conflict to resolve and new issues in CodeFactor, you have some commits to add (eventually with a rebase), so let's see if the issue remains after that.

fredericDelaporte avatar Apr 14 '20 17:04 fredericDelaporte

Hi. continuous-integration/appveyor/pr is failing and I can't figure out the reason why with looking at the log. Is it possible that the failure is not due to my changes?

georgi-yakimov avatar Apr 15 '20 21:04 georgi-yakimov

@georgi-yakimov in your issue which you have originally opened you are talking about HQL, but it seems this PR concerns only Criteria API. Can you please clarify?

hazzik avatar Apr 15 '20 22:04 hazzik

hi @hazzik the problem is that when there's inheritance join with a withClause and the column which is used in the withClause is present in the base and the inherited table the condition is not generated correctly. The table that is considered in the with clause is from the base table which is not yet joined. So instead of

select t1.*
from SomeTable t1
join Child c_1 on c_1.FK_Root_Id = t1.FK_Child_Id and c_1.IsActive = 1
join Root c_1_1 on c_1_1.Id = c_1.FK_Root_Id

the result is

select t1.*
from SomeTable t1
join Child c_1 on c_1.FK_Root_Id = t1.FK_Child_Id and c_1_1.IsActive = 1 -- c_1_1 is not joined yet
join Root c_1_1 on c_1_1.Id = c_1.FK_Root_Id

You can check the unit tests I've added for more details

georgi-yakimov avatar Apr 16 '20 07:04 georgi-yakimov

This fix with use first/last index looks more like workaround for some real core issue. Can you explain more what's behind this fix?

Also I tried to convert your query to QueryOver which is basically a typed wrapper around Criteria and for some reasons your fix is no longer working:

PersonBase f = null;
var results =
	session.QueryOver<UserEntityVisit>()
			.JoinAlias(
				x => x.PersonBase,
				() => f,
				JoinType.LeftOuterJoin,
				Restrictions.Where(() => f.Deleted == false))
			.List();

bahusoid avatar Apr 16 '20 09:04 bahusoid

Also I might be wrong but it seems some of analysis described here https://github.com/nhibernate/nhibernate-core/issues/2094 might be related to your issue

bahusoid avatar Apr 16 '20 09:04 bahusoid

hey, could somebody please take a look why a required check is hanging for like a week by now? Thanks in advance

georgi-yakimov avatar May 08 '20 07:05 georgi-yakimov

This really looks like a workaround. It is easy to break this logic.

If you add a subclass of PersonBase with Deleted property, then it will be broken.

The issue here is much deeper: only properties of the class itself are supported in the "with" clause.

hazzik avatar May 12 '20 01:05 hazzik

#2330 is actually also fixed by table group joins (#2545 for Criteria and #2361 for hql). So it seems this PR can be closed as replaced

So problematic SQL below (taken from problem description):

select t1.*
from SomeTable t1
join Child c_1 on c_1.FK_Root_Id = t1.FK_Child_Id and c_1_1.IsActive = 1 -- c_1_1 is not joined yet
join Root c_1_1 on c_1_1.Id = c_1.FK_Root_Id

Become after fix:

select t1.*
from SomeTable t1
join 
   (Child c_1  join Root c_1_1 on c_1_1.Id = c_1.FK_Root_Id)  on c_1.FK_Root_Id = t1.FK_Child_Id and c_1_1.IsActive = 1

bahusoid avatar Oct 23 '20 11:10 bahusoid

After having undone the fix in this PR, its tests work locally. So I have pushed the undo to check this with the CI.

fredericDelaporte avatar Oct 24 '20 17:10 fredericDelaporte

So the test case alone without any change to NHibernate current master branch succeeds.

fredericDelaporte avatar Nov 01 '20 17:11 fredericDelaporte