Skip to content
Merged
Show file tree
Hide file tree
Changes from 14 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
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ public async Task CreateWithNonEmptyManyToManyCollectionMergeWithNewElementAsync
s.Close();

AssertInsertCount(1);
AssertUpdateCount(isContractVersioned && isPlanVersioned ? 1 : 0); // NH-specific: Hibernate issues a separate UPDATE for the version number
AssertUpdateCount(isContractVersioned && isPlanVersioned ? 2 : 0);
ClearCounts();

s = OpenSession();
Expand Down
62 changes: 62 additions & 0 deletions src/NHibernate.Test/Async/NHSpecificTest/GH3325/Fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
//------------------------------------------------------------------------------
// <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;
using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.GH3325
{
using System.Threading.Tasks;
[TestFixture]
public class FixtureAsync : BugTestCase
{
protected override void OnTearDown()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
session.CreateQuery("delete from ChildEntity").ExecuteUpdate();
session.CreateQuery("delete from System.Object").ExecuteUpdate();

transaction.Commit();
}
}

[Test]
public async Task CanAddChildAfterFlushAsync()
{
Guid parentId;
Guid childId;
using (var session = OpenSession())
using (var t = session.BeginTransaction())
{
var parent = new Entity { Name = "Parent" };
var child = new ChildEntity { Name = "Child" };
var parentChildren = parent.Children;
parentChildren.Add(child);
await (session.SaveAsync(parent));
parentChildren.Remove(child);
parentId = parent.Id;
childId = child.Id;
await (t.CommitAsync());
}

using (var session = OpenSession())
{
var parent = await (session.GetAsync<Entity>(parentId));
Assert.That(parent, Is.Not.Null);
Assert.That(parent.Children, Has.Count.EqualTo(0));

var child = await (session.GetAsync<ChildEntity>(childId));
Assert.That(child, Is.Null);
}
}
}
}
9 changes: 4 additions & 5 deletions src/NHibernate.Test/Async/ReadOnly/ReadOnlyVersionedNodes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ public async Task MergeDetachedChildWithNewParentCommitWithReadOnlyChildAsync()
await (t.CommitAsync());
}

AssertUpdateCount(0); // NH-specific: Hibernate issues a separate UPDATE for the version number
AssertUpdateCount(1);
AssertInsertCount(1);
ClearCounts();
using (var s = OpenSession())
Expand All @@ -571,7 +571,7 @@ public async Task MergeDetachedChildWithNewParentCommitWithReadOnlyChildAsync()
Assert.That(child.Version, Is.EqualTo(1));
Assert.That(parent, Is.Not.Null);
Assert.That(parent.Children.Count, Is.EqualTo(0));
Assert.That(parent.Version, Is.EqualTo(1));
Assert.That(parent.Version, Is.EqualTo(2));
s.SetReadOnly(parent, true);
s.SetReadOnly(child, true);
await (s.DeleteAsync(parent));
Expand Down Expand Up @@ -609,7 +609,7 @@ public async Task GetChildMakeReadOnlyThenMergeDetachedChildWithNewParentAsync()
await (t.CommitAsync());
}

AssertUpdateCount(0); // NH-specific: Hibernate issues a separate UPDATE for the version number
AssertUpdateCount(1);
AssertInsertCount(1);
ClearCounts();
using (var s = OpenSession())
Expand All @@ -622,8 +622,7 @@ public async Task GetChildMakeReadOnlyThenMergeDetachedChildWithNewParentAsync()
Assert.That(child.Version, Is.EqualTo(1));
Assert.That(parent, Is.Not.Null);
Assert.That(parent.Children.Count, Is.EqualTo(0));
Assert.That(parent.Version, Is.EqualTo(1));
// NH-specific: Hibernate incorrectly increments version number, NH does not
Assert.That(parent.Version, Is.EqualTo(2));
s.SetReadOnly(parent, true);
s.SetReadOnly(child, true);
await (s.DeleteAsync(parent));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ public void CreateWithNonEmptyManyToManyCollectionMergeWithNewElement()
s.Close();

AssertInsertCount(1);
AssertUpdateCount(isContractVersioned && isPlanVersioned ? 1 : 0); // NH-specific: Hibernate issues a separate UPDATE for the version number
AssertUpdateCount(isContractVersioned && isPlanVersioned ? 2 : 0);
ClearCounts();

s = OpenSession();
Expand Down
26 changes: 26 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH3325/Entity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;

