Skip to content

Fix KeyNotFoundException in StatefulPersistenceContext.RemoveEntity on Evict #2389

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

Merged
merged 3 commits into from
May 20, 2020
Merged
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
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