Skip to content

Commit 42a4a40

Browse files
Fix one-to-one with property-ref stack overflow
Fix key type mismatch in EntityUniqueKey EntityUniqueKey was assuming the value to be always semi-resolved (to be the identifier instead of the entity for an entity type). It was always computing the value hashcode with the semi-resolved type, which is the identifier type for an entity. But this has changed with #1492, which caches unique keys also for already fully loaded entities. The value hashcode must then be computed with the entity type, which accounts for proxies. Otherwise the proxy loading may get triggered while the code is already loading the proxy, which results in a stack overflow. Add test case adapted from Michael Estermann repro Fix #1645 Co-authored-by: Michael Estermann <[email protected]>
1 parent 6e8f190 commit 42a4a40

File tree

8 files changed

+221
-2
lines changed

8 files changed

+221
-2
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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.GH1645
15+
{
16+
using System.Threading.Tasks;
17+
[TestFixture]
18+
public class FixtureAsync : BugTestCase
19+
{
20+
private Guid _superParentId;
21+
private Guid _parentId;
22+
23+
protected override void OnSetUp()
24+
{
25+
using (var session = OpenSession())
26+
using (var transaction = session.BeginTransaction())
27+
{
28+
var p = new Parent();
29+
session.Save(p);
30+
_parentId = p.Id;
31+
32+
_superParentId = (Guid) session.Save(new SuperParent { Parent = p });
33+
34+
transaction.Commit();
35+
}
36+
}
37+
38+
protected override void OnTearDown()
39+
{
40+
using (var session = OpenSession())
41+
using (var transaction = session.BeginTransaction())
42+
{
43+
// The HQL delete does all the job inside the database without loading the entities, but it does
44+
// not handle delete order for avoiding violating constraints if any. Use
45+
// session.Delete("from System.Object");
46+
// instead if in need of having NHbernate ordering the deletes, but this will cause
47+
// loading the entities in the session.
48+
session.CreateQuery("delete from System.Object").ExecuteUpdate();
49+
50+
transaction.Commit();
51+
}
52+
}
53+
54+
[Test]
55+
public async Task SOEOnLoadAsync()
56+
{
57+
using (var session = OpenSession())
58+
using (session.BeginTransaction())
59+
{
60+
var superParent = await (session.LoadAsync<SuperParent>(_superParentId));
61+
Assert.That(() => NHibernateUtil.InitializeAsync(superParent), Throws.Nothing);
62+
Assert.That(() => NHibernateUtil.InitializeAsync(superParent.Parent), Throws.Nothing);
63+
}
64+
}
65+
}
66+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using System;
2+
3+
namespace NHibernate.Test.NHSpecificTest.GH1645
4+
{
5+
public abstract class EntityBase
6+
{
7+
public virtual Guid Id { get; protected set; }
8+
9+
public static bool operator ==(EntityBase left, EntityBase right)
10+
{
11+
return Equals(left, right);
12+
}
13+
14+
public static bool operator !=(EntityBase left, EntityBase right)
15+
{
16+
return !Equals(left, right);
17+
}
18+
19+
public override bool Equals(object obj)
20+
{
21+
return Equals(obj as EntityBase);
22+
}
23+
24+
public virtual bool Equals(EntityBase other)
25+
{
26+
if (other == null)
27+
{
28+
return false;
29+
}
30+
31+
if (ReferenceEquals(this, other))
32+
{
33+
return true;
34+
}
35+
36+
if (!IsTransient(this) && !IsTransient(other) && Equals(Id, other.Id))
37+
{
38+
var otherType = other.GetUnproxiedType();
39+
var thisType = GetUnproxiedType();
40+
return thisType.IsAssignableFrom(otherType) ||
41+
otherType.IsAssignableFrom(thisType);
42+
}
43+
44+
return false;
45+
}
46+
47+
public override int GetHashCode()
48+
{
49+
if (Equals(Id, default(Guid)))
50+
{
51+
return base.GetHashCode();
52+
}
53+
54+
return Id.GetHashCode();
55+
}
56+
57+
private static bool IsTransient(EntityBase obj)
58+
{
59+
return obj != null && Equals(obj.Id, default(Guid));
60+
}
61+
62+
private System.Type GetUnproxiedType()
63+
{
64+
return GetType();
65+
}
66+
}
67+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System;
2+
using NUnit.Framework;
3+
4+
namespace NHibernate.Test.NHSpecificTest.GH1645
5+
{
6+
[TestFixture]
7+
public class Fixture : BugTestCase
8+
{
9+
private Guid _superParentId;
10+
private Guid _parentId;
11+
12+
protected override void OnSetUp()
13+
{
14+
using (var session = OpenSession())
15+
using (var transaction = session.BeginTransaction())
16+
{
17+
var p = new Parent();
18+
session.Save(p);
19+
_parentId = p.Id;
20+
21+
_superParentId = (Guid) session.Save(new SuperParent { Parent = p });
22+
23+
transaction.Commit();
24+
}
25+
}
26+
27+
protected override void OnTearDown()
28+
{
29+
using (var session = OpenSession())
30+
using (var transaction = session.BeginTransaction())
31+
{
32+
// The HQL delete does all the job inside the database without loading the entities, but it does
33+
// not handle delete order for avoiding violating constraints if any. Use
34+
// session.Delete("from System.Object");
35+
// instead if in need of having NHbernate ordering the deletes, but this will cause
36+
// loading the entities in the session.
37+
session.CreateQuery("delete from System.Object").ExecuteUpdate();
38+
39+
transaction.Commit();
40+
}
41+
}
42+
43+
[Test]
44+
public void SOEOnLoad()
45+
{
46+
using (var session = OpenSession())
47+
using (session.BeginTransaction())
48+
{
49+
var superParent = session.Load<SuperParent>(_superParentId);
50+
Assert.That(() => NHibernateUtil.Initialize(superParent), Throws.Nothing);
51+
Assert.That(() => NHibernateUtil.Initialize(superParent.Parent), Throws.Nothing);
52+
}
53+
}
54+
}
55+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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.GH1645">
4+
5+
<class name="SuperParent">
6+
<id name="Id" generator="guid.comb"/>
7+
<many-to-one name="Parent"/>
8+
</class>
9+
10+
<class name="Parent">
11+
<id name="Id" generator="guid.comb"/>
12+
<one-to-one name="SuperParent" property-ref="Parent"/>
13+
</class>
14+
15+
</hibernate-mapping>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace NHibernate.Test.NHSpecificTest.GH1645
2+
{
3+
public class Parent : EntityBase
4+
{
5+
public virtual SuperParent SuperParent { get; set; }
6+
}
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace NHibernate.Test.NHSpecificTest.GH1645
2+
{
3+
public class SuperParent : EntityBase
4+
{
5+
public virtual Parent Parent { get; set; }
6+
}
7+
}

src/NHibernate/Engine/EntityUniqueKey.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public EntityUniqueKey(string entityName, string uniqueKeyName, object semiResol
3434
this.entityName = entityName;
3535
this.uniqueKeyName = uniqueKeyName;
3636
key = semiResolvedKey;
37-
this.keyType = keyType.GetSemiResolvedType(factory);
37+
this.keyType = keyType;
3838
hashCode = GenerateHashCode(factory);
3939
}
4040

@@ -88,4 +88,4 @@ public override string ToString()
8888
}
8989

9090
}
91-
}
91+
}

src/NHibernate/Loader/Loader.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,6 +1014,8 @@ private void CacheByUniqueKey(int i, IEntityPersister persister, object obj, ISe
10141014
if (ukValue == null)
10151015
return;
10161016
var type = persister.PropertyTypes[index];
1017+
if (!alreadyLoaded)
1018+
type = type.GetSemiResolvedType(session.Factory);
10171019
var euk = new EntityUniqueKey(persister.EntityName, ukName, ukValue, type, session.Factory);
10181020
session.PersistenceContext.AddEntity(euk, obj);
10191021
}

0 commit comments

Comments
 (0)