Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions src/NHibernate.Test/Async/NHSpecificTest/GH2627/Fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by AsyncGenerator.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------


using System.Linq;
using NUnit.Framework;
using NHibernate.Linq;

namespace NHibernate.Test.NHSpecificTest.GH2627
{
using System.Threading.Tasks;
[TestFixture]
public class FixtureAsync : BugTestCase
{
protected override void OnSetUp()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
var e1 = new Entity {Name = "Bob"};
session.Save(e1);

transaction.Commit();
}
}

protected override void OnTearDown()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
session.CreateQuery("delete from Child").ExecuteUpdate();
session.CreateQuery("delete from System.Object").ExecuteUpdate();

transaction.Commit();
}
}

[Test]
public async Task NullRefInMergeAsync()
{
Child child;
using (var session = OpenSession())
using (var t = session.BeginTransaction())
{
child = new Child
{
Parent = await (session.Query<Entity>().FirstOrDefaultAsync()),
Name = "John"
};

await (t.CommitAsync());
}

using (var session = OpenSession())
using (var t = session.BeginTransaction())
{
await (session.MergeAsync(child));
await (t.CommitAsync());
}
}
}
}
11 changes: 11 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH2627/Child.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;

namespace NHibernate.Test.NHSpecificTest.GH2627
{
public class Child
{
public virtual Guid Id { get; set; }
public virtual Entity Parent { get; set; }
public virtual string Name { get; set; }
}
}
10 changes: 10 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH2627/Entity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;

namespace NHibernate.Test.NHSpecificTest.GH2627
{
public class Entity
{
public virtual Guid Id { get; set; }
public virtual string Name { get; set; }
}
}
57 changes: 57 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH2627/Fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System.Linq;
using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.GH2627
{
[TestFixture]
public class Fixture : BugTestCase
{
protected override void OnSetUp()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
var e1 = new Entity {Name = "Bob"};
session.Save(e1);

transaction.Commit();
}
}

protected override void OnTearDown()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
session.CreateQuery("delete from Child").ExecuteUpdate();
session.CreateQuery("delete from System.Object").ExecuteUpdate();

transaction.Commit();
}
}

[Test]
public void NullRefInMerge()
{
Child child;
using (var session = OpenSession())
using (var t = session.BeginTransaction())
{
child = new Child
{
Parent = session.Query<Entity>().FirstOrDefault(),
Name = "John"
};

t.Commit();
}

using (var session = OpenSession())
using (var t = session.BeginTransaction())
{
session.Merge(child);
t.Commit();
}
}
}
}
16 changes: 16 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH2627/Mappings.hbm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernate.Test"
namespace="NHibernate.Test.NHSpecificTest.GH2627">

<class name="Entity">
<id name="Id" generator="guid.comb"/>
<property name="Name"/>
</class>

<class name="Child">
<id name="Id" generator="guid.comb"/>
<property name="Name"/>
<many-to-one name="Parent" not-found="ignore"/>
</class>

</hibernate-mapping>
2 changes: 1 addition & 1 deletion src/NHibernate/Async/Type/ManyToOneType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
using System;
using System.Data.Common;
using NHibernate.Engine;
using NHibernate.Persister.Entity;
using NHibernate.Proxy;
using NHibernate.SqlTypes;
using NHibernate.Util;

Expand Down
24 changes: 17 additions & 7 deletions src/NHibernate/Type/ManyToOneType.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Data.Common;
using NHibernate.Engine;
using NHibernate.Persister.Entity;
using NHibernate.Proxy;
using NHibernate.SqlTypes;
using NHibernate.Util;

Expand Down Expand Up @@ -162,14 +162,24 @@ private bool IsIdentifier(object value, ISessionImplementor session)

public override bool IsNull(object owner, ISessionImplementor session)
{
if (IsNullable && !string.IsNullOrEmpty(PropertyName))
{
EntityEntry entry = session.PersistenceContext.GetEntry(owner);
if (!IsNullable || string.IsNullOrEmpty(PropertyName) || owner == null)
return false;

return session.PersistenceContext.IsPropertyNull(entry.EntityKey, PropertyName);
}
var entityKey = GetEntityKey(owner, session);
if (entityKey == null)
return false;

return false;
return session.PersistenceContext.IsPropertyNull(entityKey, PropertyName);
}

private static EntityKey GetEntityKey(object owner, ISessionImplementor session)
{
var entry = session.PersistenceContext.GetEntry(owner);
if (entry != null)
return entry.EntityKey;
if (owner is INHibernateProxy proxy)
return session.GenerateEntityKey(proxy.HibernateLazyInitializer.Identifier, session.Factory.GetEntityPersister(proxy.HibernateLazyInitializer.EntityName));
return null;
}

public override object Disassemble(object value, ISessionImplementor session, object owner)
Expand Down