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 3 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>
15 changes: 12 additions & 3 deletions src/NHibernate/Async/Type/ComponentType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -324,9 +324,18 @@ public override Task<object> SemiResolveAsync(object value, ISessionImplementor
{
return Task.FromCanceled<object>(cancellationToken);
}
//note that this implementation is kinda broken
//for components with many-to-one associations
return ResolveIdentifierAsync(value, session, owner, cancellationToken);
try
{
//note that this implementation is kinda broken
//for components with many-to-one associations
if (ReturnedClass.IsInstanceOfType(value))
return Task.FromResult<object>(value);
return ResolveIdentifierAsync(value, session, owner, cancellationToken);
}
catch (Exception ex)
{
return Task.FromException<object>(ex);
}
}

public override async Task<bool> IsModifiedAsync(object old, object current, bool[] checkable, ISessionImplementor session, CancellationToken cancellationToken)
Expand Down
2 changes: 2 additions & 0 deletions src/NHibernate/Type/ComponentType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,8 @@ public override object SemiResolve(object value, ISessionImplementor session, ob
{
//note that this implementation is kinda broken
//for components with many-to-one associations
if (ReturnedClass.IsInstanceOfType(value))
return value;
return ResolveIdentifier(value, session, owner);
}

Expand Down