Skip to content

Commit 21975f6

Browse files
lillo42hazzik
authored andcommitted
Fix properties mapped inside a <properties> group are empty when retrieving object (#2142)
1 parent 4f4343e commit 21975f6

File tree

6 files changed

+195
-3
lines changed

6 files changed

+195
-3
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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.NH2714
14+
{
15+
using System.Threading.Tasks;
16+
[TestFixture]
17+
public class FixtureAsync : BugTestCase
18+
{
19+
private const int ExtraId = 500;
20+
21+
protected override void OnTearDown()
22+
{
23+
using (ISession s = OpenSession())
24+
{
25+
s.Delete($"from {nameof(Item)}");
26+
s.Flush();
27+
s.Delete($"from {nameof(Information)}");
28+
s.Flush();
29+
}
30+
}
31+
32+
[Test]
33+
public async Task PropertyRefUsesOtherColumnsAsync()
34+
{
35+
var information = new Information {Name = "First", ExtraId = ExtraId};
36+
37+
var item = new Item {Id = 1, Name = information.Name, ExtraId = information.ExtraId};
38+
39+
using (ISession session = OpenSession())
40+
{
41+
await (session.SaveAsync(information));
42+
await (session.SaveAsync(item));
43+
await (session.FlushAsync());
44+
}
45+
46+
using (ISession session = OpenSession())
47+
{
48+
var otherInformation = await (session.GetAsync<Information>(information.Id));
49+
Assert.That(otherInformation.Items.Count, Is.EqualTo(1));
50+
}
51+
}
52+
53+
[Test]
54+
public async Task ChildKeyPropertiesOfParentAreRetrievedAsync()
55+
{
56+
var information = new Information {Name = "First", ExtraId = ExtraId};
57+
58+
var item = new Item {Id = 1, Name = information.Name, ExtraId = information.ExtraId};
59+
60+
using (ISession session = OpenSession())
61+
{
62+
await (session.SaveAsync(information));
63+
await (session.SaveAsync(item));
64+
await (session.FlushAsync());
65+
}
66+
67+
using (ISession session = OpenSession())
68+
{
69+
var otherInformation = await (session.GetAsync<Information>(information.Id));
70+
71+
Assert.That(otherInformation.Name, Is.EqualTo(information.Name));
72+
Assert.That(otherInformation.ExtraId, Is.EqualTo(information.ExtraId));
73+
}
74+
}
75+
}
76+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using NUnit.Framework;
2+
3+
namespace NHibernate.Test.NHSpecificTest.NH2714
4+
{
5+
[TestFixture]
6+
public class Fixture : BugTestCase
7+
{
8+
private const int ExtraId = 500;
9+
10+
protected override void OnTearDown()
11+
{
12+
using (ISession s = OpenSession())
13+
{
14+
s.Delete($"from {nameof(Item)}");
15+
s.Flush();
16+
s.Delete($"from {nameof(Information)}");
17+
s.Flush();
18+
}
19+
}
20+
21+
[Test]
22+
public void PropertyRefUsesOtherColumns()
23+
{
24+
var information = new Information {Name = "First", ExtraId = ExtraId};
25+
26+
var item = new Item {Id = 1, Name = information.Name, ExtraId = information.ExtraId};
27+
28+
using (ISession session = OpenSession())
29+
{
30+
session.Save(information);
31+
session.Save(item);
32+
session.Flush();
33+
}
34+
35+
using (ISession session = OpenSession())
36+
{
37+
var otherInformation = session.Get<Information>(information.Id);
38+
Assert.That(otherInformation.Items.Count, Is.EqualTo(1));
39+
}
40+
}
41+
42+
[Test]
43+
public void ChildKeyPropertiesOfParentAreRetrieved()
44+
{
45+
var information = new Information {Name = "First", ExtraId = ExtraId};
46+
47+
var item = new Item {Id = 1, Name = information.Name, ExtraId = information.ExtraId};
48+
49+
using (ISession session = OpenSession())
50+
{
51+
session.Save(information);
52+
session.Save(item);
53+
session.Flush();
54+
}
55+
56+
using (ISession session = OpenSession())
57+
{
58+
var otherInformation = session.Get<Information>(information.Id);
59+
60+
Assert.That(otherInformation.Name, Is.EqualTo(information.Name));
61+
Assert.That(otherInformation.ExtraId, Is.EqualTo(information.ExtraId));
62+
}
63+
}
64+
}
65+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System.Collections.Generic;
2+
3+
namespace NHibernate.Test.NHSpecificTest.NH2714
4+
{
5+
public class Information
6+
{
7+
public virtual int Id { get; set; }
8+
public virtual string Name { get; set; }
9+
public virtual int ExtraId { get; set; }
10+
public virtual ISet<Item> Items { get; set; } = new HashSet<Item>();
11+
}
12+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace NHibernate.Test.NHSpecificTest.NH2714
2+
{
3+
public class Item
4+
{
5+
public virtual int Id { get; set; }
6+
public virtual string Name { get; set; }
7+
public virtual int ExtraId { get; set; }
8+
}
9+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernate.Test" namespace="NHibernate.Test.NHSpecificTest.NH2714">
3+
<class name="Information" table="info" lazy="false">
4+
<id name="Id" column="id">
5+
<generator class="native" />
6+
</id>
7+
<properties name="pref">
8+
<property name="Name" column="aName" />
9+
<property name="ExtraId" column="aExtraId" />
10+
</properties>
11+
<set name="Items" lazy="true">
12+
<key property-ref="pref">
13+
<column name="aName" />
14+
<column name="aExtraId" />
15+
</key>
16+
<one-to-many class="Item" />
17+
</set>
18+
</class>
19+
20+
<class name="Item" table="itens">
21+
<id name="Id" column="id">
22+
<generator class="assigned" />
23+
</id>
24+
<property name="Name" column="aName" />
25+
<property name="ExtraId" column="aExtraId" />
26+
</class>
27+
</hibernate-mapping>

src/NHibernate/Type/EmbeddedComponentType.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,12 @@ public override bool IsEmbedded
1919

2020
public override object Instantiate(object parent, ISessionImplementor session)
2121
{
22-
bool useParent= false;
23-
// NH Different implementation : since we are not sure about why H3.2 use the "parent"
24-
//useParent = parent != null && base.ReturnedClass.IsInstanceOfType(parent);
22+
var useParent =
23+
parent != null &&
24+
//TODO: Yuck! This is not quite good enough, it's a quick
25+
//hack around the problem of having a to-one association
26+
//that refers to an embedded component:
27+
ReturnedClass.IsInstanceOfType(parent);
2528

2629
return useParent ? parent : base.Instantiate(parent, session);
2730
}

0 commit comments

Comments
 (0)