-
Notifications
You must be signed in to change notification settings - Fork 931
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
NH generates incorrect SQL (ignoring property-ref mapping attribute) when querying a many-to-one relationship with a subquery that uses EXISTS(...) #3609
Comments
Changing |
Ah, thanks, you are right. Sadly, in our real app, the |
In order to start looking at this, I have done a couple of branches on 5.3.x. It requires fixing some vulnerable dependency troubles in order to be able to run this. Here are the branches:
|
Hi @fredericDelaporte . Thanks for this. I'll add a reminder to - probably - write a third test case which demonstrates the problem as seen on 5.3.16. I don't even recall now exactly what was wrong. I remember seeing that the test passed, but when I read the generated SQL there was still something wrong with it. Albeit it wasn't precisely the same/primary problem as reported in this issue. |
As requested in nhibernate/nhibernate-core#3609 (comment) this expands the test case so that it fails in v5.3.16 as well as 5.3.17+. It also adds some comments to the test pointing out why it fails on 5.3.16, and why that's for a different reason to the reason it fails in 5.3.17+.
@fredericDelaporte - I've updated the test case (and sample data). It was easier to expand the existing test than write a new one. The comments added to the test indicate why it fails on v5.3.16, but for a different reason to why it fails on 5.3.17+. It's all in this commit: craigfowler/NHibernate.PropertyRefBug@c4bbf8e Oops, in case you didn't spot, the "Apples" line item shouldn't be included in the results because the date is too early ;) I meant to point that out in comments but seem to have forgotten. |
I have found this issue whilst attempting to upgrade from NHibernate 5.3.7 to 5.5.2. This issue has caused us to abort the upgrade, as it causes a number of our app's queries to return incorrect results.
I have created a minimal reproduction case for the issue and published it in this git repository. I have also reproduced the README for it below, as that contains details about the issue.
NHibernate HBM
property-ref
bugThis is a reproduction case for an NHibernate bug whereby a
property-ref
attribute on a<many-to-one />
XML HBM mapping is incorrectly used when generating SQL.This appears to affect only queries which would cause an
EXISTS( ... )
-style SQL query to be generated by NHibernate.The issue differs a little depending upon whether or not the
<many-to-one />
mapping uses thenot-found="ignore"
attribute.The incorrect SQL appears within that subquery. It relates to the column from the table which is the main subject of the subquery, corresponding to the foreign key column of the table which has the many-to-one mapping. NHibernate generates SQL using the primary key column, where it should instead use the column corresponding to the property identified by the
property-ref
attribute.Versions affected
I have tried this out against a variety of NHibernate versions.
In our apps, where we are affected by this issue, we have also been using
not-found="ignore"
on the same mappings.Whilst investigating/creating the reproduction case, I discovered that if I remove the
not-found
attribute from the mappings then I can reproduce this problem even in version 5.3.7.But - for the scenario which affects us, when we are using
not-found="ignore"
:It seems like 5.3.17 along with 5.4.3 are the first affected versions.
Muddying the picture a little, there is another related issue, predating this, affecting versions 5.3.16 & 5.4.2 only. See below for more info.
This means that if we wish to see a sample of 'good' SQL for the query, we must use 5.3.15 or 5.4.1, or earlier.
Another related issue, predating this
There is a separate-but-related issue to this which affects NH 5.3.16 and 5.4.2.
That issue might be #3269 but I can't be 100% certain just from a read of the ticket.
The related issue means that the test case included in this repository will pass on those two versions, but NHibernate would still generate incorrect SQL, causing other problems.
It's best to consider the "last good" versions to be 5.3.15 & 5.4.1 with regard to the issue that this repo demonstrates.
Sample reproduction case
There are two mapped classes in this solution, including a mapping which reproduces this issue:
not-found="ignore"
property-ref="..."
One of the tests included in the solution executes a query which triggers generation of a SQL
EXISTS(...)
subquery.This solution uses SQLite in-memory driver/dialect so as to be self-contained.
As far as I can tell the DB driver/dialect is irrelevant though, because we reproduced this in an MS SQL Server environment.
To run this reproduction case:
dotnet test
NhVersion
property to the desired NHibernate versiondotnet test /p:NhVersion=5.4.9
The mappings include an interceptor which will capture and emit the SQL which is generated to STDERR.
This is to help illustrate/diagnose the problem.
Expected behaviour
Both unit tests should pass; the queries performed by the tests are querying against known data which has been added to the in-memory DB.
Actual behaviour
The unit test for the subquery fails.
Further info/analysis
Note that in passing scenarios (NH version 5.3.15) the SQL emitted by the interceptor is as follows.
However in failing scenarios (NH >= 5.3.17) the SQL emitted by the interceptor is as follows. The problematic area is pointed out in a comment.
The text was updated successfully, but these errors were encountered: