Skip to content

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

@CodyRude

Description

@CodyRude

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions