11using System ;
2+ using System . Collections . Generic ;
23using NUnit . Framework ;
34
45namespace NHibernate . Test . NHSpecificTest . GH3325
@@ -8,44 +9,101 @@ public class Fixture : BugTestCase
89 {
910 protected override void OnTearDown ( )
1011 {
11- using ( var session = OpenSession ( ) )
12- using ( var transaction = session . BeginTransaction ( ) )
13- {
14- session . CreateQuery ( "delete from ChildEntity" ) . ExecuteUpdate ( ) ;
15- session . CreateQuery ( "delete from System.Object" ) . ExecuteUpdate ( ) ;
16-
17- transaction . Commit ( ) ;
18- }
12+ using var session = OpenSession ( ) ;
13+ using var transaction = session . BeginTransaction ( ) ;
14+
15+ session . CreateQuery ( "delete from ChildEntity" ) . ExecuteUpdate ( ) ;
16+ session . CreateQuery ( "delete from System.Object" ) . ExecuteUpdate ( ) ;
17+
18+ transaction . Commit ( ) ;
19+ }
20+
21+ [ Test ]
22+ public void CanRemoveChildAfterSave ( )
23+ {
24+ using var session = OpenSession ( ) ;
25+ using var t = session . BeginTransaction ( ) ;
26+
27+ var parent = new Entity { Name = "Parent" } ;
28+ var child = new ChildEntity { Name = "Child" } ;
29+ parent . Children . Add ( child ) ;
30+ session . Save ( parent ) ;
31+ parent . Children . Remove ( child ) ;
32+ var parentId = parent . Id ;
33+ var childId = child . Id ;
34+ t . Commit ( ) ;
35+
36+ AssertParentIsChildless ( parentId , childId ) ;
37+ }
38+
39+ [ Test ]
40+ public void CanRemoveChildFromUnwrappedCollectionAfterSave ( )
41+ {
42+ using var session = OpenSession ( ) ;
43+ using var t = session . BeginTransaction ( ) ;
44+
45+ var parent = new Entity { Name = "Parent" } ;
46+ var child = new ChildEntity { Name = "Child" } ;
47+ var parentChildren = parent . Children ;
48+ parentChildren . Add ( child ) ;
49+ session . Save ( parent ) ;
50+ parentChildren . Remove ( child ) ;
51+ var parentId = parent . Id ;
52+ var childId = child . Id ;
53+ t . Commit ( ) ;
54+
55+ AssertParentIsChildless ( parentId , childId ) ;
1956 }
2057
2158 [ Test ]
22- public void CanAddChildAfterFlush ( )
59+ public void CanRemoveChildAfterSaveAndExplicitFlush ( )
2360 {
24- Guid parentId ;
25- Guid childId ;
26- using ( var session = OpenSession ( ) )
27- using ( var t = session . BeginTransaction ( ) )
28- {
29- var parent = new Entity { Name = "Parent" } ;
30- var child = new ChildEntity { Name = "Child" } ;
31- var parentChildren = parent . Children ;
32- parentChildren . Add ( child ) ;
33- session . Save ( parent ) ;
34- parentChildren . Remove ( child ) ;
35- parentId = parent . Id ;
36- childId = child . Id ;
37- t . Commit ( ) ;
38- }
39-
40- using ( var session = OpenSession ( ) )
41- {
42- var parent = session . Get < Entity > ( parentId ) ;
43- Assert . That ( parent , Is . Not . Null ) ;
44- Assert . That ( parent . Children , Has . Count . EqualTo ( 0 ) ) ;
45-
46- var child = session . Get < ChildEntity > ( childId ) ;
47- Assert . That ( child , Is . Null ) ;
48- }
61+ using var session = OpenSession ( ) ;
62+ using var t = session . BeginTransaction ( ) ;
63+
64+ var parent = new Entity { Name = "Parent" } ;
65+ var child = new ChildEntity { Name = "Child" } ;
66+ parent . Children . Add ( child ) ;
67+ session . Save ( parent ) ;
68+ session . Flush ( ) ;
69+ parent . Children . Remove ( child ) ;
70+ var parentId = parent . Id ;
71+ var childId = child . Id ;
72+ t . Commit ( ) ;
73+
74+ AssertParentIsChildless ( parentId , childId ) ;
75+ }
76+
77+ [ Test ]
78+ public void CanRemoveChildFromUnwrappedCollectionAfterSaveAndExplicitFlush ( )
79+ {
80+ using var session = OpenSession ( ) ;
81+ using var t = session . BeginTransaction ( ) ;
82+
83+ var parent = new Entity { Name = "Parent" } ;
84+ var child = new ChildEntity { Name = "Child" } ;
85+ var parentChildren = parent . Children ;
86+ parentChildren . Add ( child ) ;
87+ session . Save ( parent ) ;
88+ session . Flush ( ) ;
89+ parentChildren . Remove ( child ) ;
90+ var parentId = parent . Id ;
91+ var childId = child . Id ;
92+ t . Commit ( ) ;
93+
94+ AssertParentIsChildless ( parentId , childId ) ;
95+ }
96+
97+ private void AssertParentIsChildless ( Guid parentId , Guid childId )
98+ {
99+ using var session = OpenSession ( ) ;
100+
101+ var parent = session . Get < Entity > ( parentId ) ;
102+ Assert . That ( parent , Is . Not . Null ) ;
103+ Assert . That ( parent . Children , Has . Count . EqualTo ( 0 ) ) ;
104+
105+ var child = session . Get < ChildEntity > ( childId ) ;
106+ Assert . That ( child , Is . Null ) ;
49107 }
50108 }
51109}
0 commit comments