Skip to content

Commit 539a814

Browse files
Merge pull request #1871 from fredericDelaporte/propertyRefOnComponentProperty
Fix property-ref on a component's property
2 parents 3f53ab6 + 7f503d0 commit 539a814

File tree

6 files changed

+260
-1
lines changed

6 files changed

+260
-1
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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.GH1824
14+
{
15+
using System.Threading.Tasks;
16+
[TestFixture]
17+
public class FixtureAsync : BugTestCase
18+
{
19+
private object _fooId;
20+
private object _barId;
21+
22+
protected override void OnSetUp()
23+
{
24+
using (var session = OpenSession())
25+
using (var transaction = session.BeginTransaction())
26+
{
27+
var bar = new Bar
28+
{
29+
MyProps =
30+
{
31+
["DynValString2"] = "a"
32+
},
33+
StaticProps = new StaticBarComponent
34+
{
35+
StaticValString2 = "b"
36+
}
37+
};
38+
_barId = session.Save(bar);
39+
_fooId = session.Save(
40+
new Foo
41+
{
42+
MyProps =
43+
{
44+
["DynPointer"] = bar
45+
},
46+
StaticProps = new StaticFooComponent
47+
{
48+
StaticPointer = bar
49+
}
50+
});
51+
transaction.Commit();
52+
}
53+
}
54+
55+
protected override void OnTearDown()
56+
{
57+
using (var session = OpenSession())
58+
using (var transaction = session.BeginTransaction())
59+
{
60+
// The HQL delete does all the job inside the database without loading the entities, but it does
61+
// not handle delete order for avoiding violating constraints if any. Use
62+
// session.Delete("from System.Object");
63+
// instead if in need of having NHbernate ordering the deletes, but this will cause
64+
// loading the entities in the session.
65+
session.CreateQuery("delete from System.Object").ExecuteUpdate();
66+
67+
transaction.Commit();
68+
}
69+
}
70+
71+
[Test]
72+
public async Task CanReadDynPointerFromFooAsync()
73+
{
74+
using (var session = OpenSession())
75+
using (session.BeginTransaction())
76+
{
77+
var foo = await (session.GetAsync<Foo>(_fooId));
78+
var bar = foo.MyProps["DynPointer"];
79+
Assert.That(bar, Is.Not.Null);
80+
Assert.That(bar, Is.InstanceOf<Bar>());
81+
Assert.That(bar, Has.Property("Id").EqualTo(_barId));
82+
}
83+
}
84+
85+
[Test]
86+
public async Task CanReadStaticPointerFromFooAsync()
87+
{
88+
using (var session = OpenSession())
89+
using (session.BeginTransaction())
90+
{
91+
var foo = await (session.GetAsync<Foo>(_fooId));
92+
var bar = foo.StaticProps.StaticPointer;
93+
Assert.That(bar, Is.Not.Null);
94+
Assert.That(bar, Has.Property("Id").EqualTo(_barId));
95+
}
96+
}
97+
}
98+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
4+
namespace NHibernate.Test.NHSpecificTest.GH1824
5+
{
6+
public class Bar
7+
{
8+
public virtual int Id { get; set; }
9+
10+
public virtual IDictionary MyProps { get; set; } = new Dictionary<string, object>();
11+
12+
public virtual StaticBarComponent StaticProps { get; set; }
13+
}
14+
15+
public class StaticBarComponent
16+
{
17+
public string StaticValString2 { get; set; }
18+
public string YetAnotherProperty { get; set; }
19+
}
20+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
using NUnit.Framework;
2+
3+
namespace NHibernate.Test.NHSpecificTest.GH1824
4+
{
5+
[TestFixture]
6+
public class Fixture : BugTestCase
7+
{
8+
private object _fooId;
9+
private object _barId;
10+
11+
protected override void OnSetUp()
12+
{
13+
using (var session = OpenSession())
14+
using (var transaction = session.BeginTransaction())
15+
{
16+
var bar = new Bar
17+
{
18+
MyProps =
19+
{
20+
["DynValString2"] = "a"
21+
},
22+
StaticProps = new StaticBarComponent
23+
{
24+
StaticValString2 = "b"
25+
}
26+
};
27+
_barId = session.Save(bar);
28+
_fooId = session.Save(
29+
new Foo
30+
{
31+
MyProps =
32+
{
33+
["DynPointer"] = bar
34+
},
35+
StaticProps = new StaticFooComponent
36+
{
37+
StaticPointer = bar
38+
}
39+
});
40+
transaction.Commit();
41+
}
42+
}
43+
44+
protected override void OnTearDown()
45+
{
46+
using (var session = OpenSession())
47+
using (var transaction = session.BeginTransaction())
48+
{
49+
// The HQL delete does all the job inside the database without loading the entities, but it does
50+
// not handle delete order for avoiding violating constraints if any. Use
51+
// session.Delete("from System.Object");
52+
// instead if in need of having NHbernate ordering the deletes, but this will cause
53+
// loading the entities in the session.
54+
session.CreateQuery("delete from System.Object").ExecuteUpdate();
55+
56+
transaction.Commit();
57+
}
58+
}
59+
60+
[Test]
61+
public void CanReadDynPointerFromFoo()
62+
{
63+
using (var session = OpenSession())
64+
using (session.BeginTransaction())
65+
{
66+
var foo = session.Get<Foo>(_fooId);
67+
var bar = foo.MyProps["DynPointer"];
68+
Assert.That(bar, Is.Not.Null);
69+
Assert.That(bar, Is.InstanceOf<Bar>());
70+
Assert.That(bar, Has.Property("Id").EqualTo(_barId));
71+
}
72+
}
73+
74+
[Test]
75+
public void CanReadStaticPointerFromFoo()
76+
{
77+
using (var session = OpenSession())
78+
using (session.BeginTransaction())
79+
{
80+
var foo = session.Get<Foo>(_fooId);
81+
var bar = foo.StaticProps.StaticPointer;
82+
Assert.That(bar, Is.Not.Null);
83+
Assert.That(bar, Has.Property("Id").EqualTo(_barId));
84+
}
85+
}
86+
}
87+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
4+
namespace NHibernate.Test.NHSpecificTest.GH1824
5+
{
6+
public class Foo
7+
{
8+
public virtual int Id { get; set; }
9+
10+
public virtual IDictionary MyProps { get; set; } = new Dictionary<string, object>();
11+
12+
public virtual StaticFooComponent StaticProps { get; set; }
13+
}
14+
15+
public class StaticFooComponent
16+
{
17+
public Bar StaticPointer { get; set; }
18+
}
19+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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.GH1824">
5+
6+
<class name="Foo">
7+
<id type="int" name="Id">
8+
<column name="Id"/>
9+
<generator class="native"/>
10+
</id>
11+
12+
<dynamic-component name="MyProps">
13+
<many-to-one name="DynPointer" class="Bar" property-ref="MyProps.DynValString2"/>
14+
</dynamic-component>
15+
16+
<component name="StaticProps">
17+
<many-to-one name="StaticPointer" class="Bar" property-ref="StaticProps.StaticValString2"/>
18+
</component>
19+
</class>
20+
21+
<class name="Bar">
22+
<id type="int" name="Id">
23+
<column name="Id"/>
24+
<generator class="native"/>
25+
</id>
26+
<dynamic-component name="MyProps">
27+
<property name="DynValString2" type="string" unique="1"/>
28+
<property name="AnotherProperty" type="string" />
29+
</dynamic-component>
30+
<component name="StaticProps">
31+
<property name="StaticValString2" type="string" unique="1"/>
32+
<property name="YetAnotherProperty" type="string" />
33+
</component>
34+
</class>
35+
</hibernate-mapping>

src/NHibernate/Cfg/Configuration.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ public string GetIdentifierPropertyName(string className)
221221
public IType GetReferencedPropertyType(string className, string propertyName)
222222
{
223223
PersistentClass pc = GetPersistentClass(className);
224-
Property prop = pc.GetProperty(propertyName);
224+
Property prop = pc.GetReferencedProperty(propertyName);
225225

226226
if (prop == null)
227227
{

0 commit comments

Comments
 (0)