Skip to content

Commit

Permalink
Fix KeyNotFoundException in StatefulPersistenceContext.RemoveEntity o…
Browse files Browse the repository at this point in the history
…n Evict (#2389)

* NH-1845 - Modify test to be as initially reported

Fixes #1310
  • Loading branch information
hazzik authored May 20, 2020
1 parent a7025c6 commit 20a61d1
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 57 deletions.
23 changes: 1 addition & 22 deletions src/NHibernate.Test/Async/NHSpecificTest/NH1845/Fixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,14 @@
//------------------------------------------------------------------------------


using NHibernate.Cfg.MappingSchema;
using NHibernate.Mapping.ByCode;
using NUnit.Framework;
namespace NHibernate.Test.NHSpecificTest.NH1845
{
using System.Threading.Tasks;
using System.Threading;
[TestFixture]
public class FixtureAsync : TestCaseMappingByCode
public class FixtureAsync : BugTestCase
{
protected override HbmMapping GetMappings()
{
var mapper = new ModelMapper();
mapper.Class<Category>(rc =>
{
rc.Id(x => x.Id, map => map.Generator(Generators.Native));
rc.Property(x => x.Name);
rc.ManyToOne(x => x.Parent, map => map.Column("ParentId"));
rc.Bag(x => x.Subcategories, map =>
{
map.Access(Accessor.NoSetter);
map.Key(km => km.Column("ParentId"));
map.Cascade(Mapping.ByCode.Cascade.All.Include(Mapping.ByCode.Cascade.DeleteOrphans));
}, rel => rel.OneToMany());
});
var mappings = mapper.CompileMappingForAllExplicitlyAddedEntities();
return mappings;
}

[Test]
public async Task LazyLoad_Initialize_AndEvictAsync()
{
Expand Down
8 changes: 6 additions & 2 deletions src/NHibernate.Test/NHSpecificTest/NH1845/Category.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ public class Category
{
private readonly IList<Category> subcategories = new List<Category>();

public Category() : this("") {}
public Category() : this("")
{
}

public Category(string name)
{
Expand All @@ -17,6 +19,8 @@ public Category(string name)

public virtual string Name { get; set; }

public virtual int SortIndex { get; set; }

public virtual Category Parent { get; set; }

public virtual IList<Category> Subcategories
Expand Down Expand Up @@ -50,4 +54,4 @@ public override int GetHashCode()
return Name.GetHashCode();
}
}
}
}
23 changes: 1 addition & 22 deletions src/NHibernate.Test/NHSpecificTest/NH1845/Fixture.cs
Original file line number Diff line number Diff line change
@@ -1,30 +1,9 @@
using NHibernate.Cfg.MappingSchema;
using NHibernate.Mapping.ByCode;
using NUnit.Framework;
namespace NHibernate.Test.NHSpecificTest.NH1845
{
[TestFixture]
public class Fixture : TestCaseMappingByCode
public class Fixture : BugTestCase
{
protected override HbmMapping GetMappings()
{
var mapper = new ModelMapper();
mapper.Class<Category>(rc =>
{
rc.Id(x => x.Id, map => map.Generator(Generators.Native));
rc.Property(x => x.Name);
rc.ManyToOne(x => x.Parent, map => map.Column("ParentId"));
rc.Bag(x => x.Subcategories, map =>
{
map.Access(Accessor.NoSetter);
map.Key(km => km.Column("ParentId"));
map.Cascade(Mapping.ByCode.Cascade.All.Include(Mapping.ByCode.Cascade.DeleteOrphans));
}, rel => rel.OneToMany());
});
var mappings = mapper.CompileMappingForAllExplicitlyAddedEntities();
return mappings;
}

[Test]
public void LazyLoad_Initialize_AndEvict()
{
Expand Down
24 changes: 24 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/NH1845/Mappings.hbm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernate.Test"
namespace="NHibernate.Test.NHSpecificTest.NH1845"
default-cascade="all" >
<class name="Category" table="Categories" lazy="true"
dynamic-update="true" select-before-update="true">

<id name="Id" column="Id" type="Int32" unsaved-value="0">
<generator class="identity" />
</id>

<property name="Name" />
<property name="SortIndex" />

<many-to-one name="Parent" column="ParentCategoryId" class="Category" not-null="false"/>

<list name="Subcategories" cascade="all" generic="true" access="nosetter.camelcase"
inverse="true" lazy="true">
<key column="ParentCategoryId"/>
<index column="SortIndex"/>
<one-to-many class="Category"/>
</list>
</class>
</hibernate-mapping>
22 changes: 11 additions & 11 deletions src/NHibernate/Engine/StatefulPersistenceContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -443,18 +443,18 @@ public bool ContainsEntity(EntityKey key)
/// </summary>
public object RemoveEntity(EntityKey key)
{
if (!entitiesByKey.Remove(key, out var tempObject))
throw new KeyNotFoundException(key.ToString());

object entity = tempObject;
List<EntityUniqueKey> toRemove = new List<EntityUniqueKey>();
foreach (KeyValuePair<EntityUniqueKey, object> pair in entitiesByUniqueKey)
if (entitiesByKey.Remove(key, out var entity))
{
if (pair.Value == entity) toRemove.Add(pair.Key);
}
foreach (EntityUniqueKey uniqueKey in toRemove)
{
entitiesByUniqueKey.Remove(uniqueKey);
List<EntityUniqueKey> toRemove = new List<EntityUniqueKey>();
foreach (KeyValuePair<EntityUniqueKey, object> pair in entitiesByUniqueKey)
{
if (pair.Value == entity) toRemove.Add(pair.Key);
}

foreach (EntityUniqueKey uniqueKey in toRemove)
{
entitiesByUniqueKey.Remove(uniqueKey);
}
}

entitySnapshotsByKey.Remove(key);
Expand Down

0 comments on commit 20a61d1

Please sign in to comment.