Skip to content
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

Query issues when using not-found='ignore' in entity mapping #2988

Closed
CodyRude opened this issue Jan 13, 2022 · 3 comments
Closed

Query issues when using not-found='ignore' in entity mapping #2988

CodyRude opened this issue Jan 13, 2022 · 3 comments

Comments

@CodyRude
Copy link

There was a previous fix to queries for entites that use the "not-found='ignore'" mapping that worked prior to 5.3. That fixed an issue when there was a single null check, but there are still some other edge cases that aren't being handled correctly.

The first is when querying for null or another entity. For example, giving two entities:

public class Human
{
    public virtual int Id { get; protected set; }
    public virtual string Name { get; set; }
}

public class Dog
{
    public virtual int Id { get; protected set; }
    public virtual string Name { get; set; }
}

Giving the mapping

public class DogMap : ClassMap<Dog>
{
    public DogMap()
    {
        Id(x => x.Id);
        Map(x => x.Name)
            .Length(16)
            .Not.Nullable();
        References(x => x.Human)
            .NotFound.Ignore();
    }
}

And data:

Fido -> Joe (deleted)
Buddy -> Jane

and we query

session.Query<Dog>().Where(d => d.Human == null || d.Human.Id == janeId).Select(d => d.Name)

Only one dog with the human that still exists will show up due to an implicit comma join:

select
	dog0_.Name as col_0_0_
from
	"Dog" dog0_
left outer join
	 "Human" human1_ on dog0_.Human_id=human1_.Id,
	 "Human" human2_
where
	dog0_.Human_id=human2_.Id and (human1_.Id is null or human2_.Id=@p0);
@p0 = 2 [Type: Int32 (0:0:0)]

The second issue is when you try and query for the deleted id directly:

session.Query<Dog>().Where(d => d.Human.Id == joeId).Select(d => d.Name)

This used to bring back dogs whose owner is the now deleted Joe. This seems to also be due to the implicit comma join

select
	dog0_.Name as col_0_0_
from
	"Dog" dog0_,
	"Human" human1_
where
	dog0_.Human_id=human1_.Id and human1_.Id=@p0;
@p0 = 1 [Type: Int32 (0:0:0)]

Interestingly enough, after Joe has been removed, and you remove the NotFound.Ignore property on the dog mapping, then the query using Joe's old id works. This seems like it may be a workaround if you don't need to delete new entities.

I've attached a minimal working example using NHibernate 5.3.10 and FluentNhibernate that demonstrates these issues
NHibernateTest.zip

@bahusoid
Copy link
Member

The second issue is when you try and query for the deleted id directly:

First issue should be fixed by #2989. And the second issue - current behavior looks correct to me. As you directly instructed to ignore not found records so they are treated as NULL also in queries and only query by valid ID or by NULL can be found.

@CodyRude
Copy link
Author

Awesome, thanks for the quick response and fix!

@fredericDelaporte
Copy link
Member

Fixed by #2989.

@fredericDelaporte fredericDelaporte changed the title More query issues when using not-found='ignore' in entity mapping Query issues when using not-found='ignore' in entity mapping Feb 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants