Skip to content

Commit 20a61d1

Browse files
authored
Fix KeyNotFoundException in StatefulPersistenceContext.RemoveEntity on Evict (#2389)
* NH-1845 - Modify test to be as initially reported Fixes #1310
1 parent a7025c6 commit 20a61d1

File tree

5 files changed

+43
-57
lines changed

5 files changed

+43
-57
lines changed

src/NHibernate.Test/Async/NHSpecificTest/NH1845/Fixture.cs

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

1010

11-
using NHibernate.Cfg.MappingSchema;
12-
using NHibernate.Mapping.ByCode;
1311
using NUnit.Framework;
1412
namespace NHibernate.Test.NHSpecificTest.NH1845
1513
{
1614
using System.Threading.Tasks;
1715
using System.Threading;
1816
[TestFixture]
19-
public class FixtureAsync : TestCaseMappingByCode
17+
public class FixtureAsync : BugTestCase
2018
{
21-
protected override HbmMapping GetMappings()
22-
{
23-
var mapper = new ModelMapper();
24-
mapper.Class<Category>(rc =>
25-
{
26-
rc.Id(x => x.Id, map => map.Generator(Generators.Native));
27-
rc.Property(x => x.Name);
28-
rc.ManyToOne(x => x.Parent, map => map.Column("ParentId"));
29-
rc.Bag(x => x.Subcategories, map =>
30-
{
31-
map.Access(Accessor.NoSetter);
32-
map.Key(km => km.Column("ParentId"));
33-
map.Cascade(Mapping.ByCode.Cascade.All.Include(Mapping.ByCode.Cascade.DeleteOrphans));
34-
}, rel => rel.OneToMany());
35-
});
36-
var mappings = mapper.CompileMappingForAllExplicitlyAddedEntities();
37-
return mappings;
38-
}
39-
4019
[Test]
4120
public async Task LazyLoad_Initialize_AndEvictAsync()
4221
{

src/NHibernate.Test/NHSpecificTest/NH1845/Category.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ public class Category
66
{
77
private readonly IList<Category> subcategories = new List<Category>();
88

9-
public Category() : this("") {}
9+
public Category() : this("")
10+
{
11+
}
1012

1113
public Category(string name)
1214
{
@@ -17,6 +19,8 @@ public Category(string name)
1719

1820
public virtual string Name { get; set; }
1921

22+
public virtual int SortIndex { get; set; }
23+
2024
public virtual Category Parent { get; set; }
2125

2226
public virtual IList<Category> Subcategories
@@ -50,4 +54,4 @@ public override int GetHashCode()
5054
return Name.GetHashCode();
5155
}
5256
}
53-
}
57+
}

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

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,9 @@
1-
using NHibernate.Cfg.MappingSchema;
2-
using NHibernate.Mapping.ByCode;
31
using NUnit.Framework;
42
namespace NHibernate.Test.NHSpecificTest.NH1845
53
{
64
[TestFixture]
7-
public class Fixture : TestCaseMappingByCode
5+
public class Fixture : BugTestCase
86
{
9-
protected override HbmMapping GetMappings()
10-
{
11-
var mapper = new ModelMapper();
12-
mapper.Class<Category>(rc =>
13-
{
14-
rc.Id(x => x.Id, map => map.Generator(Generators.Native));
15-
rc.Property(x => x.Name);
16-
rc.ManyToOne(x => x.Parent, map => map.Column("ParentId"));
17-
rc.Bag(x => x.Subcategories, map =>
18-
{
19-
map.Access(Accessor.NoSetter);
20-
map.Key(km => km.Column("ParentId"));
21-
map.Cascade(Mapping.ByCode.Cascade.All.Include(Mapping.ByCode.Cascade.DeleteOrphans));
22-
}, rel => rel.OneToMany());
23-
});
24-
var mappings = mapper.CompileMappingForAllExplicitlyAddedEntities();
25-
return mappings;
26-
}
27-
287
[Test]
298
public void LazyLoad_Initialize_AndEvict()
309
{
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernate.Test"
3+
namespace="NHibernate.Test.NHSpecificTest.NH1845"
4+
default-cascade="all" >
5+
<class name="Category" table="Categories" lazy="true"
6+
dynamic-update="true" select-before-update="true">
7+
8+
<id name="Id" column="Id" type="Int32" unsaved-value="0">
9+
<generator class="identity" />
10+
</id>
11+
12+
<property name="Name" />
13+
<property name="SortIndex" />
14+
15+
<many-to-one name="Parent" column="ParentCategoryId" class="Category" not-null="false"/>
16+
17+
<list name="Subcategories" cascade="all" generic="true" access="nosetter.camelcase"
18+
inverse="true" lazy="true">
19+
<key column="ParentCategoryId"/>
20+
<index column="SortIndex"/>
21+
<one-to-many class="Category"/>
22+
</list>
23+
</class>
24+
</hibernate-mapping>

src/NHibernate/Engine/StatefulPersistenceContext.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -443,18 +443,18 @@ public bool ContainsEntity(EntityKey key)
443443
/// </summary>
444444
public object RemoveEntity(EntityKey key)
445445
{
446-
if (!entitiesByKey.Remove(key, out var tempObject))
447-
throw new KeyNotFoundException(key.ToString());
448-
449-
object entity = tempObject;
450-
List<EntityUniqueKey> toRemove = new List<EntityUniqueKey>();
451-
foreach (KeyValuePair<EntityUniqueKey, object> pair in entitiesByUniqueKey)
446+
if (entitiesByKey.Remove(key, out var entity))
452447
{
453-
if (pair.Value == entity) toRemove.Add(pair.Key);
454-
}
455-
foreach (EntityUniqueKey uniqueKey in toRemove)
456-
{
457-
entitiesByUniqueKey.Remove(uniqueKey);
448+
List<EntityUniqueKey> toRemove = new List<EntityUniqueKey>();
449+
foreach (KeyValuePair<EntityUniqueKey, object> pair in entitiesByUniqueKey)
450+
{
451+
if (pair.Value == entity) toRemove.Add(pair.Key);
452+
}
453+
454+
foreach (EntityUniqueKey uniqueKey in toRemove)
455+
{
456+
entitiesByUniqueKey.Remove(uniqueKey);
457+
}
458458
}
459459

460460
entitySnapshotsByKey.Remove(key);

0 commit comments

Comments
 (0)