Skip to content

Fix property-ref on a component's property #1871

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
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
98 changes: 98 additions & 0 deletions src/NHibernate.Test/Async/NHSpecificTest/GH1824/Fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
//------------------------------------------------------------------------------
// <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.GH1824
{
using System.Threading.Tasks;
[TestFixture]
public class FixtureAsync : BugTestCase
{
private object _fooId;
private object _barId;

protected override void OnSetUp()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
var bar = new Bar
{
MyProps =
{
["DynValString2"] = "a"
},
StaticProps = new StaticBarComponent
{
StaticValString2 = "b"
}
};
_barId = session.Save(bar);
_fooId = session.Save(
new Foo
{
MyProps =
{
["DynPointer"] = bar
},
StaticProps = new StaticFooComponent
{
StaticPointer = bar
}
});
transaction.Commit();
}
}

protected override void OnTearDown()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
// The HQL delete does all the job inside the database without loading the entities, but it does
// not handle delete order for avoiding violating constraints if any. Use
// session.Delete("from System.Object");
// instead if in need of having NHbernate ordering the deletes, but this will cause
// loading the entities in the session.
session.CreateQuery("delete from System.Object").ExecuteUpdate();

transaction.Commit();
}
}

[Test]
public async Task CanReadDynPointerFromFooAsync()
{
using (var session = OpenSession())
using (session.BeginTransaction())
{
var foo = await (session.GetAsync<Foo>(_fooId));
var bar = foo.MyProps["DynPointer"];
Assert.That(bar, Is.Not.Null);
Assert.That(bar, Is.InstanceOf<Bar>());
Assert.That(bar, Has.Property("Id").EqualTo(_barId));
}
}

[Test]
public async Task CanReadStaticPointerFromFooAsync()
{
using (var session = OpenSession())
using (session.BeginTransaction())
{
var foo = await (session.GetAsync<Foo>(_fooId));
var bar = foo.StaticProps.StaticPointer;
Assert.That(bar, Is.Not.Null);
Assert.That(bar, Has.Property("Id").EqualTo(_barId));
}
}
}
}
20 changes: 20 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH1824/Bar.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Collections;
using System.Collections.Generic;

namespace NHibernate.Test.NHSpecificTest.GH1824
{
public class Bar
{
public virtual int Id { get; set; }

public virtual IDictionary MyProps { get; set; } = new Dictionary<string, object>();

public virtual StaticBarComponent StaticProps { get; set; }
}

public class StaticBarComponent
{
public string StaticValString2 { get; set; }
public string YetAnotherProperty { get; set; }
}
}
87 changes: 87 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH1824/Fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.GH1824
{
[TestFixture]
public class Fixture : BugTestCase
{
private object _fooId;
private object _barId;

protected override void OnSetUp()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
var bar = new Bar
{
MyProps =
{
["DynValString2"] = "a"
},
StaticProps = new StaticBarComponent
{
StaticValString2 = "b"
}
};
_barId = session.Save(bar);
_fooId = session.Save(
new Foo
{
MyProps =
{
["DynPointer"] = bar
},
StaticProps = new StaticFooComponent
{
StaticPointer = bar
}
});
transaction.Commit();
}
}

protected override void OnTearDown()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
// The HQL delete does all the job inside the database without loading the entities, but it does
// not handle delete order for avoiding violating constraints if any. Use
// session.Delete("from System.Object");
// instead if in need of having NHbernate ordering the deletes, but this will cause
// loading the entities in the session.
session.CreateQuery("delete from System.Object").ExecuteUpdate();

transaction.Commit();
}
}

[Test]
public void CanReadDynPointerFromFoo()
{
using (var session = OpenSession())
using (session.BeginTransaction())
{
var foo = session.Get<Foo>(_fooId);
var bar = foo.MyProps["DynPointer"];
Assert.That(bar, Is.Not.Null);
Assert.That(bar, Is.InstanceOf<Bar>());
Assert.That(bar, Has.Property("Id").EqualTo(_barId));
}
}

[Test]
public void CanReadStaticPointerFromFoo()
{
using (var session = OpenSession())
using (session.BeginTransaction())
{
var foo = session.Get<Foo>(_fooId);
var bar = foo.StaticProps.StaticPointer;
Assert.That(bar, Is.Not.Null);
Assert.That(bar, Has.Property("Id").EqualTo(_barId));
}
}
}
}
19 changes: 19 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH1824/Foo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.Collections;
using System.Collections.Generic;

namespace NHibernate.Test.NHSpecificTest.GH1824
{
public class Foo
{
public virtual int Id { get; set; }

public virtual IDictionary MyProps { get; set; } = new Dictionary<string, object>();

public virtual StaticFooComponent StaticProps { get; set; }
}

public class StaticFooComponent
{
public Bar StaticPointer { get; set; }
}
}
35 changes: 35 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH1824/Mappings.hbm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="NHibernate.Test"
namespace="NHibernate.Test.NHSpecificTest.GH1824">

<class name="Foo">
<id type="int" name="Id">
<column name="Id"/>
<generator class="native"/>
</id>

<dynamic-component name="MyProps">
<many-to-one name="DynPointer" class="Bar" property-ref="MyProps.DynValString2"/>
</dynamic-component>

<component name="StaticProps">
<many-to-one name="StaticPointer" class="Bar" property-ref="StaticProps.StaticValString2"/>
</component>
</class>

<class name="Bar">
<id type="int" name="Id">
<column name="Id"/>
<generator class="native"/>
</id>
<dynamic-component name="MyProps">
<property name="DynValString2" type="string" unique="1"/>
<property name="AnotherProperty" type="string" />
</dynamic-component>
<component name="StaticProps">
<property name="StaticValString2" type="string" unique="1"/>
<property name="YetAnotherProperty" type="string" />
</component>
</class>
</hibernate-mapping>
2 changes: 1 addition & 1 deletion src/NHibernate/Cfg/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ public string GetIdentifierPropertyName(string className)
public IType GetReferencedPropertyType(string className, string propertyName)
{
PersistentClass pc = GetPersistentClass(className);
Property prop = pc.GetProperty(propertyName);
Property prop = pc.GetReferencedProperty(propertyName);
Copy link
Member Author

Choose a reason for hiding this comment

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

GetProperty yields properties of the entity, it does not go inside components for yielding the component property.


if (prop == null)
{
Expand Down