forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCollectionRecreateAction.cs
79 lines (70 loc) · 2.38 KB
/
CollectionRecreateAction.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
using System;
using System.Diagnostics;
using NHibernate.Collection;
using NHibernate.Engine;
using NHibernate.Event;
using NHibernate.Persister.Collection;
namespace NHibernate.Action
{
[Serializable]
public sealed partial class CollectionRecreateAction : CollectionAction
{
public CollectionRecreateAction(IPersistentCollection collection, ICollectionPersister persister, object key, ISessionImplementor session)
: base(persister, collection, key, session) { }
/// <summary> Execute this action</summary>
/// <remarks>
/// This method is called when a new non-null collection is persisted
/// or when an existing (non-null) collection is moved to a new owner
/// </remarks>
public override void Execute()
{
bool statsEnabled = Session.Factory.Statistics.IsStatisticsEnabled;
Stopwatch stopwatch = null;
if (statsEnabled)
{
stopwatch = Stopwatch.StartNew();
}
IPersistentCollection collection = Collection;
PreRecreate();
var key = GetKey();
Persister.Recreate(collection, key, Session);
var entry = Session.PersistenceContext.GetCollectionEntry(collection);
// On collection create, the current key may refer a delayed post insert instance instead
// of the actual identifier, that GetKey above should have resolved at that point. Update
// it.
entry.CurrentKey = key;
entry.AfterAction(collection);
Evict();
PostRecreate();
if (statsEnabled)
{
stopwatch.Stop();
Session.Factory.StatisticsImplementor.RecreateCollection(Persister.Role, stopwatch.Elapsed);
}
}
private void PreRecreate()
{
IPreCollectionRecreateEventListener[] preListeners = Session.Listeners.PreCollectionRecreateEventListeners;
if (preListeners.Length > 0)
{
PreCollectionRecreateEvent preEvent = new PreCollectionRecreateEvent(Persister, Collection, (IEventSource)Session);
for (int i = 0; i < preListeners.Length; i++)
{
preListeners[i].OnPreRecreateCollection(preEvent);
}
}
}
private void PostRecreate()
{
IPostCollectionRecreateEventListener[] postListeners = Session.Listeners.PostCollectionRecreateEventListeners;
if (postListeners.Length > 0)
{
PostCollectionRecreateEvent postEvent = new PostCollectionRecreateEvent(Persister, Collection, (IEventSource)Session);
for (int i = 0; i < postListeners.Length; i++)
{
postListeners[i].OnPostRecreateCollection(postEvent);
}
}
}
}
}