Skip to content

Force join for comparisons in hql when not null entity key can represent null entity #2081

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 5 commits into from
Mar 31, 2020

Conversation

bahusoid
Copy link
Member

@bahusoid bahusoid commented Mar 21, 2019

Fixes #1274 (criteria is not fixed, but criteria doesn't support implicit joins. So in criteria it's up to user to properly create joins for such associations)

Add force join for associations with EntityType.IsNullable == true (like not constrained one-to-one or associations with not-found="ignore" mapping):

/// <summary>
/// True if not null entity key can represent null entity
/// (e.g. entity mapped with not-found="ignore" or not constrained one-to-one mapping)
/// </summary>
public abstract bool IsNullable { get; }

So when such property is used in comparisons like where alias.OneToOne is null we must add join and do comparison with joined table.

@hazzik hazzik force-pushed the nullableHqlJoin branch 2 times, most recently from b849b26 to e84c87f Compare April 15, 2019 02:07
@hazzik
Copy link
Member

hazzik commented Apr 15, 2019

Force pushed to resolve conflicts. The fixup commit got squashed.

@R0nd
Copy link

R0nd commented Oct 24, 2019

Can we get this reviewed?

Copy link
Member

@fredericDelaporte fredericDelaporte left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree for fixing the one-to-one non constrained case, where the foreign-key is (in true one-to-one cases) also the primary key and can never be null as such, and the the other side uses the foreign id generator, borrowing its primary key from the non-constrained entity.

But I disagree for the not-found ignore case, because it is detrimental for the general use-case, which is not to use this feature mainly meant for legacy DB with corrupted data. (Hibernate does not have this feature by the way, it is a NHibernate specific addition. I am not sure it is a good legacy to have added it.)

Adding joins for the general case, where foreign key are actual foreign keys, where a not-null foreign key can only mean the other side exists, is a bad move I think. So I think this PR should be adjusted to perform this change only for the one-to-one non-constrained case, and eventually for the case where a not-found ignore is detected.

@bahusoid
Copy link
Member Author

bahusoid commented Mar 30, 2020

Adding joins for the general case, where foreign key are actual foreign keys, where a not-null foreign key can only mean the other side exists, is a bad move

Just to be clear EntityType.IsNullable has nothing to do with not-null attribute. EntityType.IsNullable == true means not-null id can be treated as NULL entity for whatever reasons. And currently it's set for true for exactly two cases:

  1. not constrained one-to-one
  2. associations with not-found="ignore" mapping

/// <summary>
/// True if not null entity key can represent null entity
/// (e.g. entity mapped with not-found="ignore" or not constrained one-to-one mapping)
/// </summary>
public abstract bool IsNullable { get; }

Offtopic: It's strange I didn't get notification for your review. Just noticed accidentally that PR was recently updated.

@bahusoid bahusoid changed the title Force join for comparisons with nullable entity properties in hql Force join for comparisons where not null entity key can represent null entity Mar 31, 2020
@bahusoid bahusoid changed the title Force join for comparisons where not null entity key can represent null entity Force join for comparisons in hql when not null entity key can represent null entity Mar 31, 2020
@fredericDelaporte fredericDelaporte added this to the 5.3 milestone Mar 31, 2020
@fredericDelaporte fredericDelaporte merged commit 5cf0cf4 into nhibernate:master Mar 31, 2020
fredericDelaporte added a commit to fredericDelaporte/nhibernate-core that referenced this pull request Apr 4, 2020
georgi-yakimov pushed a commit to georgi-yakimov/nhibernate-core that referenced this pull request Apr 16, 2020
…ent null entity (nhibernate#2081)

Co-authored-by: Alexander Zaytsev <hazzik@gmail.com>
georgi-yakimov pushed a commit to georgi-yakimov/nhibernate-core that referenced this pull request Apr 16, 2020
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.

NH-3117 - Query on one-to-one property returns incorrect results
4 participants