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 properties from unmapped base class #3357

Merged
merged 5 commits into from
Jul 13, 2023
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
//------------------------------------------------------------------------------
// <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.GH3352
{
using System.Threading.Tasks;
[TestFixture]
public class FetchFromNotMappedBaseClassFixtureAsync : TestCaseMappingByCode
{
protected override HbmMapping GetMappings()
{
var mapper = new ModelMapper();
mapper.Class<EntityNameMapped>(rc =>
{
rc.Id(x => x.Id, m => m.Generator(Generators.GuidComb));
rc.Property(x => x.Name, m => m.Lazy(true));
});
mapper.Class<EntityParentMapped>(rc =>
{
rc.Id(x => x.Id, m => m.Generator(Generators.GuidComb));
rc.ManyToOne(x => x.Parent, m => m.ForeignKey("none"));
});
mapper.Class<EntityComponentMapped>(rc =>
{
rc.Id(x => x.Id, m => m.Generator(Generators.GuidComb));
rc.Component(x => x.Component);
});
mapper.Component<Component>(rc =>
{
rc.Property(x => x.Field);
rc.ManyToOne(x => x.Entity, m => m.ForeignKey("none"));
rc.Lazy(true);
});
return mapper.CompileMappingForAllExplicitlyAddedEntities();
}

protected override void OnSetUp()
{
using var session = OpenSession();
using var transaction = session.BeginTransaction();
var np = new EntityComponentMapped { Component = new Component { Field = "x" } };
session.Save(np);
var e = new EntityParentMapped { Parent = np };
session.Save(e);
var nameMapped = new EntityNameMapped { Name = "lazy" };
session.Save(nameMapped);
np.Component.Entity = nameMapped;

transaction.Commit();
}

protected override void OnTearDown()
{
using var session = OpenSession();
using var transaction = session.BeginTransaction();
session.CreateQuery("delete from System.Object").ExecuteUpdate();

transaction.Commit();
}

[Test]
public async Task CanFetchLazyComponentFromNotMappedBaseClassAsync()
{
using var session = OpenSession();
var list = await (session.Query<EntityComponentMapped>().Fetch(x => x.Component).ToListAsync());

Assert.That(list, Has.Count.EqualTo(1));
var result = list[0];
Assert.That(NHibernateUtil.IsPropertyInitialized(result, nameof(result.Component)));
Assert.That(result.Component.Field, Is.EqualTo("x"));
}

[Test]
public async Task CanFetchLazyComponentThenEntityFromNotMappedBaseClassAsync()
{
using var session = OpenSession();
var list = await (session.Query<EntityComponentMapped>()
.Fetch(x => x.Component)
.ThenFetch(x => x.Entity)
.ThenFetch(x => x.Name)
.ToListAsync());

Assert.That(list, Has.Count.EqualTo(1));
var result = list[0];
Assert.That(NHibernateUtil.IsPropertyInitialized(result, nameof(result.Component)));
Assert.That(result.Component.Field, Is.EqualTo("x"));
Assert.That(result.Component.Entity, Is.Not.Null);
Assert.That(NHibernateUtil.IsInitialized(result.Component.Entity), Is.True);
Assert.That(NHibernateUtil.IsPropertyInitialized(result.Component.Entity, nameof(result.Name)), Is.True);
Assert.That(result.Component.Entity.Name, Is.EqualTo("lazy"));
}

[Test]
public async Task CanFetchLazyPropertyFromNotMappedBaseClassAsync()
{
using var session = OpenSession();
var list = await (session.Query<EntityNameMapped>().Fetch(x => x.Name).ToListAsync());

Assert.That(list, Has.Count.EqualTo(1));
var result = list[0];
Assert.That(NHibernateUtil.IsPropertyInitialized(result, nameof(result.Name)));
Assert.That(result.Name, Is.EqualTo("lazy"));
}

[Test]
public async Task CanThenFetchLazyComponentFromNotMappedBaseClassAsync()
{
using var session = OpenSession();
var list = await (session.Query<EntityParentMapped>().Fetch(x => x.Parent).ThenFetch(x => x.Component).ToListAsync());

Assert.That(list, Has.Count.EqualTo(1));
var result = list[0].Parent;
Assert.That(NHibernateUtil.IsInitialized(result), Is.True);
Assert.That(NHibernateUtil.IsPropertyInitialized(result, nameof(result.Component)));
Assert.That(result.Component.Field, Is.EqualTo("x"));
}

[KnownBug("GH-3356")]
[Test(Description = "GH-3356" )]
public async Task FetchAfterSelectAsync()
{
using var log = new SqlLogSpy();

using var s = OpenSession();
var list = await (s.Query<EntityParentMapped>()
.Select(x => x.Parent)
.Fetch(x => x.Component)
.ThenFetch(x => x.Entity)
.ThenFetch(x => x.Name)
.ToListAsync());
Assert.That(list, Has.Count.EqualTo(1));
var result = list[0];
Assert.That(NHibernateUtil.IsPropertyInitialized(result, nameof(result.Component)));
Assert.That(result.Component.Field, Is.EqualTo("x"));
Assert.That(result.Component.Entity, Is.Not.Null);
Assert.That(NHibernateUtil.IsInitialized(result.Component.Entity), Is.True);
Assert.That(NHibernateUtil.IsPropertyInitialized(result.Component.Entity, nameof(result.Name)), Is.True);
Assert.That(result.Component.Entity.Name, Is.EqualTo("lazy"));
}

[Test]
public async Task CanFetchEntityFromNotMappedBaseClassAsync()
{
using var session = OpenSession();
var list = await (session.Query<EntityParentMapped>().Fetch(x => x.Parent).ToListAsync());

Assert.That(list, Has.Count.EqualTo(1));
Assert.That(list[0].Parent, Is.Not.Null);
Assert.That(NHibernateUtil.IsInitialized(list[0].Parent));
}

[Test]
public void FetchNotMappedAssociationThrowsAsync()
{
using var session = OpenSession();
var query = session.Query<EntityNameMapped>().Fetch(x => x.Parent);

Assert.ThrowsAsync<QueryException>(() => query.ToListAsync());
}
}
}
31 changes: 31 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH3352/Entity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;

