-
Notifications
You must be signed in to change notification settings - Fork 934
Fixes cascading orphan delete on versioned entity #3326
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
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
2164f29
Fixes #3325
csharper2010 70f7b9f
Generate async files
github-actions[bot] ec39961
Fix Namespace
csharper2010 cd6362f
Merge branch 'GH3325' of https://github.com/csharper2010/nhibernate-c…
csharper2010 36d4d44
Generate async files
github-actions[bot] 00d1bf2
Clean out custom collection because irrelevant for the problem
csharper2010 a99ad9c
Merge branch 'GH3325' of https://github.com/csharper2010/nhibernate-c…
csharper2010 eaf89ac
Align fix with Hibernate and suggestions
hazzik cf4e9a3
Align tests with Hibernate
hazzik 5cb796f
Generate async files
github-actions[bot] 8d85192
Adjust test to Hibernate
hazzik 9cf5946
Generate async files
github-actions[bot] 593f3fe
Use unwrapped collection to add/remove items
bahusoid c3c2a3b
Generate async files
github-actions[bot] ae069a8
Test for more behaviours
hazzik 5cbe028
Generate async files
github-actions[bot] 80e9bec
Merge branch 'master' into GH3325
hazzik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
src/NHibernate.Test/Async/NHSpecificTest/GH3325/Fixture.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
//------------------------------------------------------------------------------ | ||
// <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 System.Collections.Generic; | ||
using NUnit.Framework; | ||
|
||
namespace NHibernate.Test.NHSpecificTest.GH3325 | ||
{ | ||
using System.Threading.Tasks; | ||
using System.Threading; | ||
[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 CanRemoveChildAfterSaveAsync() | ||
{ | ||
using var session = OpenSession(); | ||
using var t = session.BeginTransaction(); | ||
|
||
var parent = new Entity { Name = "Parent" }; | ||
var child = new ChildEntity { Name = "Child" }; | ||
parent.Children.Add(child); | ||
await (session.SaveAsync(parent)); | ||
parent.Children.Remove(child); | ||
var parentId = parent.Id; | ||
var childId = child.Id; | ||
await (t.CommitAsync()); | ||
|
||
await (AssertParentIsChildlessAsync(parentId, childId)); | ||
} | ||
|
||
[Test] | ||
public async Task CanRemoveChildFromUnwrappedCollectionAfterSaveAsync() | ||
{ | ||
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); | ||
var parentId = parent.Id; | ||
var childId = child.Id; | ||
await (t.CommitAsync()); | ||
|
||
await (AssertParentIsChildlessAsync(parentId, childId)); | ||
} | ||
|
||
[Test] | ||
public async Task CanRemoveChildAfterSaveAndExplicitFlushAsync() | ||
{ | ||
using var session = OpenSession(); | ||
using var t = session.BeginTransaction(); | ||
|
||
var parent = new Entity { Name = "Parent" }; | ||
var child = new ChildEntity { Name = "Child" }; | ||
parent.Children.Add(child); | ||
await (session.SaveAsync(parent)); | ||
await (session.FlushAsync()); | ||
parent.Children.Remove(child); | ||
var parentId = parent.Id; | ||
var childId = child.Id; | ||
await (t.CommitAsync()); | ||
|
||
await (AssertParentIsChildlessAsync(parentId, childId)); | ||
} | ||
|
||
[Test] | ||
public async Task CanRemoveChildFromUnwrappedCollectionAfterSaveAndExplicitFlushAsync() | ||
{ | ||
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)); | ||
await (session.FlushAsync()); | ||
parentChildren.Remove(child); | ||
var parentId = parent.Id; | ||
var childId = child.Id; | ||
await (t.CommitAsync()); | ||
|
||
await (AssertParentIsChildlessAsync(parentId, childId)); | ||
} | ||
|
||
private async Task AssertParentIsChildlessAsync(Guid parentId, Guid childId, CancellationToken cancellationToken = default(CancellationToken)) | ||
{ | ||
using var session = OpenSession(); | ||
|
||
var parent = await (session.GetAsync<Entity>(parentId, cancellationToken)); | ||
Assert.That(parent, Is.Not.Null); | ||
Assert.That(parent.Children, Has.Count.EqualTo(0)); | ||
|
||
var child = await (session.GetAsync<ChildEntity>(childId, cancellationToken)); | ||
Assert.That(child, Is.Null); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
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 CanRemoveChildAfterSave() | ||
{ | ||
using var session = OpenSession(); | ||
using var t = session.BeginTransaction(); | ||
|
||
var parent = new Entity { Name = "Parent" }; | ||
var child = new ChildEntity { Name = "Child" }; | ||
parent.Children.Add(child); | ||
session.Save(parent); | ||
parent.Children.Remove(child); | ||
var parentId = parent.Id; | ||
var childId = child.Id; | ||
t.Commit(); | ||
|
||
AssertParentIsChildless(parentId, childId); | ||
} | ||
|
||
[Test] | ||
public void CanRemoveChildFromUnwrappedCollectionAfterSave() | ||
{ | ||
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); | ||
var parentId = parent.Id; | ||
var childId = child.Id; | ||
t.Commit(); | ||
|
||
AssertParentIsChildless(parentId, childId); | ||
} | ||
|
||
[Test] | ||
public void CanRemoveChildAfterSaveAndExplicitFlush() | ||
{ | ||
using var session = OpenSession(); | ||
using var t = session.BeginTransaction(); | ||
|
||
var parent = new Entity { Name = "Parent" }; | ||
var child = new ChildEntity { Name = "Child" }; | ||
parent.Children.Add(child); | ||
session.Save(parent); | ||
session.Flush(); | ||
parent.Children.Remove(child); | ||
var parentId = parent.Id; | ||
var childId = child.Id; | ||
t.Commit(); | ||
|
||
AssertParentIsChildless(parentId, childId); | ||
} | ||
|
||
[Test] | ||
public void CanRemoveChildFromUnwrappedCollectionAfterSaveAndExplicitFlush() | ||
{ | ||
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); | ||
session.Flush(); | ||
parentChildren.Remove(child); | ||
var parentId = parent.Id; | ||
var childId = child.Id; | ||
t.Commit(); | ||
|
||
AssertParentIsChildless(parentId, childId); | ||
} | ||
|
||
private void AssertParentIsChildless(Guid parentId, Guid childId) | ||
{ | ||
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
31
src/NHibernate.Test/NHSpecificTest/GH3325/Mappings.hbm.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.