Skip to content

Commit 1bef543

Browse files
Fix Get failing with a null exception
1 parent 73bba29 commit 1bef543

File tree

4 files changed

+77
-10
lines changed

4 files changed

+77
-10
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
//------------------------------------------------------------------------------
2+
// <auto-generated>
3+
// This code was generated by AsyncGenerator.
4+
//
5+
// Changes to this file may cause incorrect behavior and will be lost if
6+
// the code is regenerated.
7+
// </auto-generated>
8+
//------------------------------------------------------------------------------
9+
10+
11+
using System;
12+
using NUnit.Framework;
13+
14+
namespace NHibernate.Test.NHSpecificTest.GH1920
15+
{
16+
using System.Threading.Tasks;
17+
using System.Threading;
18+
[TestFixture]
19+
public class FixtureAsync : BugTestCase
20+
{
21+
private Guid entityId;
22+
private Guid someOtherEntityId;
23+
24+
protected override void OnSetUp()
25+
{
26+
using (var session = OpenSession())
27+
using (var transaction = session.BeginTransaction())
28+
{
29+
entityId = (Guid) session.Save(new EntityWithBatchSize { Name = "some name" });
30+
someOtherEntityId = (Guid) session.Save(new EntityWithBatchSize());
31+
32+
transaction.Commit();
33+
}
34+
}
35+
36+
[TestCase(true)]
37+
[TestCase(false)]
38+
public async Task CanLoadEntityAsync(bool loadProxyOfOtherEntity, CancellationToken cancellationToken = default(CancellationToken))
39+
{
40+
using (var session = OpenSession())
41+
using (session.BeginTransaction())
42+
{
43+
if (loadProxyOfOtherEntity)
44+
await (session.LoadAsync<EntityWithBatchSize>(someOtherEntityId, cancellationToken));
45+
46+
var result = await (session.GetAsync<EntityWithBatchSize>(entityId, cancellationToken));
47+
48+
Assert.That(result.Name, Is.Not.Null);
49+
}
50+
}
51+
52+
protected override void OnTearDown()
53+
{
54+
using (var session = OpenSession())
55+
using (var transaction = session.BeginTransaction())
56+
{
57+
session.CreateQuery("delete from EntityWithBatchSize").ExecuteUpdate();
58+
transaction.Commit();
59+
}
60+
}
61+
}
62+
}

src/NHibernate.Test/NHSpecificTest/GH1920/Fixture.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ public class Fixture : BugTestCase
88
{
99
private Guid entityId;
1010
private Guid someOtherEntityId;
11-
11+
1212
protected override void OnSetUp()
1313
{
1414
using (var session = OpenSession())
1515
using (var transaction = session.BeginTransaction())
1616
{
17-
entityId = (Guid) session.Save(new EntityWithBatchSize {Name = "some name"});
17+
entityId = (Guid) session.Save(new EntityWithBatchSize { Name = "some name" });
1818
someOtherEntityId = (Guid) session.Save(new EntityWithBatchSize());
1919

2020
transaction.Commit();
@@ -28,16 +28,15 @@ public void CanLoadEntity(bool loadProxyOfOtherEntity)
2828
using (var session = OpenSession())
2929
using (session.BeginTransaction())
3030
{
31-
if(loadProxyOfOtherEntity)
31+
if (loadProxyOfOtherEntity)
3232
session.Load<EntityWithBatchSize>(someOtherEntityId);
33-
33+
3434
var result = session.Get<EntityWithBatchSize>(entityId);
3535

3636
Assert.That(result.Name, Is.Not.Null);
3737
}
3838
}
39-
40-
39+
4140
protected override void OnTearDown()
4241
{
4342
using (var session = OpenSession())

src/NHibernate/Async/Engine/BatchFetchQueue.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,10 @@ async Task<bool> CheckCacheAndProcessResultAsync()
295295
? entityKeys.Count - Math.Min(batchSize, entityKeys.Count)
296296
: 0;
297297
var toIndex = entityKeys.Count - 1;
298-
var indexes = GetSortedKeyIndexes(entityKeys, idIndex.Value, fromIndex, toIndex);
298+
// In case of an ISession.Get on an entity not already loaded as an uninitialized proxy,
299+
// it will not be found in entities awaiting a load, and idIndex will be null. Providing
300+
// -1 then allows to take the entities most recently loaded as uninitialized proxies.
301+
var indexes = GetSortedKeyIndexes(entityKeys, idIndex ?? -1, fromIndex, toIndex);
299302
if (batchableCache == null)
300303
{
301304
for (var j = 0; j < entityKeys.Count; j++)

src/NHibernate/Engine/BatchFetchQueue.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,10 @@ bool CheckCacheAndProcessResult()
455455
? entityKeys.Count - Math.Min(batchSize, entityKeys.Count)
456456
: 0;
457457
var toIndex = entityKeys.Count - 1;
458-
var indexes = GetSortedKeyIndexes(entityKeys, idIndex.Value, fromIndex, toIndex);
458+
// In case of an ISession.Get on an entity not already loaded as an uninitialized proxy,
459+
// it will not be found in entities awaiting a load, and idIndex will be null. Providing
460+
// -1 then allows to take the entities most recently loaded as uninitialized proxies.
461+
var indexes = GetSortedKeyIndexes(entityKeys, idIndex ?? -1, fromIndex, toIndex);
459462
if (batchableCache == null)
460463
{
461464
for (var j = 0; j < entityKeys.Count; j++)
@@ -612,11 +615,11 @@ private bool[] AreCached(List<KeyValuePair<KeyValuePair<CollectionEntry, IPersis
612615
}
613616

614617
/// <summary>
615-
/// Sorts the given keys by thier indexes, where the keys that are after the demanded key will be located
618+
/// Sorts the given keys by their indexes, where the keys that are after the demanded key will be located
616619
/// at the start and the remaining indexes at the end of the returned array.
617620
/// </summary>
618621
/// <typeparam name="T">The type of the key</typeparam>
619-
/// <param name="keys">The list of pairs of keys and thier indexes.</param>
622+
/// <param name="keys">The list of pairs of keys and their indexes.</param>
620623
/// <param name="keyIndex">The index of the demanded key</param>
621624
/// <param name="fromIndex">The index where the sorting will begin.</param>
622625
/// <param name="toIndex">The index where the sorting will end.</param>

0 commit comments

Comments
 (0)