namespace NHibernate.Test.NHSpecificTest.GH3352
{
public class Entity
{
public virtual Guid Id { get; set; }
public virtual string Name { get; set; }
public virtual EntityComponentMapped Parent { get; set; }
public virtual Component Component { get; set; }
}

public class EntityNameMapped : Entity
{
}

public class EntityParentMapped : Entity
{
}

public class EntityComponentMapped : Entity
{
}

public class Component
{
public string Field { get; set; }

public EntityNameMapped Entity { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
using System.Linq;
using NHibernate.Cfg.MappingSchema;
using NHibernate.Linq;
using NHibernate.Mapping.ByCode;
using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.GH3352
{
[TestFixture]
public class FetchFromNotMappedBaseClassFixture : TestCaseMappingByCode
{
protected override HbmMapping GetMappings()
{
var mapper = new ModelMapper();
mapper.Class<EntityNameMapped>(rc =>
{
rc.Id(x => x.Id, m => m.Generator(Generators.GuidComb));
rc.Property(x => x.Name, m => m.Lazy(true));
});
mapper.Class<EntityParentMapped>(rc =>
{
rc.Id(x => x.Id, m => m.Generator(Generators.GuidComb));
rc.ManyToOne(x => x.Parent, m => m.ForeignKey("none"));
});
mapper.Class<EntityComponentMapped>(rc =>
{
rc.Id(x => x.Id, m => m.Generator(Generators.GuidComb));
rc.Component(x => x.Component);
});
mapper.Component<Component>(rc =>
{
rc.Property(x => x.Field);
rc.ManyToOne(x => x.Entity, m => m.ForeignKey("none"));
rc.Lazy(true);
});
return mapper.CompileMappingForAllExplicitlyAddedEntities();
}

protected override void OnSetUp()
{
using var session = OpenSession();
using var transaction = session.BeginTransaction();
var np = new EntityComponentMapped { Component = new Component { Field = "x" } };
session.Save(np);
var e = new EntityParentMapped { Parent = np };
session.Save(e);
var nameMapped = new EntityNameMapped { Name = "lazy" };
session.Save(nameMapped);
np.Component.Entity = nameMapped;

transaction.Commit();
}

protected override void OnTearDown()
{
using var session = OpenSession();
using var transaction = session.BeginTransaction();
session.CreateQuery("delete from System.Object").ExecuteUpdate();

transaction.Commit();
}

[Test]
public void CanFetchLazyComponentFromNotMappedBaseClass()
{
using var session = OpenSession();
var list = session.Query<EntityComponentMapped>().Fetch(x => x.Component).ToList();

Assert.That(list, Has.Count.EqualTo(1));
var result = list[0];
Assert.That(NHibernateUtil.IsPropertyInitialized(result, nameof(result.Component)));
Assert.That(result.Component.Field, Is.EqualTo("x"));
}

[Test]
public void CanFetchLazyComponentThenEntityFromNotMappedBaseClass()
{
using var session = OpenSession();
var list = session.Query<EntityComponentMapped>()
.Fetch(x => x.Component)
.ThenFetch(x => x.Entity)
.ThenFetch(x => x.Name)
.ToList();

Assert.That(list, Has.Count.EqualTo(1));
var result = list[0];
Assert.That(NHibernateUtil.IsPropertyInitialized(result, nameof(result.Component)));
Assert.That(result.Component.Field, Is.EqualTo("x"));
Assert.That(result.Component.Entity, Is.Not.Null);
Assert.That(NHibernateUtil.IsInitialized(result.Component.Entity), Is.True);
Assert.That(NHibernateUtil.IsPropertyInitialized(result.Component.Entity, nameof(result.Name)), Is.True);
Assert.That(result.Component.Entity.Name, Is.EqualTo("lazy"));
}

[Test]
public void CanFetchLazyPropertyFromNotMappedBaseClass()
{
using var session = OpenSession();
var list = session.Query<EntityNameMapped>().Fetch(x => x.Name).ToList();

Assert.That(list, Has.Count.EqualTo(1));
var result = list[0];
Assert.That(NHibernateUtil.IsPropertyInitialized(result, nameof(result.Name)));
Assert.That(result.Name, Is.EqualTo("lazy"));
}

[Test]
public void CanThenFetchLazyComponentFromNotMappedBaseClass()
{
using var session = OpenSession();
var list = session.Query<EntityParentMapped>().Fetch(x => x.Parent).ThenFetch(x => x.Component).ToList();

Assert.That(list, Has.Count.EqualTo(1));
var result = list[0].Parent;
Assert.That(NHibernateUtil.IsInitialized(result), Is.True);
Assert.That(NHibernateUtil.IsPropertyInitialized(result, nameof(result.Component)));
Assert.That(result.Component.Field, Is.EqualTo("x"));
}

[KnownBug("GH-3356")]
[Test(Description = "GH-3356" )]
public void FetchAfterSelect()
{
using var log = new SqlLogSpy();

using var s = OpenSession();
var list = s.Query<EntityParentMapped>()
.Select(x => x.Parent)
.Fetch(x => x.Component)
.ThenFetch(x => x.Entity)
.ThenFetch(x => x.Name)
.ToList();
Assert.That(list, Has.Count.EqualTo(1));
var result = list[0];
Assert.That(NHibernateUtil.IsPropertyInitialized(result, nameof(result.Component)));
Assert.That(result.Component.Field, Is.EqualTo("x"));
Assert.That(result.Component.Entity, Is.Not.Null);
Assert.That(NHibernateUtil.IsInitialized(result.Component.Entity), Is.True);
Assert.That(NHibernateUtil.IsPropertyInitialized(result.Component.Entity, nameof(result.Name)), Is.True);
Assert.That(result.Component.Entity.Name, Is.EqualTo("lazy"));
}

[Test]
public void CanFetchEntityFromNotMappedBaseClass()
{
using var session = OpenSession();
var list = session.Query<EntityParentMapped>().Fetch(x => x.Parent).ToList();

Assert.That(list, Has.Count.EqualTo(1));
Assert.That(list[0].Parent, Is.Not.Null);
Assert.That(NHibernateUtil.IsInitialized(list[0].Parent));
}

[Test]
public void FetchNotMappedAssociationThrows()
{
using var session = OpenSession();
var query = session.Query<EntityNameMapped>().Fetch(x => x.Parent);

Assert.Throws<QueryException>(() => query.ToList());
}
}
}
Loading