Skip to content

Fix properties mapped inside a <properties> group are empty when retrieving object #2142

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 4 commits into from
Apr 30, 2019
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
76 changes: 76 additions & 0 deletions src/NHibernate.Test/Async/NHSpecificTest/NH2714/Fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
//------------------------------------------------------------------------------
// <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.NH2714
{
using System.Threading.Tasks;
[TestFixture]
public class FixtureAsync : BugTestCase
{
private const int ExtraId = 500;

protected override void OnTearDown()
{
using (ISession s = OpenSession())
{
s.Delete($"from {nameof(Item)}");
s.Flush();
s.Delete($"from {nameof(Information)}");
s.Flush();
}
}

[Test]
public async Task PropertyRefUsesOtherColumnsAsync()
{
var information = new Information {Name = "First", ExtraId = ExtraId};

var item = new Item {Id = 1, Name = information.Name, ExtraId = information.ExtraId};

using (ISession session = OpenSession())
{
await (session.SaveAsync(information));
await (session.SaveAsync(item));
await (session.FlushAsync());
}

using (ISession session = OpenSession())
{
var otherInformation = await (session.GetAsync<Information>(information.Id));
Assert.That(otherInformation.Items.Count, Is.EqualTo(1));
}
}

[Test]
public async Task ChildKeyPropertiesOfParentAreRetrievedAsync()
{
var information = new Information {Name = "First", ExtraId = ExtraId};

var item = new Item {Id = 1, Name = information.Name, ExtraId = information.ExtraId};

using (ISession session = OpenSession())
{
await (session.SaveAsync(information));
await (session.SaveAsync(item));
await (session.FlushAsync());
}

using (ISession session = OpenSession())
{
var otherInformation = await (session.GetAsync<Information>(information.Id));

Assert.That(otherInformation.Name, Is.EqualTo(information.Name));
Assert.That(otherInformation.ExtraId, Is.EqualTo(information.ExtraId));
}
}
}
}
65 changes: 65 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/NH2714/Fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.NH2714
{
[TestFixture]
public class Fixture : BugTestCase
{
private const int ExtraId = 500;

protected override void OnTearDown()
{
using (ISession s = OpenSession())
{
s.Delete($"from {nameof(Item)}");
s.Flush();
s.Delete($"from {nameof(Information)}");
s.Flush();
}
}

[Test]
public void PropertyRefUsesOtherColumns()
{
var information = new Information {Name = "First", ExtraId = ExtraId};

var item = new Item {Id = 1, Name = information.Name, ExtraId = information.ExtraId};

using (ISession session = OpenSession())
{
session.Save(information);
session.Save(item);
session.Flush();
}

using (ISession session = OpenSession())
{
var otherInformation = session.Get<Information>(information.Id);
Assert.That(otherInformation.Items.Count, Is.EqualTo(1));
}
}

[Test]
public void ChildKeyPropertiesOfParentAreRetrieved()
{
var information = new Information {Name = "First", ExtraId = ExtraId};

var item = new Item {Id = 1, Name = information.Name, ExtraId = information.ExtraId};

using (ISession session = OpenSession())
{
session.Save(information);
session.Save(item);
session.Flush();
}

using (ISession session = OpenSession())
{
var otherInformation = session.Get<Information>(information.Id);

Assert.That(otherInformation.Name, Is.EqualTo(information.Name));
Assert.That(otherInformation.ExtraId, Is.EqualTo(information.ExtraId));
}
}
}
}
12 changes: 12 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/NH2714/Information.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Collections.Generic;

namespace NHibernate.Test.NHSpecificTest.NH2714
{
public class Information
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual int ExtraId { get; set; }
public virtual ISet<Item> Items { get; set; } = new HashSet<Item>();
}
}
9 changes: 9 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/NH2714/Item.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace NHibernate.Test.NHSpecificTest.NH2714
{
public class Item
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual int ExtraId { get; set; }
}
}
27 changes: 27 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/NH2714/Mappings.hbm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernate.Test" namespace="NHibernate.Test.NHSpecificTest.NH2714">
<class name="Information" table="info" lazy="false">
<id name="Id" column="id">
<generator class="native" />
</id>
<properties name="pref">
<property name="Name" column="aName" />
<property name="ExtraId" column="aExtraId" />
</properties>
<set name="Items" lazy="true">
<key property-ref="pref">
<column name="aName" />
<column name="aExtraId" />
</key>
<one-to-many class="Item" />
</set>
</class>

<class name="Item" table="itens">
<id name="Id" column="id">
<generator class="assigned" />
</id>
<property name="Name" column="aName" />
<property name="ExtraId" column="aExtraId" />
</class>
</hibernate-mapping>
9 changes: 6 additions & 3 deletions src/NHibernate/Type/EmbeddedComponentType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@ public override bool IsEmbedded

public override object Instantiate(object parent, ISessionImplementor session)
{
bool useParent= false;
// NH Different implementation : since we are not sure about why H3.2 use the "parent"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, now we're sure why this code was written like this...

//useParent = parent != null && base.ReturnedClass.IsInstanceOfType(parent);
var useParent =
parent != null &&
//TODO: Yuck! This is not quite good enough, it's a quick
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

//hack around the problem of having a to-one association
//that refers to an embedded component:
ReturnedClass.IsInstanceOfType(parent);

return useParent ? parent : base.Instantiate(parent, session);
}
Expand Down