Skip to content

Fix Get failing with a null exception #1924

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions src/NHibernate.Test/Async/NHSpecificTest/GH1920/Fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by AsyncGenerator.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------


using System;
using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.GH1920
{
using System.Threading.Tasks;
using System.Threading;
[TestFixture]
public class FixtureAsync : BugTestCase
{
private Guid entityId;
private Guid someOtherEntityId;

protected override void OnSetUp()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
entityId = (Guid) session.Save(new EntityWithBatchSize { Name = "some name" });
someOtherEntityId = (Guid) session.Save(new EntityWithBatchSize());

transaction.Commit();
}
}

[TestCase(true)]
[TestCase(false)]
public async Task CanLoadEntityAsync(bool loadProxyOfOtherEntity, CancellationToken cancellationToken = default(CancellationToken))
{
using (var session = OpenSession())
using (session.BeginTransaction())
{
if (loadProxyOfOtherEntity)
await (session.LoadAsync<EntityWithBatchSize>(someOtherEntityId, cancellationToken));

var result = await (session.GetAsync<EntityWithBatchSize>(entityId, cancellationToken));

Assert.That(result.Name, Is.Not.Null);
}
}

protected override void OnTearDown()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
session.CreateQuery("delete from EntityWithBatchSize").ExecuteUpdate();
transaction.Commit();
}
}
}
}
10 changes: 10 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH1920/EntityWithBatchSize.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;

namespace NHibernate.Test.NHSpecificTest.GH1920
{
public class EntityWithBatchSize
{
public virtual Guid Id { get; set; }
public virtual string Name { get; set; }
}
}
50 changes: 50 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH1920/Fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System;
using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.GH1920
{
[TestFixture]
public class Fixture : BugTestCase
{
private Guid entityId;
private Guid someOtherEntityId;

protected override void OnSetUp()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
entityId = (Guid) session.Save(new EntityWithBatchSize { Name = "some name" });
someOtherEntityId = (Guid) session.Save(new EntityWithBatchSize());

transaction.Commit();
}
}

[TestCase(true)]
[TestCase(false)]
public void CanLoadEntity(bool loadProxyOfOtherEntity)
{
using (var session = OpenSession())
using (session.BeginTransaction())
{
if (loadProxyOfOtherEntity)
session.Load<EntityWithBatchSize>(someOtherEntityId);

var result = session.Get<EntityWithBatchSize>(entityId);

Assert.That(result.Name, Is.Not.Null);
}
}

protected override void OnTearDown()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
session.CreateQuery("delete from EntityWithBatchSize").ExecuteUpdate();
transaction.Commit();
}
}
}
}
9 changes: 9 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH1920/Mappings.hbm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernate.Test"
namespace="NHibernate.Test.NHSpecificTest.GH1920">

<class name="EntityWithBatchSize" batch-size="3">
<id name="Id" generator="guid.comb"/>
<property name="Name"/>
</class>
</hibernate-mapping>
4 changes: 2 additions & 2 deletions src/NHibernate/Async/Engine/BatchFetchQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ async Task<bool> CheckCacheAndProcessResultAsync()
? collectionKeys.Count - Math.Min(batchSize, collectionKeys.Count)
: 0;
var toIndex = collectionKeys.Count - 1;
var indexes = GetSortedKeyIndexes(collectionKeys, keyIndex.Value, fromIndex, toIndex);
var indexes = GetSortedKeyIndexes(collectionKeys, keyIndex, fromIndex, toIndex);
if (batchableCache == null)
{
for (var j = 0; j < collectionKeys.Count; j++)
Expand Down Expand Up @@ -295,7 +295,7 @@ async Task<bool> CheckCacheAndProcessResultAsync()
? entityKeys.Count - Math.Min(batchSize, entityKeys.Count)
: 0;
var toIndex = entityKeys.Count - 1;
var indexes = GetSortedKeyIndexes(entityKeys, idIndex.Value, fromIndex, toIndex);
var indexes = GetSortedKeyIndexes(entityKeys, idIndex, fromIndex, toIndex);
if (batchableCache == null)
{
for (var j = 0; j < entityKeys.Count; j++)
Expand Down
12 changes: 7 additions & 5 deletions src/NHibernate/Engine/BatchFetchQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ bool CheckCacheAndProcessResult()
? collectionKeys.Count - Math.Min(batchSize, collectionKeys.Count)
: 0;
var toIndex = collectionKeys.Count - 1;
var indexes = GetSortedKeyIndexes(collectionKeys, keyIndex.Value, fromIndex, toIndex);
var indexes = GetSortedKeyIndexes(collectionKeys, keyIndex, fromIndex, toIndex);
if (batchableCache == null)
{
for (var j = 0; j < collectionKeys.Count; j++)
Expand Down Expand Up @@ -455,7 +455,7 @@ bool CheckCacheAndProcessResult()
? entityKeys.Count - Math.Min(batchSize, entityKeys.Count)
: 0;
var toIndex = entityKeys.Count - 1;
var indexes = GetSortedKeyIndexes(entityKeys, idIndex.Value, fromIndex, toIndex);
var indexes = GetSortedKeyIndexes(entityKeys, idIndex, fromIndex, toIndex);
if (batchableCache == null)
{
for (var j = 0; j < entityKeys.Count; j++)
Expand Down Expand Up @@ -612,22 +612,24 @@ private bool[] AreCached(List<KeyValuePair<KeyValuePair<CollectionEntry, IPersis
}

/// <summary>
/// Sorts the given keys by thier indexes, where the keys that are after the demanded key will be located
/// Sorts the given keys by their indexes, where the keys that are after the demanded key will be located
/// at the start and the remaining indexes at the end of the returned array.
/// </summary>
/// <typeparam name="T">The type of the key</typeparam>
/// <param name="keys">The list of pairs of keys and thier indexes.</param>
/// <param name="keys">The list of pairs of keys and their indexes.</param>
/// <param name="keyIndex">The index of the demanded key</param>
/// <param name="fromIndex">The index where the sorting will begin.</param>
/// <param name="toIndex">The index where the sorting will end.</param>
/// <returns>An array of sorted key indexes.</returns>
private static int[] GetSortedKeyIndexes<T>(List<KeyValuePair<T, int>> keys, int keyIndex, int fromIndex, int toIndex)
private static int[] GetSortedKeyIndexes<T>(List<KeyValuePair<T, int>> keys, int? keyIndex, int fromIndex, int toIndex)
{
var result = new int[Math.Abs(toIndex - fromIndex) + 1];
var lowerIndexes = new List<int>();
var i = 0;
for (var j = fromIndex; j <= toIndex; j++)
{
// If the index was not found (null), this test will be falsy and it will take the most recently
// registered entities or collections.
if (keys[j].Value < keyIndex)
{
lowerIndexes.Add(j);
Expand Down