namespace NHibernate.Test.NHSpecificTest.GH3325
{
public class Entity
{
public virtual Guid Id { get; set; }
public virtual int Version { get; set; } = -1;
public virtual string Name { get; set; }
public virtual ISet<ChildEntity> Children { get; set; } = new HashSet<ChildEntity>();
}

public class EntityWithoutDeleteOrphan
{
public virtual Guid Id { get; set; }
public virtual string Name { get; set; }
public virtual ISet<ChildEntity> Children { get; set; } = new HashSet<ChildEntity>();
}

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

namespace NHibernate.Test.NHSpecificTest.GH3325
{
[TestFixture]
public class Fixture : BugTestCase
{
protected override void OnTearDown()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
session.CreateQuery("delete from ChildEntity").ExecuteUpdate();
session.CreateQuery("delete from System.Object").ExecuteUpdate();

transaction.Commit();
}
}

[Test]
public void CanAddChildAfterFlush()
{
Guid parentId;
Guid childId;
using (var session = OpenSession())
using (var t = session.BeginTransaction())
{
var parent = new Entity { Name = "Parent" };
var child = new ChildEntity { Name = "Child" };
var parentChildren = parent.Children;
parentChildren.Add(child);
session.Save(parent);
parentChildren.Remove(child);
parentId = parent.Id;
childId = child.Id;
t.Commit();
}

using (var session = OpenSession())
{
var parent = session.Get<Entity>(parentId);
Assert.That(parent, Is.Not.Null);
Assert.That(parent.Children, Has.Count.EqualTo(0));

var child = session.Get<ChildEntity>(childId);
Assert.That(child, Is.Null);
}
}
}
}
31 changes: 31 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH3325/Mappings.hbm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernate.Test"
namespace="NHibernate.Test.NHSpecificTest.GH3325">

<class name="Entity">
<id name="Id" generator="guid.comb"/>
<version name="Version" column="Version" type="Int32" unsaved-value="-1" />
<property name="Name"/>
<set name="Children" cascade="persist,delete,save-update,evict,lock,replicate,delete-orphan">
<key column="Parent" />
<one-to-many class="ChildEntity"/>
</set>
</class>

<class name="EntityWithoutDeleteOrphan">
<id name="Id" generator="guid.comb"/>
<property name="Name"/>
<set name="Children" cascade="persist,merge,save-update">
<key column="ParentWdo" />
<one-to-many class="ChildEntity"/>
</set>
</class>

<class name="ChildEntity">
<!-- Do not use a persisted on save generator for children. Otherwise flush cascade may
trigger parent insertion as a side effect, which tends to hide troubles. -->
<id name="Id" generator="guid.comb"/>
<property name="Name"/>
</class>

</hibernate-mapping>
9 changes: 4 additions & 5 deletions src/NHibernate.Test/ReadOnly/ReadOnlyVersionedNodes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,7 @@ public void MergeDetachedChildWithNewParentCommitWithReadOnlyChild()
t.Commit();
}

AssertUpdateCount(0); // NH-specific: Hibernate issues a separate UPDATE for the version number
AssertUpdateCount(1);
AssertInsertCount(1);
ClearCounts();
using (var s = OpenSession())
Expand All @@ -637,7 +637,7 @@ public void MergeDetachedChildWithNewParentCommitWithReadOnlyChild()
Assert.That(child.Version, Is.EqualTo(1));
Assert.That(parent, Is.Not.Null);
Assert.That(parent.Children.Count, Is.EqualTo(0));
Assert.That(parent.Version, Is.EqualTo(1));
Assert.That(parent.Version, Is.EqualTo(2));
s.SetReadOnly(parent, true);
s.SetReadOnly(child, true);
s.Delete(parent);
Expand Down Expand Up @@ -675,7 +675,7 @@ public void GetChildMakeReadOnlyThenMergeDetachedChildWithNewParent()
t.Commit();
}

AssertUpdateCount(0); // NH-specific: Hibernate issues a separate UPDATE for the version number
AssertUpdateCount(1);
AssertInsertCount(1);
ClearCounts();
using (var s = OpenSession())
Expand All @@ -688,8 +688,7 @@ public void GetChildMakeReadOnlyThenMergeDetachedChildWithNewParent()
Assert.That(child.Version, Is.EqualTo(1));
Assert.That(parent, Is.Not.Null);
Assert.That(parent.Children.Count, Is.EqualTo(0));
Assert.That(parent.Version, Is.EqualTo(1));
// NH-specific: Hibernate incorrectly increments version number, NH does not
Assert.That(parent.Version, Is.EqualTo(2));
s.SetReadOnly(parent, true);
s.SetReadOnly(child, true);
s.Delete(parent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ protected virtual async Task<object> PerformSaveOrReplicateAsync(object entity,

if (persister.HasCollections)
{
substitute = substitute || await (VisitCollectionsBeforeSaveAsync(entity, id, values, types, source, cancellationToken)).ConfigureAwait(false);
substitute = await (VisitCollectionsBeforeSaveAsync(entity, id, values, types, source, cancellationToken)).ConfigureAwait(false) || substitute;
}

if (substitute)
Expand Down
2 changes: 1 addition & 1 deletion src/NHibernate/Event/Default/AbstractSaveEventListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ protected virtual object PerformSaveOrReplicate(object entity, EntityKey key, IE

if (persister.HasCollections)
{
substitute = substitute || VisitCollectionsBeforeSave(entity, id, values, types, source);
substitute = VisitCollectionsBeforeSave(entity, id, values, types, source) || substitute;
}

if (substitute)
Expand Down