-
Notifications
You must be signed in to change notification settings - Fork 934
Fix orphan removal for detached one-to-one #3406
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
9 commits
Select commit
Hold shift + click to select a range
edc6c53
Test case
bahusoid bb763a6
Fix
bahusoid 4525471
Generate async files
github-actions[bot] 153bd7c
Update src/NHibernate.Test/NHSpecificTest/GH3403OneToOne/Fixture.cs
bahusoid 3d3adaf
Update Fixture.cs
bahusoid cd7c81c
Generate async files
github-actions[bot] ac57f3a
Apply suggestions from code review
bahusoid 4850c9c
Generate async files
github-actions[bot] 260645f
Merge branch 'master' into oneToOneDetachedOrphan
bahusoid 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
74 changes: 74 additions & 0 deletions
74
src/NHibernate.Test/Async/NHSpecificTest/GH3403OneToOne/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,74 @@ | ||
//------------------------------------------------------------------------------ | ||
// <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.GH3403OneToOne | ||
{ | ||
using System.Threading.Tasks; | ||
[TestFixture] | ||
public class FixtureAsync : BugTestCase | ||
{ | ||
private Guid _id; | ||
|
||
protected override void OnSetUp() | ||
{ | ||
using var session = OpenSession(); | ||
using var transaction = session.BeginTransaction(); | ||
var entity = new Entity1 | ||
{ | ||
Child = new Entity2() | ||
}; | ||
|
||
entity.Child.Parent = entity; | ||
|
||
session.Save(entity); | ||
transaction.Commit(); | ||
_id = entity.Id; | ||
} | ||
|
||
protected override void OnTearDown() | ||
{ | ||
using var session = OpenSession(); | ||
using var transaction = session.BeginTransaction(); | ||
session.CreateQuery("delete from System.Object").ExecuteUpdate(); | ||
|
||
transaction.Commit(); | ||
} | ||
|
||
[Test] | ||
public async Task OrphanDeleteForDetachedOneToOneAsync() | ||
{ | ||
Guid childId; | ||
using (var session = OpenSession()) | ||
using (var transaction = session.BeginTransaction()) | ||
{ | ||
var entity = await (session.GetAsync<Entity1>(_id)); | ||
childId = entity.Child.Id; | ||
await (session.EvictAsync(entity.Child)); | ||
entity.Child = null; | ||
|
||
await (session.FlushAsync()); | ||
await (transaction.CommitAsync()); | ||
} | ||
|
||
using (var session = OpenSession()) | ||
{ | ||
var entity = await (session.GetAsync<Entity1>(_id)); | ||
Assert.That(entity, Is.Not.Null); | ||
Assert.That(entity.Child, Is.Null, "Unexpected child on parent"); | ||
|
||
var child = await (session.GetAsync<Entity2>(childId)); | ||
Assert.That(child , Is.Null, "Child is still in database"); | ||
} | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/NHibernate.Test/NHSpecificTest/GH3403OneToOne/Entity.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,17 @@ | ||
using System; | ||
|
||
namespace NHibernate.Test.NHSpecificTest.GH3403OneToOne | ||
{ | ||
public class Entity1 | ||
{ | ||
public virtual Guid Id { get; set; } | ||
|
||
public virtual Entity2 Child { get; set; } | ||
} | ||
public class Entity2 | ||
{ | ||
public virtual Guid Id { get; set; } | ||
|
||
public virtual Entity1 Parent { get; set; } | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
src/NHibernate.Test/NHSpecificTest/GH3403OneToOne/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,63 @@ | ||
using System; | ||
using NUnit.Framework; | ||
|
||
namespace NHibernate.Test.NHSpecificTest.GH3403OneToOne | ||
{ | ||
[TestFixture] | ||
public class Fixture : BugTestCase | ||
{ | ||
private Guid _id; | ||
|
||
protected override void OnSetUp() | ||
{ | ||
using var session = OpenSession(); | ||
using var transaction = session.BeginTransaction(); | ||
var entity = new Entity1 | ||
{ | ||
Child = new Entity2() | ||
}; | ||
|
||
entity.Child.Parent = entity; | ||
|
||
session.Save(entity); | ||
transaction.Commit(); | ||
_id = entity.Id; | ||
} | ||
|
||
protected override void OnTearDown() | ||
{ | ||
using var session = OpenSession(); | ||
using var transaction = session.BeginTransaction(); | ||
session.CreateQuery("delete from System.Object").ExecuteUpdate(); | ||
|
||
transaction.Commit(); | ||
} | ||
|
||
[Test] | ||
public void OrphanDeleteForDetachedOneToOne() | ||
{ | ||
Guid childId; | ||
using (var session = OpenSession()) | ||
using (var transaction = session.BeginTransaction()) | ||
{ | ||
var entity = session.Get<Entity1>(_id); | ||
childId = entity.Child.Id; | ||
session.Evict(entity.Child); | ||
entity.Child = null; | ||
|
||
session.Flush(); | ||
transaction.Commit(); | ||
} | ||
|
||
using (var session = OpenSession()) | ||
{ | ||
var entity = session.Get<Entity1>(_id); | ||
Assert.That(entity, Is.Not.Null); | ||
Assert.That(entity.Child, Is.Null, "Unexpected child on parent"); | ||
|
||
var child = session.Get<Entity2>(childId); | ||
Assert.That(child , Is.Null, "Child is still in database"); | ||
} | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/NHibernate.Test/NHSpecificTest/GH3403OneToOne/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,18 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernate.Test" | ||
namespace="NHibernate.Test.NHSpecificTest.GH3403OneToOne"> | ||
<class name="Entity1"> | ||
<id name="Id"> | ||
<generator class="guid"/> | ||
</id> | ||
<one-to-one name="Child" cascade="all-delete-orphan" foreign-key="none" /> | ||
</class> | ||
<class name="Entity2"> | ||
<id name="Id"> | ||
<generator class="foreign"> | ||
<param name="property">Parent</param> | ||
</generator> | ||
</id> | ||
<one-to-one name="Parent" foreign-key="none" /> | ||
</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
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.
This
entry
is for parent. So parent entity name is supplied for removing child entity. This entity name is only used when one-to-one entity is detached - so it's quite rare case.