Skip to content
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

Fix fetching lazy component on not mapped interface #3320

Merged
merged 11 commits into from
Jun 20, 2023
111 changes: 111 additions & 0 deletions src/NHibernate.Test/Async/NHSpecificTest/GH3289/FixtureByCode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
//------------------------------------------------------------------------------
// <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 System.Linq;
using NHibernate.Cfg.MappingSchema;
using NHibernate.Linq;
using NHibernate.Mapping.ByCode;
using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.GH3289
{
using System.Threading.Tasks;
[TestFixture]
public class ByCodeFixtureAsync : TestCaseMappingByCode
{
protected override HbmMapping GetMappings()
{
var mapper = new ModelMapper();
mapper.Class<Entity>(rc =>
{
rc.Id(x => x.Id, m => m.Generator(Generators.Identity));
rc.Property(x => x.Name);
rc.Component(x => x.Component);
});
mapper.Class<OtherEntity>(rc =>
{
rc.Id(x => x.Id, m => m.Generator(Generators.Identity));
rc.Property(x => x.Name);
rc.Component(x => x.Component);
});
mapper.JoinedSubclass<SubEntity>(rc =>
{
rc.Key(k => k.Column("Id"));
rc.Property(x => x.SomeProperty);
});
mapper.Component<Component>(rc =>
{
rc.Property(x => x.Field);
rc.Lazy(true);
});

return mapper.CompileMappingForAllExplicitlyAddedEntities();
}

protected override void OnSetUp()
{
using var session = OpenSession();
using var transaction = session.BeginTransaction();
var e1 = new SubEntity { Name = "Jim" };
session.Save(e1);

transaction.Commit();
}

protected override void OnTearDown()
{
using var session = OpenSession();
using var transaction = session.BeginTransaction();

if (Dialect.SupportsTemporaryTables)
session.CreateQuery("delete from System.Object").ExecuteUpdate();
else
session.Delete("from System.Object");

transaction.Commit();
}

[Test]
public async Task TestSubEntityInterfaceWithFetchIsPropertyInitializedAsync()
{
using var session = OpenSession();
var data = await (session.Query<ISubEntity>()
.Fetch(e => e.Component)
.ToListAsync());
var result = NHibernateUtil.IsPropertyInitialized(data[0], "Component");

Assert.That(result, Is.True);
}

[Test]
public async Task TestEntityInterfaceWithFetchIsPropertyInitializedAsync()
{
using var session = OpenSession();
var data = await (session.Query<IEntity>()
.Fetch(e => e.Component)
.ToListAsync());
var result = NHibernateUtil.IsPropertyInitialized(data[0], "Component");

Assert.That(result, Is.True);
}

[Test]
public async Task TestSubEntityWithFetchIsPropertyInitializedAsync()
{
using var session = OpenSession();
var data = await (session.Query<SubEntity>()
.Fetch(e => e.Component)
.ToListAsync());
var result = NHibernateUtil.IsPropertyInitialized(data[0], "Component");

Assert.That(result, Is.True);
}
}
}
100 changes: 100 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH3289/FixtureByCode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
using System.Linq;
using NHibernate.Cfg.MappingSchema;
using NHibernate.Linq;
using NHibernate.Mapping.ByCode;
using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.GH3289
{
[TestFixture]
public class ByCodeFixture : TestCaseMappingByCode
{
protected override HbmMapping GetMappings()
{
var mapper = new ModelMapper();
mapper.Class<Entity>(rc =>
{
rc.Id(x => x.Id, m => m.Generator(Generators.Identity));
rc.Property(x => x.Name);
rc.Component(x => x.Component);
});
mapper.Class<OtherEntity>(rc =>
{
rc.Id(x => x.Id, m => m.Generator(Generators.Identity));
rc.Property(x => x.Name);
rc.Component(x => x.Component);
});
mapper.JoinedSubclass<SubEntity>(rc =>
{
rc.Key(k => k.Column("Id"));
rc.Property(x => x.SomeProperty);
});
mapper.Component<Component>(rc =>
{
rc.Property(x => x.Field);
rc.Lazy(true);
});

return mapper.CompileMappingForAllExplicitlyAddedEntities();
}

protected override void OnSetUp()
{
using var session = OpenSession();
using var transaction = session.BeginTransaction();
var e1 = new SubEntity { Name = "Jim" };
session.Save(e1);

transaction.Commit();
}

protected override void OnTearDown()
{
using var session = OpenSession();
using var transaction = session.BeginTransaction();

if (Dialect.SupportsTemporaryTables)
session.CreateQuery("delete from System.Object").ExecuteUpdate();
else
session.Delete("from System.Object");

transaction.Commit();
}

[Test]
public void TestSubEntityInterfaceWithFetchIsPropertyInitialized()
{
using var session = OpenSession();
var data = session.Query<ISubEntity>()
.Fetch(e => e.Component)
.ToList();
var result = NHibernateUtil.IsPropertyInitialized(data[0], "Component");

Assert.That(result, Is.True);
}

[Test]
public void TestEntityInterfaceWithFetchIsPropertyInitialized()
{
using var session = OpenSession();
var data = session.Query<IEntity>()
.Fetch(e => e.Component)
.ToList();
var result = NHibernateUtil.IsPropertyInitialized(data[0], "Component");

Assert.That(result, Is.True);
}

[Test]
public void TestSubEntityWithFetchIsPropertyInitialized()
{
using var session = OpenSession();
var data = session.Query<SubEntity>()
.Fetch(e => e.Component)
.ToList();
var result = NHibernateUtil.IsPropertyInitialized(data[0], "Component");

Assert.That(result, Is.True);
}
}
}
37 changes: 37 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH3289/Models.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
namespace NHibernate.Test.NHSpecificTest.GH3289
{
public interface IEntity
{
int Id { get; set; }
string Name { get; set; }
Component Component { get; set; }
}

public interface ISubEntity : IEntity
{
public bool SomeProperty { get; set; }
}

public class Entity : IEntity
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual Component Component { get; set; }
}
public class OtherEntity : IEntity
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual Component Component { get; set; }
}

public class SubEntity : Entity, ISubEntity
{
public virtual bool SomeProperty { get; set; }
}

public class Component
{
public virtual string Field { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ private void Process(
{
var metadata = queryModelVisitor.VisitorParameters.SessionFactory
.GetClassMetadata(resultOperator.RelationMember.ReflectedType);
if (metadata == null)
{
var entityName = queryModelVisitor.VisitorParameters.SessionFactory.GetImplementors(
resultOperator.RelationMember.ReflectedType.FullName).FirstOrDefault();
if (!string.IsNullOrEmpty(entityName))
{
metadata = queryModelVisitor.VisitorParameters.SessionFactory.GetClassMetadata(entityName);
}
}

propType = metadata?.GetPropertyType(resultOperator.RelationMember.Name);
}

Expand Down