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

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

Closed
nhibernate-bot opened this issue Oct 12, 2017 · 2 comments · Fixed by #1755

Comments

@nhibernate-bot
Copy link
Collaborator

nhibernate-bot commented Oct 12, 2017

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

@fredericDelaporte
Copy link
Member

fredericDelaporte commented Jun 17, 2018

At time of ticket report, this was a documented known limitation.

Please note that a composite element mapping doesn't support null-able properties if you're using a <set>. NHibernate has to use each columns value to identify a record when deleting objects (there is no separate primary key column in the composite element table), which is not possible with null values. You have to either use only not-null properties in a composite-element or choose a <list>, <map>, <bag> or <idbag>.

The limitation has been removed in NH v4.1 with NH-3634. So ideally this delete case should now work, but I have not checked if it is covered by a test case.

By the way this ticket is a duplicate of NH-3151, which was closed as nullable component properties were not supported in sets at that time either.

fredericDelaporte added a commit to fredericDelaporte/nhibernate-core that referenced this issue Jun 17, 2018
Demonstrate nhibernate#1170 - Incorrect query when items removed from a collection
of components contain null values
@fredericDelaporte
Copy link
Member

Now there is a test, and it is failing. See #1755.

fredericDelaporte added a commit to fredericDelaporte/nhibernate-core that referenced this issue Jun 25, 2018
fredericDelaporte added a commit to fredericDelaporte/nhibernate-core that referenced this issue Jun 25, 2018
Demonstrate nhibernate#1170 - Incorrect query when items removed from a collection
of components contain null values
fredericDelaporte added a commit to fredericDelaporte/nhibernate-core that referenced this issue Jun 25, 2018
fredericDelaporte added a commit that referenced this issue Jul 4, 2018
Fixes #1170 - Incorrect query when items removed from a collection
of components contain null values
@fredericDelaporte fredericDelaporte added this to the 5.2 milestone Jul 4, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants