-
Notifications
You must be signed in to change notification settings - Fork 933
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
Changes from 11 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
9d910cd
Add failing tests
hazzik efb5284
Revert previous behaviour
hazzik 48ee983
Set shouldDelayIdentityInserts = !requiresImmediateIdAccess
hazzik a759f83
Update xml comment outdated by the PR
fredericDelaporte 1f3f430
Port HHH-12826 and HHH-12846.
fredericDelaporte 611c583
Fix Firebird failure
fredericDelaporte 4497aff
Do a better fix than HHH-12826
fredericDelaporte 256c1da
Remove unused finalKey field from CollectionAction
hazzik 8858389
Temporary comment out resolving delayed key in AfterAction
hazzik 1e6ca03
Fix compilation
hazzik d9f3d17
Set CurrentKey of CollectionEntry prior AfterAction
hazzik 512cbd4
Temporary comment out setting CurrentKey before AfterAction
hazzik 7c840a6
Add tests for ownership changes
fredericDelaporte 2a7906c
Remove unneeded code and add some comments
fredericDelaporte c455f86
Merge branch 'master' into mpga
fredericDelaporte 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
165 changes: 165 additions & 0 deletions
165
src/NHibernate.Test/Async/NHSpecificTest/GH1754/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,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)); | ||
} | ||
} | ||
} | ||
} |
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,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>(); | ||
} | ||
} |
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,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
14
src/NHibernate.Test/NHSpecificTest/GH1754/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,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> |
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.
There was a problem hiding this comment.
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.