Skip to content

Delay entity insert on Persist until session is flushed #1754

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 15 commits into from
Dec 28, 2018
Merged
Show file tree
Hide file tree
Changes from 11 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
165 changes: 165 additions & 0 deletions src/NHibernate.Test/Async/NHSpecificTest/GH1754/Fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
//------------------------------------------------------------------------------
// <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.GH1754
{
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())
{
// Firebird does not like deleting tables with auto-fk.
foreach (var e in session.Query<Entity>())
{
e.Children.Clear();
}
session.Flush();

session.CreateQuery("delete from System.Object").ExecuteUpdate();

transaction.Commit();
}
}

[Test]
public async Task PersistIdentityDoNotImmediateExecuteQueryAsync()
{
using (var session = OpenSession())
{
Sfi.Statistics.Clear();
await (session.PersistAsync(new Entity {Name = "Test"}));

Assert.That(Sfi.Statistics.EntityInsertCount, Is.EqualTo(0));

await (session.FlushAsync());

Assert.That(Sfi.Statistics.EntityInsertCount, Is.EqualTo(1));
}
}

[Test]
public async Task PersistIdentityDoNotSaveIfSessionIsNotFlushedAsync()
{
using (var session = OpenSession())
{
await (session.PersistAsync(new Entity {Name = "Test"}));
}

using (var session = OpenSession())
{
var count = await (session.Query<Entity>().CountAsync());
Assert.That(count, Is.EqualTo(0));
}
}

// https://hibernate.atlassian.net/browse/HHH-12826
[Test]
public async Task CanAddChildAfterFlushAsync()
{
using (var session = OpenSession())
{
var parent = new Entity { Name = "Parent" };
var child = new Entity { Name = "Child" };
using (var t = session.BeginTransaction())
{
await (session.PersistAsync(parent));
await (session.FlushAsync());
parent.Children.Add(child);
await (t.CommitAsync());
}

Assert.That(parent.Children, Has.Count.EqualTo(1));
Assert.That(parent.Children, Does.Contain(child));
Assert.That(parent.Children.Single().Id, Is.Not.EqualTo(0));
}
}

[Test]
public async Task CanAddChildAfterFlushWithoutTransactionAsync()
{
using (var session = OpenSession())
{
var parent = new Entity { Name = "Parent" };
var child = new Entity { Name = "Child" };
await (session.PersistAsync(parent));
await (session.FlushAsync());
parent.Children.Add(child);
await (session.FlushAsync());

Assert.That(parent.Children, Has.Count.EqualTo(1));
Assert.That(parent.Children, Does.Contain(child));
Assert.That(parent.Children.Single().Id, Is.Not.EqualTo(0));
}
}

// https://hibernate.atlassian.net/browse/HHH-12846
[Test]
public async Task CanMergeWithTransientChildAsync()
{
using (var session = OpenSession())
{
var parent = new Entity { Name = "Parent" };
using (var t = session.BeginTransaction())
{
await (session.PersistAsync(parent));
await (t.CommitAsync());
}

var child = new Entity { Name = "Child" };
using (var t = session.BeginTransaction())
{
parent.Children.Add(child);
await (session.MergeAsync(parent));
await (t.CommitAsync());
}

Assert.That(parent.Children, Has.Count.EqualTo(1));
// Merge should duplicate child and leave original instance un-associated with the session.
Assert.That(parent.Children, Does.Not.Contain(child));
Assert.That(parent.Children.Single().Id, Is.Not.EqualTo(0));
}
}

[Test]
public async Task CanMergeWithTransientChildWithoutTransactionAsync()
{
using (var session = OpenSession())
{
var parent = new Entity { Name = "Parent" };
await (session.PersistAsync(parent));
await (session.FlushAsync());

var child = new Entity { Name = "Child" };
parent.Children.Add(child);
await (session.MergeAsync(parent));
await (session.FlushAsync());

Assert.That(parent.Children, Has.Count.EqualTo(1));
// Merge should duplicate child and leave original instance un-associated with the session.
Assert.That(parent.Children, Does.Not.Contain(child));
Assert.That(parent.Children.Single().Id, Is.Not.EqualTo(0));
}
}
}
}
11 changes: 11 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH1754/Entity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Collections.Generic;

namespace NHibernate.Test.NHSpecificTest.GH1754
{
class Entity
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual ISet<Entity> Children { get; set; } = new HashSet<Entity>();
}
}
153 changes: 153 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH1754/Fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
using System.Linq;
using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.GH1754
{
[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())
{
// Firebird does not like deleting tables with auto-fk.
foreach (var e in session.Query<Entity>())
{
e.Children.Clear();
}
session.Flush();

session.CreateQuery("delete from System.Object").ExecuteUpdate();

transaction.Commit();
}
}

