-
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 5 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
68 changes: 68 additions & 0 deletions
68
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,68 @@ | ||
//------------------------------------------------------------------------------ | ||
// <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 OnSetUp() | ||
{ | ||
Sfi.Statistics.IsStatisticsEnabled = true; | ||
} | ||
|
||
protected override void OnTearDown() | ||
{ | ||
Sfi.Statistics.IsStatisticsEnabled = false; | ||
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" }; | ||
parent.Children.Add(child); | ||
await (session.SaveAsync(parent)); | ||
parent.Children.Remove(child); | ||
parentId = parent.Id; | ||
childId = child.Id; | ||
await (t.CommitAsync()); | ||
} | ||
|
||
using (var session = OpenSession()) | ||
using (var _ = session.BeginTransaction()) | ||
{ | ||
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); | ||
} | ||
} | ||
} | ||
} |
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,57 @@ | ||
using System; | ||
using NUnit.Framework; | ||
|
||
namespace NHibernate.Test.NHSpecificTest.GH3325 | ||
{ | ||
[TestFixture] | ||
public class Fixture : BugTestCase | ||
{ | ||
protected override void OnSetUp() | ||
{ | ||
Sfi.Statistics.IsStatisticsEnabled = true; | ||
} | ||
|
||
protected override void OnTearDown() | ||
{ | ||
Sfi.Statistics.IsStatisticsEnabled = false; | ||
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" }; | ||
parent.Children.Add(child); | ||
session.Save(parent); | ||
parent.Children.Remove(child); | ||
parentId = parent.Id; | ||
childId = child.Id; | ||
t.Commit(); | ||
} | ||
|
||
using (var session = OpenSession()) | ||
using (var _ = session.BeginTransaction()) | ||
{ | ||
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" collection-type="PersistentSetType`1[ChildEntity]"> | ||
<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> |
71 changes: 71 additions & 0 deletions
71
src/NHibernate.Test/NHSpecificTest/GH3325/PersistentSetType.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,71 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using NHibernate.Collection; | ||
using NHibernate.Collection.Generic; | ||
using NHibernate.Engine; | ||
using NHibernate.Persister.Collection; | ||
using NHibernate.UserTypes; | ||
|
||
namespace NHibernate.Test.NHSpecificTest.GH3325 | ||
{ | ||
public class PersistentSetType<T> : IUserCollectionType { | ||
IPersistentCollection IUserCollectionType.Instantiate(ISessionImplementor session, ICollectionPersister persister) { | ||
return new PersistentGenericSet<T>(session); | ||
} | ||
|
||
IPersistentCollection IUserCollectionType.Wrap(ISessionImplementor session, object collection) { | ||
return new PersistentGenericSet<T>(session, (ISet<T>) collection); | ||
} | ||
|
||
object IUserCollectionType.ReplaceElements(object original, object target, ICollectionPersister persister, object owner, IDictionary copyCache, ISessionImplementor session) { | ||
object result = target; | ||
Clear(result); | ||
|
||
foreach (object obj in (IEnumerable) original) { | ||
Add(result, CopyElement(persister, obj, session, owner, copyCache)); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
protected virtual object CopyElement(ICollectionPersister persister, object element, ISessionImplementor session, | ||
object owner, IDictionary copiedAlready) { | ||
return persister.ElementType.Replace(element, null, session, owner, copiedAlready); | ||
} | ||
|
||
object IUserCollectionType.Instantiate(int anticipatedSize) { | ||
return new HashSetWithICollection<T>(); | ||
} | ||
|
||
public IEnumerable GetElements(object collection) { | ||
return (ISet<T>) collection; | ||
} | ||
|
||
public bool Contains(object collection, object entity) { | ||
return ((ISet<T>) collection).Contains((T) entity); | ||
} | ||
|
||
public object IndexOf(object collection, object entity) { | ||
throw new NotSupportedException(); | ||
} | ||
|
||
protected virtual void Add(object collection, object element) { | ||
((ISet<T>) collection).Add((T) element); | ||
} | ||
|
||
protected virtual void Clear(object collection) { | ||
((ISet<T>) collection).Clear(); | ||
} | ||
} | ||
|
||
public class HashSetWithICollection<T> : HashSet<T>, ICollection { | ||
public void CopyTo(Array array, int index) { | ||
this.Cast<object>().ToArray().CopyTo(array, index); | ||
} | ||
|
||
public object SyncRoot => this; | ||
public bool IsSynchronized => false; | ||
} | ||
} |
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
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.