Skip to content

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 17 commits into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
68 changes: 68 additions & 0 deletions src/NHibernate.Test/Async/NHSpecificTest/GH3325/Fixture.cs
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);
}
}
}
}
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; }
}
}
57 changes: 57 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH3325/Fixture.cs
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 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" 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 src/NHibernate.Test/NHSpecificTest/GH3325/PersistentSetType.cs
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;
}
}
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 = substitute | await (VisitCollectionsBeforeSaveAsync(entity, id, values, types, source, cancellationToken)).ConfigureAwait(false);
}

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 = substitute | VisitCollectionsBeforeSave(entity, id, values, types, source);
}

if (substitute)
Expand Down