Skip to content

Commit d45a965

Browse files
committed
AbstractCollectionPersister.cs: Avoid use of Reset() on enumerator - it is usually not expected to be implemented.
1 parent c69c3ea commit d45a965

File tree

1 file changed

+39
-40
lines changed

1 file changed

+39
-40
lines changed

src/NHibernate/Persister/Collection/AbstractCollectionPersister.cs

Lines changed: 39 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
using System.Collections;
33
using System.Collections.Generic;
44
using System.Data;
5-
using System.Data.Common;
6-
using System.Linq;
5+
using System.Data.Common;
6+
using System.Linq;
77
using System.Text;
88
using NHibernate.AdoNet;
99
using NHibernate.Cache;
@@ -621,11 +621,11 @@ private ICollectionInitializer GetSubselectInitializer(object key, ISessionImple
621621
else
622622
{
623623
// Take care of any entities that might have
624-
// been evicted!
625-
List<EntityKey> keysToRemove = subselect.Result
626-
.Where(entityKey => !persistenceContext.ContainsEntity(entityKey)).ToList();
627-
628-
foreach (var entityKey in keysToRemove)
624+
// been evicted!
625+
List<EntityKey> keysToRemove = subselect.Result
626+
.Where(entityKey => !persistenceContext.ContainsEntity(entityKey)).ToList();
627+
628+
foreach (var entityKey in keysToRemove)
629629
subselect.Result.Remove(entityKey);
630630

631631
// Run a subquery loader
@@ -1080,50 +1080,49 @@ public void Recreate(IPersistentCollection collection, object id, ISessionImplem
10801080

10811081
try
10821082
{
1083+
IExpectation expectation = null;
1084+
bool useBatch = false;
1085+
int i = 0;
1086+
int count = 0;
1087+
10831088
// create all the new entries
10841089
IEnumerator entries = collection.Entries(this).GetEnumerator();
1085-
if (entries.MoveNext())
1090+
while (entries.MoveNext())
10861091
{
1087-
entries.Reset();
1088-
IExpectation expectation = Expectations.AppropriateExpectation(insertCheckStyle);
1089-
collection.PreInsert(this);
1090-
//bool callable = InsertCallable;
1091-
bool useBatch = expectation.CanBeBatched;
1092-
int i = 0;
1093-
int count = 0;
1094-
1095-
while (entries.MoveNext())
1092+
// Init, if we're on the first element.
1093+
if (count == 0)
10961094
{
1097-
object entry = entries.Current;
1098-
if (collection.EntryExists(entry, i))
1099-
{
1100-
object entryId;
1101-
if (!IsIdentifierAssignedByInsert)
1102-
{
1103-
// NH Different implementation: write once
1104-
entryId = PerformInsert(id, collection, expectation, entry, i, useBatch, false, session);
1105-
}
1106-
else
1107-
{
1108-
entryId = PerformInsert(id, collection, entry, i, session);
1109-
}
1110-
collection.AfterRowInsert(this, entry, i, entryId);
1111-
count++;
1112-
}
1113-
i++;
1095+
expectation = Expectations.AppropriateExpectation(insertCheckStyle);
1096+
collection.PreInsert(this);
1097+
//bool callable = InsertCallable;
1098+
useBatch = expectation.CanBeBatched;
11141099
}
11151100

1116-
if (log.IsDebugEnabled)
1101+
object entry = entries.Current;
1102+
if (collection.EntryExists(entry, i))
11171103
{
1118-
log.Debug(string.Format("done inserting collection: {0} rows inserted", count));
1104+
object entryId;
1105+
if (!IsIdentifierAssignedByInsert)
1106+
{
1107+
// NH Different implementation: write once
1108+
entryId = PerformInsert(id, collection, expectation, entry, i, useBatch, false, session);
1109+
}
1110+
else
1111+
{
1112+
entryId = PerformInsert(id, collection, entry, i, session);
1113+
}
1114+
collection.AfterRowInsert(this, entry, i, entryId);
1115+
count++;
11191116
}
1117+
i++;
11201118
}
1121-
else
1119+
1120+
if (log.IsDebugEnabled)
11221121
{
1123-
if (log.IsDebugEnabled)
1124-
{
1122+
if (count > 0)
1123+
log.Debug(string.Format("done inserting collection: {0} rows inserted", count));
1124+
else
11251125
log.Debug("collection was empty");
1126-
}
11271126
}
11281127
}
11291128
catch (DbException sqle)

0 commit comments

Comments
 (0)