[Test]
public void PersistIdentityDoNotImmediateExecuteQuery()
{
using (var session = OpenSession())
{
Sfi.Statistics.Clear();
session.Persist(new Entity {Name = "Test"});

Assert.That(Sfi.Statistics.EntityInsertCount, Is.EqualTo(0));

session.Flush();

Assert.That(Sfi.Statistics.EntityInsertCount, Is.EqualTo(1));
}
}

[Test]
public void PersistIdentityDoNotSaveIfSessionIsNotFlushed()
{
using (var session = OpenSession())
{
session.Persist(new Entity {Name = "Test"});
}

using (var session = OpenSession())
{
var count = session.Query<Entity>().Count();
Assert.That(count, Is.EqualTo(0));
}
}

// https://hibernate.atlassian.net/browse/HHH-12826
[Test]
public void CanAddChildAfterFlush()
{
using (var session = OpenSession())
{
var parent = new Entity { Name = "Parent" };
var child = new Entity { Name = "Child" };
using (var t = session.BeginTransaction())
{
session.Persist(parent);
session.Flush();
parent.Children.Add(child);
t.Commit();
}

Assert.That(parent.Children, Has.Count.EqualTo(1));
Assert.That(parent.Children, Does.Contain(child));
Assert.That(parent.Children.Single().Id, Is.Not.EqualTo(0));
}
}

[Test]
public void CanAddChildAfterFlushWithoutTransaction()
{
using (var session = OpenSession())
{
var parent = new Entity { Name = "Parent" };
var child = new Entity { Name = "Child" };
session.Persist(parent);
session.Flush();
parent.Children.Add(child);
session.Flush();

Assert.That(parent.Children, Has.Count.EqualTo(1));
Assert.That(parent.Children, Does.Contain(child));
Assert.That(parent.Children.Single().Id, Is.Not.EqualTo(0));
}
}

// https://hibernate.atlassian.net/browse/HHH-12846
[Test]
public void CanMergeWithTransientChild()
{
using (var session = OpenSession())
{
var parent = new Entity { Name = "Parent" };
using (var t = session.BeginTransaction())
{
session.Persist(parent);
t.Commit();
}

var child = new Entity { Name = "Child" };
using (var t = session.BeginTransaction())
{
parent.Children.Add(child);
session.Merge(parent);
t.Commit();
}

Assert.That(parent.Children, Has.Count.EqualTo(1));
// Merge should duplicate child and leave original instance un-associated with the session.
Assert.That(parent.Children, Does.Not.Contain(child));
Assert.That(parent.Children.Single().Id, Is.Not.EqualTo(0));
}
}

[Test]
public void CanMergeWithTransientChildWithoutTransaction()
{
using (var session = OpenSession())
{
var parent = new Entity { Name = "Parent" };
session.Persist(parent);
session.Flush();

var child = new Entity { Name = "Child" };
parent.Children.Add(child);
session.Merge(parent);
session.Flush();

Assert.That(parent.Children, Has.Count.EqualTo(1));
// Merge should duplicate child and leave original instance un-associated with the session.
Assert.That(parent.Children, Does.Not.Contain(child));
Assert.That(parent.Children.Single().Id, Is.Not.EqualTo(0));
}
}
}
}
14 changes: 14 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH1754/Mappings.hbm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernate.Test"
namespace="NHibernate.Test.NHSpecificTest.GH1754">

<class name="Entity">
<id name="Id" generator="identity"/>
<property name="Name"/>
<set name="Children" cascade="persist,merge,save-update,delete-orphan">
<key column="Parent" />
<one-to-many class="Entity"/>
</set>
</class>

</hibernate-mapping>
26 changes: 14 additions & 12 deletions src/NHibernate/Action/CollectionAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ namespace NHibernate.Action
public abstract partial class CollectionAction : IAsyncExecutable, IComparable<CollectionAction>, IDeserializationCallback, IAfterTransactionCompletionProcess
{
private readonly object key;
private object finalKey;
[NonSerialized] private ICollectionPersister persister;
private readonly ISessionImplementor session;
private readonly string collectionRole;
Expand Down Expand Up @@ -51,23 +50,26 @@ protected internal ICollectionPersister Persister
get { return persister; }
}

protected internal object Key
//Since v5.3
[Obsolete("Please use GetKey() instead.")]
protected internal object Key => GetKey();

protected object GetKey()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Making this a method enables to generate an async counterpart.

{
get
if (key is DelayedPostInsertIdentifier)
{
finalKey = key;
if (key is DelayedPostInsertIdentifier)
// need to look it up
var finalKey = persister.CollectionType.GetKeyOfOwner(collection.Owner, session);
if (finalKey == key)
{
// need to look it up from the persistence-context
finalKey = session.PersistenceContext.GetEntry(collection.Owner).Id;
if (finalKey == key)
{
// we may be screwed here since the collection action is about to execute
// and we do not know the final owner key value
}
// we may be screwed here since the collection action is about to execute
// and we do not know the final owner key value
}

return finalKey;
}

return key;
}

protected internal ISessionImplementor Session
Expand Down
Loading