Skip to content

Property-ref on many-to-one with composite id fails #1918

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
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
59 changes: 59 additions & 0 deletions src/NHibernate.Test/Async/NHSpecificTest/GH1918/Fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by AsyncGenerator.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------


using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.GH1918
{
using System.Threading.Tasks;
[TestFixture]
public class FixtureAsync : BugTestCase
{
private const string _ingData = "data_ing";
private readonly EmbId _ingId = new EmbId { X = 5, Y = 6 };

protected override void OnSetUp()
{
var edId = new EmbId { X = 1, Y = 2 };
var ed = new BiEmbIdRefEdEntity { Id = edId, Data = "data_ed" };
var ing = new BiEmbIdRefIngEntity { Id = _ingId, Data = _ingData, Reference = ed };
using (var s = OpenSession())
using (var tx = s.BeginTransaction())
{
s.Save(ed);
s.Save(ing);
tx.Commit();
}
}

[Test]
public async Task CanReadBidirectionalEntitiesWithEmbeddedIdAsync()
{
using (var s = OpenSession())
using (var tx = s.BeginTransaction())
{
var ingEntity = await (s.GetAsync<BiEmbIdRefIngEntity>(_ingId));
Assert.That(ingEntity.Data, Is.EqualTo(_ingData));
await (tx.CommitAsync());
}
}

protected override void OnTearDown()
{
using (var session = OpenSession())
using (var tx = session.BeginTransaction())
{
session.CreateQuery("delete from BiEmbIdRefIngEntity").ExecuteUpdate();
session.CreateQuery("delete from BiEmbIdRefEdEntity").ExecuteUpdate();
tx.Commit();
}
}
}
}
22 changes: 22 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH1918/BiEmbIdRefEdEntity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace NHibernate.Test.NHSpecificTest.GH1918
{
public class BiEmbIdRefEdEntity
{
public virtual EmbId Id { get; set; }
public virtual string Data { get; set; }
public virtual BiEmbIdRefIngEntity Referencing { get; set; }

public override bool Equals(object obj)
{
var casted = obj as BiEmbIdRefEdEntity;
if (casted == null)
return false;
return Id.Equals(casted.Id) && Data.Equals(casted.Data);
}

public override int GetHashCode()
{
return Id.GetHashCode() ^ Data.GetHashCode();
}
}
}
22 changes: 22 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH1918/BiEmbIdRefIngEntity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace NHibernate.Test.NHSpecificTest.GH1918
{
public class BiEmbIdRefIngEntity
{
public virtual EmbId Id { get; set; }
public virtual string Data { get; set; }
public virtual BiEmbIdRefEdEntity Reference { get; set; }

public override bool Equals(object obj)
{
var casted = obj as BiEmbIdRefIngEntity;
if (casted == null)
return false;
return Id.Equals(casted.Id) && Data.Equals(casted.Data);
}

public override int GetHashCode()
{
return Id.GetHashCode() ^ Data.GetHashCode();
}
}
}
21 changes: 21 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH1918/EmbId.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace NHibernate.Test.NHSpecificTest.GH1918
{
public class EmbId
{
public virtual int X { get; set; }
public virtual int Y { get; set; }

public override bool Equals(object obj)
{
var casted = obj as EmbId;
if (casted == null)
return false;
return X == casted.X && Y == casted.Y;
}

public override int GetHashCode()
{
return X ^ Y;
}
}
}
48 changes: 48 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH1918/Fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.GH1918
{
[TestFixture]
public class Fixture : BugTestCase
{
private const string _ingData = "data_ing";
private readonly EmbId _ingId = new EmbId { X = 5, Y = 6 };

protected override void OnSetUp()
{
var edId = new EmbId { X = 1, Y = 2 };
var ed = new BiEmbIdRefEdEntity { Id = edId, Data = "data_ed" };
var ing = new BiEmbIdRefIngEntity { Id = _ingId, Data = _ingData, Reference = ed };
using (var s = OpenSession())
using (var tx = s.BeginTransaction())
{
s.Save(ed);
s.Save(ing);
tx.Commit();
}
}

[Test]
public void CanReadBidirectionalEntitiesWithEmbeddedId()
{
using (var s = OpenSession())
using (var tx = s.BeginTransaction())
{
var ingEntity = s.Get<BiEmbIdRefIngEntity>(_ingId);
Assert.That(ingEntity.Data, Is.EqualTo(_ingData));
tx.Commit();
}
}

protected override void OnTearDown()
{
using (var session = OpenSession())
using (var tx = session.BeginTransaction())
{
session.CreateQuery("delete from BiEmbIdRefIngEntity").ExecuteUpdate();
session.CreateQuery("delete from BiEmbIdRefEdEntity").ExecuteUpdate();
tx.Commit();
}
}
}
}
28 changes: 28 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH1918/Mappings.hbm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="NHibernate.Test"
namespace="NHibernate.Test.NHSpecificTest.GH1918">

<class name="BiEmbIdRefEdEntity">
<composite-id name="Id">
<key-property name="X"/>
<key-property name="Y"/>
</composite-id>
<property name="Data" />
<one-to-one name="Referencing"
class="BiEmbIdRefIngEntity"
property-ref="Reference"/>
</class>

<class name="BiEmbIdRefIngEntity">
<composite-id name="Id">
<key-property name="X"/>
<key-property name="Y"/>
</composite-id>
<property name="Data" />
<many-to-one name="Reference">
<column name="RefX" />
<column name="RefY" />
</many-to-one>
</class>
</hibernate-mapping>
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,9 @@ public async Task CacheByUniqueKeysAsync(object entity, ISessionImplementor sess
if (propertyValue == null)
continue;
var type = PropertyTypes[i].GetSemiResolvedType(session.Factory);
propertyValue = await (type.SemiResolveAsync(propertyValue, session, entity, cancellationToken)).ConfigureAwait(false);

if (!type.ReturnedClass.IsInstanceOfType(propertyValue))
propertyValue = await (type.SemiResolveAsync(propertyValue, session, entity, cancellationToken)).ConfigureAwait(false);
var euk = new EntityUniqueKey(EntityName, PropertyNames[i], propertyValue, type, session.Factory);
session.PersistenceContext.AddEntity(euk, entity);
}
Expand Down
4 changes: 3 additions & 1 deletion src/NHibernate/Persister/Entity/AbstractEntityPersister.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2140,7 +2140,9 @@ public void CacheByUniqueKeys(object entity, ISessionImplementor session)
if (propertyValue == null)
continue;
var type = PropertyTypes[i].GetSemiResolvedType(session.Factory);
propertyValue = type.SemiResolve(propertyValue, session, entity);

if (!type.ReturnedClass.IsInstanceOfType(propertyValue))
propertyValue = type.SemiResolve(propertyValue, session, entity);
var euk = new EntityUniqueKey(EntityName, PropertyNames[i], propertyValue, type, session.Factory);
session.PersistenceContext.AddEntity(euk, entity);
}
Expand Down