Skip to content

Commit 6f38e13

Browse files
RogerKratzhazzikfredericDelaporte
committed
Fix property-ref on many-to-one with composite id fails (#1918)
Co-authored-by: Alexander Zaytsev <[email protected]> Co-authored-by: Frédéric Delaporte <[email protected]>
1 parent fdac0d0 commit 6f38e13

File tree

8 files changed

+206
-2
lines changed

8 files changed

+206
-2
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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 NUnit.Framework;
12+
13+
namespace NHibernate.Test.NHSpecificTest.GH1918
14+
{
15+
using System.Threading.Tasks;
16+
[TestFixture]
17+
public class FixtureAsync : BugTestCase
18+
{
19+
private const string _ingData = "data_ing";
20+
private readonly EmbId _ingId = new EmbId { X = 5, Y = 6 };
21+
22+
protected override void OnSetUp()
23+
{
24+
var edId = new EmbId { X = 1, Y = 2 };
25+
var ed = new BiEmbIdRefEdEntity { Id = edId, Data = "data_ed" };
26+
var ing = new BiEmbIdRefIngEntity { Id = _ingId, Data = _ingData, Reference = ed };
27+
using (var s = OpenSession())
28+
using (var tx = s.BeginTransaction())
29+
{
30+
s.Save(ed);
31+
s.Save(ing);
32+
tx.Commit();
33+
}
34+
}
35+
36+
[Test]
37+
public async Task CanReadBidirectionalEntitiesWithEmbeddedIdAsync()
38+
{
39+
using (var s = OpenSession())
40+
using (var tx = s.BeginTransaction())
41+
{
42+
var ingEntity = await (s.GetAsync<BiEmbIdRefIngEntity>(_ingId));
43+
Assert.That(ingEntity.Data, Is.EqualTo(_ingData));
44+
await (tx.CommitAsync());
45+
}
46+
}
47+
48+
protected override void OnTearDown()
49+
{
50+
using (var session = OpenSession())
51+
using (var tx = session.BeginTransaction())
52+
{
53+
session.CreateQuery("delete from BiEmbIdRefIngEntity").ExecuteUpdate();
54+
session.CreateQuery("delete from BiEmbIdRefEdEntity").ExecuteUpdate();
55+
tx.Commit();
56+
}
57+
}
58+
}
59+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
namespace NHibernate.Test.NHSpecificTest.GH1918
2+
{
3+
public class BiEmbIdRefEdEntity
4+
{
5+
public virtual EmbId Id { get; set; }
6+
public virtual string Data { get; set; }
7+
public virtual BiEmbIdRefIngEntity Referencing { get; set; }
8+
9+
public override bool Equals(object obj)
10+
{
11+
var casted = obj as BiEmbIdRefEdEntity;
12+
if (casted == null)
13+
return false;
14+
return Id.Equals(casted.Id) && Data.Equals(casted.Data);
15+
}
16+
17+
public override int GetHashCode()
18+
{
19+
return Id.GetHashCode() ^ Data.GetHashCode();
20+
}
21+
}
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
namespace NHibernate.Test.NHSpecificTest.GH1918
2+
{
3+
public class BiEmbIdRefIngEntity
4+
{
5+
public virtual EmbId Id { get; set; }
6+
public virtual string Data { get; set; }
7+
public virtual BiEmbIdRefEdEntity Reference { get; set; }
8+
9+
public override bool Equals(object obj)
10+
{
11+
var casted = obj as BiEmbIdRefIngEntity;
12+
if (casted == null)
13+
return false;
14+
return Id.Equals(casted.Id) && Data.Equals(casted.Data);
15+
}
16+
17+
public override int GetHashCode()
18+
{
19+
return Id.GetHashCode() ^ Data.GetHashCode();
20+
}
21+
}
22+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
namespace NHibernate.Test.NHSpecificTest.GH1918
2+
{
3+
public class EmbId
4+
{
5+
public virtual int X { get; set; }
6+
public virtual int Y { get; set; }
7+
8+
public override bool Equals(object obj)
9+
{
10+
var casted = obj as EmbId;
11+
if (casted == null)
12+
return false;
13+
return X == casted.X && Y == casted.Y;
14+
}
15+
16+
public override int GetHashCode()
17+
{
18+
return X ^ Y;
19+
}
20+
}
21+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using NUnit.Framework;
2+
3+
namespace NHibernate.Test.NHSpecificTest.GH1918
4+
{
5+
[TestFixture]
6+
public class Fixture : BugTestCase
7+
{
8+
private const string _ingData = "data_ing";
9+
private readonly EmbId _ingId = new EmbId { X = 5, Y = 6 };
10+
11+
protected override void OnSetUp()
12+
{
13+
var edId = new EmbId { X = 1, Y = 2 };
14+
var ed = new BiEmbIdRefEdEntity { Id = edId, Data = "data_ed" };
15+
var ing = new BiEmbIdRefIngEntity { Id = _ingId, Data = _ingData, Reference = ed };
16+
using (var s = OpenSession())
17+
using (var tx = s.BeginTransaction())
18+
{
19+
s.Save(ed);
20+
s.Save(ing);
21+
tx.Commit();
22+
}
23+
}
24+
25+
[Test]
26+
public void CanReadBidirectionalEntitiesWithEmbeddedId()
27+
{
28+
using (var s = OpenSession())
29+
using (var tx = s.BeginTransaction())
30+
{
31+
var ingEntity = s.Get<BiEmbIdRefIngEntity>(_ingId);
32+
Assert.That(ingEntity.Data, Is.EqualTo(_ingData));
33+
tx.Commit();
34+
}
35+
}
36+
37+
protected override void OnTearDown()
38+
{
39+
using (var session = OpenSession())
40+
using (var tx = session.BeginTransaction())
41+
{
42+
session.CreateQuery("delete from BiEmbIdRefIngEntity").ExecuteUpdate();
43+
session.CreateQuery("delete from BiEmbIdRefEdEntity").ExecuteUpdate();
44+
tx.Commit();
45+
}
46+
}
47+
}
48+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
3+
assembly="NHibernate.Test"
4+
namespace="NHibernate.Test.NHSpecificTest.GH1918">
5+
6+
<class name="BiEmbIdRefEdEntity">
7+
<composite-id name="Id">
8+
<key-property name="X"/>
9+
<key-property name="Y"/>
10+
</composite-id>
11+
<property name="Data" />
12+
<one-to-one name="Referencing"
13+
class="BiEmbIdRefIngEntity"
14+
property-ref="Reference"/>
15+
</class>
16+
17+
<class name="BiEmbIdRefIngEntity">
18+
<composite-id name="Id">
19+
<key-property name="X"/>
20+
<key-property name="Y"/>
21+
</composite-id>
22+
<property name="Data" />
23+
<many-to-one name="Reference">
24+
<column name="RefX" />
25+
<column name="RefY" />
26+
</many-to-one>
27+
</class>
28+
</hibernate-mapping>

src/NHibernate/Async/Persister/Entity/AbstractEntityPersister.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,9 @@ public async Task CacheByUniqueKeysAsync(object entity, ISessionImplementor sess
270270
if (propertyValue == null)
271271
continue;
272272
var type = PropertyTypes[i].GetSemiResolvedType(session.Factory);
273-
propertyValue = await (type.SemiResolveAsync(propertyValue, session, entity, cancellationToken)).ConfigureAwait(false);
273+
274+
if (!type.ReturnedClass.IsInstanceOfType(propertyValue))
275+
propertyValue = await (type.SemiResolveAsync(propertyValue, session, entity, cancellationToken)).ConfigureAwait(false);
274276
var euk = new EntityUniqueKey(EntityName, PropertyNames[i], propertyValue, type, session.Factory);
275277
session.PersistenceContext.AddEntity(euk, entity);
276278
}

src/NHibernate/Persister/Entity/AbstractEntityPersister.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2140,7 +2140,9 @@ public void CacheByUniqueKeys(object entity, ISessionImplementor session)
21402140
if (propertyValue == null)
21412141
continue;
21422142
var type = PropertyTypes[i].GetSemiResolvedType(session.Factory);
2143-
propertyValue = type.SemiResolve(propertyValue, session, entity);
2143+
2144+
if (!type.ReturnedClass.IsInstanceOfType(propertyValue))
2145+
propertyValue = type.SemiResolve(propertyValue, session, entity);
21442146
var euk = new EntityUniqueKey(EntityName, PropertyNames[i], propertyValue, type, session.Factory);
21452147
session.PersistenceContext.AddEntity(euk, entity);
21462148
}

0 commit comments

Comments
 (0)