Skip to content

NH-3646 - Incorrect query when items removed from a collection of components contain null values #1170

Closed
@nhibernate-bot

Description

@nhibernate-bot

CBP created an issue — 31st July 2014, 2:28:27:

I have an entity that has a collection of components mapped.

The Fluent NH mapping like this:

public sealed class FooDbMap : ClassMap<Foo>
{
	public FooDbMap()
	{
           ... etc

		HasMany(x => x.Bars)
			.Component(part =>
			           	{
			           		part.References(x => x.Qux, "QuxId").LazyLoad().Nullable();
			           		part.Map(x => x.Baz).Nullable();
			           	})
			.AsSet()
			.KeyColumn("FooId")
			.LazyLoad()
			.Table("sfFooBars");
        }
}

The component class looks like this:

public class Bar
{
	public virtual Qux Qux { get; set; }
	public virtual string Baz { get; set; }

	public bool Equals(Bar other)
	{
		if (ReferenceEquals(null, other)) return false;
		if (ReferenceEquals(this, other)) return true;
		return Equals(other.Qux, Qux) && Equals(other.Baz, Baz);
	}

	public override int GetHashCode()
	{
		unchecked
		{
			int result = (Qux != null ? Qux.GetHashCode() : 0);
			result = (result*397) ^ (Baz != null ? Baz.GetHashCode() : 0);
			return result;
		}
	}
}

The following test demonstrates the problem:

[Test]
public void Test()
{
    var foo = new Foo();
	
	foo.AddBar(new Bar 
		{
			Qux = null,
			Baz = "asdf"
		});
		
	CurrentSession.Save(foo);
	CurrentSession.Flush();
	CurrentSession.Clear();
	
	var reloadedFoo = CurrentSession.Load<Foo>();
	
	reloadedFoo.RemoveBar(reloadedFoo.Bars.First());
	
	CurrentSession.Flush();
}

This test fails to remove the bar.
The reason is because the generated SQL is incorrect:

exec sp_executesql N'DELETE FROM Foo WHERE FooId = @p0 AND Baz = @p1 AND Qux = @p2',N'@p0 uniqueidentifier,@p1 varchar(8000),@p2 uniqueidentifier',@p0='494464F4-6B1A-455F-8786-A37900C9CE36',@p1='asdf',@p2=NULL

The correct SQL should check if Qux is null using the syntax "Qux is null", rather than "Qux = @p2".


Alexander Zaytsev added a comment — 12th October 2014, 10:49:27:

A test case would be appreciated

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions