Skip to content

Commit 4497aff

Browse files
Do a better fix than HHH-12826
To be squashed.
1 parent 611c583 commit 4497aff

11 files changed

+59
-18
lines changed

src/NHibernate/Action/CollectionAction.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ protected internal object Key
5858
finalKey = key;
5959
if (key is DelayedPostInsertIdentifier)
6060
{
61-
// need to look it up from the persistence-context
62-
finalKey = session.PersistenceContext.GetEntry(collection.Owner).Id;
61+
// need to look it up
62+
finalKey = persister.CollectionType.GetKeyOfOwner(collection.Owner, session);
6363
if (finalKey == key)
6464
{
6565
// we may be screwed here since the collection action is about to execute

src/NHibernate/Action/CollectionRecreateAction.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public override void Execute()
3232

3333
Persister.Recreate(collection, Key, Session);
3434

35-
Session.PersistenceContext.GetCollectionEntry(collection).AfterAction(collection);
35+
Session.PersistenceContext.GetCollectionEntry(collection).AfterAction(collection, Session);
3636

3737
Evict();
3838

@@ -70,4 +70,4 @@ private void PostRecreate()
7070
}
7171
}
7272
}
73-
}
73+
}

src/NHibernate/Action/CollectionRemoveAction.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public override void Execute()
7474
IPersistentCollection collection = Collection;
7575
if (collection != null)
7676
{
77-
Session.PersistenceContext.GetCollectionEntry(collection).AfterAction(collection);
77+
Session.PersistenceContext.GetCollectionEntry(collection).AfterAction(collection, Session);
7878
}
7979

8080
Evict();
@@ -121,4 +121,4 @@ public override int CompareTo(CollectionAction other)
121121
return 0;
122122
}
123123
}
124-
}
124+
}

src/NHibernate/Action/CollectionUpdateAction.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public override void Execute()
7474
persister.InsertRows(collection, id, session);
7575
}
7676

77-
Session.PersistenceContext.GetCollectionEntry(collection).AfterAction(collection);
77+
Session.PersistenceContext.GetCollectionEntry(collection).AfterAction(collection, session);
7878

7979
Evict();
8080

src/NHibernate/Async/Action/CollectionRecreateAction.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public override async Task ExecuteAsync(CancellationToken cancellationToken)
4343

4444
await (Persister.RecreateAsync(collection, Key, Session, cancellationToken)).ConfigureAwait(false);
4545

46-
Session.PersistenceContext.GetCollectionEntry(collection).AfterAction(collection);
46+
await (Session.PersistenceContext.GetCollectionEntry(collection).AfterActionAsync(collection, Session, cancellationToken)).ConfigureAwait(false);
4747

4848
await (EvictAsync(cancellationToken)).ConfigureAwait(false);
4949

@@ -83,4 +83,4 @@ private async Task PostRecreateAsync(CancellationToken cancellationToken)
8383
}
8484
}
8585
}
86-
}
86+
}

src/NHibernate/Async/Action/CollectionRemoveAction.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public override async Task ExecuteAsync(CancellationToken cancellationToken)
4242
IPersistentCollection collection = Collection;
4343
if (collection != null)
4444
{
45-
Session.PersistenceContext.GetCollectionEntry(collection).AfterAction(collection);
45+
await (Session.PersistenceContext.GetCollectionEntry(collection).AfterActionAsync(collection, Session, cancellationToken)).ConfigureAwait(false);
4646
}
4747

4848
await (EvictAsync(cancellationToken)).ConfigureAwait(false);
@@ -86,4 +86,4 @@ private async Task PostRemoveAsync(CancellationToken cancellationToken)
8686
}
8787
}
8888
}
89-
}
89+
}

src/NHibernate/Async/Action/CollectionUpdateAction.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public override async Task ExecuteAsync(CancellationToken cancellationToken)
7878
await (persister.InsertRowsAsync(collection, id, session, cancellationToken)).ConfigureAwait(false);
7979
}
8080

81-
Session.PersistenceContext.GetCollectionEntry(collection).AfterAction(collection);
81+
await (Session.PersistenceContext.GetCollectionEntry(collection).AfterActionAsync(collection, session, cancellationToken)).ConfigureAwait(false);
8282

8383
await (EvictAsync(cancellationToken)).ConfigureAwait(false);
8484

src/NHibernate/Async/Engine/CollectionEntry.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
using System;
1212
using System.Collections;
13-
13+
using NHibernate.Action;
1414
using NHibernate.Collection;
1515
using NHibernate.Impl;
1616
using NHibernate.Persister.Collection;
@@ -71,6 +71,27 @@ public async Task PreFlushAsync(IPersistentCollection collection, CancellationTo
7171
processed = false;
7272
}
7373

74+
public async Task AfterActionAsync(IPersistentCollection collection, ISessionImplementor session, CancellationToken cancellationToken)
75+
{
76+
cancellationToken.ThrowIfCancellationRequested();
77+
loadedKey = CurrentKey;
78+
if (loadedKey is DelayedPostInsertIdentifier && CurrentPersister != null)
79+
{
80+
// Resolve the actual key
81+
loadedKey = await (CurrentPersister.CollectionType.GetKeyOfOwnerAsync(collection.Owner, session, cancellationToken)).ConfigureAwait(false);
82+
}
83+
84+
SetLoadedPersister(CurrentPersister);
85+
86+
if (collection.WasInitialized && (IsDoremove || IsDorecreate || IsDoupdate))
87+
{
88+
//re-snapshot
89+
snapshot = loadedPersister == null || !loadedPersister.IsMutable ? null : collection.GetSnapshot(loadedPersister);
90+
}
91+
92+
collection.PostAction();
93+
}
94+
7495
public Task<ICollection> GetOrphansAsync(string entityName, IPersistentCollection collection, CancellationToken cancellationToken)
7596
{
7697
if (snapshot == null)

src/NHibernate/Async/Engine/Collections.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
//------------------------------------------------------------------------------
99

1010

11-
using NHibernate.Action;
11+
1212
using NHibernate.Collection;
1313
using NHibernate.Impl;
1414
using NHibernate.Persister.Collection;
@@ -204,7 +204,6 @@ private static Task PrepareCollectionForUpdateAsync(IPersistentCollection collec
204204
{
205205
// it is or was referenced _somewhere_
206206
bool ownerChanged = loadedPersister != currentPersister ||
207-
!(entry.LoadedKey is DelayedPostInsertIdentifier) &&
208207
!currentPersister.KeyType.IsEqual(entry.LoadedKey, entry.CurrentKey, factory);
209208

210209
if (ownerChanged)

src/NHibernate/Engine/CollectionEntry.cs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System;
22
using System.Collections;
3-
3+
using NHibernate.Action;
44
using NHibernate.Collection;
55
using NHibernate.Impl;
66
using NHibernate.Persister.Collection;
@@ -346,6 +346,8 @@ public void PostFlush(IPersistentCollection collection)
346346
collection.SetSnapshot(loadedKey, role, snapshot);
347347
}
348348

349+
// Since v5.2
350+
[Obsolete("Please use AfterAction overload taking a session instead")]
349351
public void AfterAction(IPersistentCollection collection)
350352
{
351353
loadedKey = CurrentKey;
@@ -361,6 +363,26 @@ public void AfterAction(IPersistentCollection collection)
361363
collection.PostAction();
362364
}
363365

366+
public void AfterAction(IPersistentCollection collection, ISessionImplementor session)
367+
{
368+
loadedKey = CurrentKey;
369+
if (loadedKey is DelayedPostInsertIdentifier && CurrentPersister != null)
370+
{
371+
// Resolve the actual key
372+
loadedKey = CurrentPersister.CollectionType.GetKeyOfOwner(collection.Owner, session);
373+
}
374+
375+
SetLoadedPersister(CurrentPersister);
376+
377+
if (collection.WasInitialized && (IsDoremove || IsDorecreate || IsDoupdate))
378+
{
379+
//re-snapshot
380+
snapshot = loadedPersister == null || !loadedPersister.IsMutable ? null : collection.GetSnapshot(loadedPersister);
381+
}
382+
383+
collection.PostAction();
384+
}
385+
364386
/// <summary>
365387
/// Sets the information in this CollectionEntry that is specific to the
366388
/// <see cref="ICollectionPersister"/>.

src/NHibernate/Engine/Collections.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using NHibernate.Action;
1+
22
using NHibernate.Collection;
33
using NHibernate.Impl;
44
using NHibernate.Persister.Collection;
@@ -151,7 +151,6 @@ private static void PrepareCollectionForUpdate(IPersistentCollection collection,
151151
{
152152
// it is or was referenced _somewhere_
153153
bool ownerChanged = loadedPersister != currentPersister ||
154-
!(entry.LoadedKey is DelayedPostInsertIdentifier) &&
155154
!currentPersister.KeyType.IsEqual(entry.LoadedKey, entry.CurrentKey, factory);
156155

157156
if (ownerChanged)

0 commit comments

Comments
 (0)