Skip to content

Fix fetching lazy component with formula #3171

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
merged 5 commits into from
Sep 30, 2022
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
4 changes: 2 additions & 2 deletions build-common/NHibernate.props
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

<PropertyGroup>
<NhVersion Condition="'$(NhVersion)' == ''" >5.3</NhVersion>
<VersionPatch Condition="'$(VersionPatch)' == ''">13</VersionPatch>
<VersionPatch Condition="'$(VersionPatch)' == ''">14</VersionPatch>
<!-- Clear VersionSuffix for making release and set it to dev for making development builds -->
<VersionSuffix Condition="'$(VersionSuffix)' == ''"></VersionSuffix>
<VersionSuffix Condition="'$(VersionSuffix)' == ''">dev</VersionSuffix>

<VersionPrefix Condition="'$(VersionPrefix)' == ''">$(NhVersion).$(VersionPatch)</VersionPrefix>
<VersionSuffix Condition="'$(VersionSuffix)' != '' AND '$(BuildNumber)' != ''">$(VersionSuffix).$(BuildNumber)</VersionSuffix>
Expand Down
66 changes: 66 additions & 0 deletions src/NHibernate.Test/Async/NHSpecificTest/GH3164/FixtureByCode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//------------------------------------------------------------------------------
// <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.Mapping.ByCode;
using NUnit.Framework;
using NHibernate.Linq;

namespace NHibernate.Test.NHSpecificTest.GH3164
{
using System.Threading.Tasks;
[TestFixture]
public class ByCodeFixtureAsync : TestCaseMappingByCode
{
protected override HbmMapping GetMappings()
{
var mapper = new ModelMapper();
mapper.AddMapping<ContentItemMapping>();
mapper.AddMapping<HeadMapping>();

return mapper.CompileMappingForAllExplicitlyAddedEntities();
}

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

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 FetchComponentAsync()
{
using (var session = OpenSession())
{
var result = await (session.Query<ContentItem>().Fetch(i => i.Head).ToListAsync());

Assert.That(result.Count, Is.EqualTo(1));
}
}
}
}
9 changes: 9 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH3164/ContentItem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace NHibernate.Test.NHSpecificTest.GH3164
{
public class ContentItem
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual IHead Head { get; set; }
}
}
16 changes: 16 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH3164/ContentItemMapping.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using NHibernate.Mapping.ByCode;
using NHibernate.Mapping.ByCode.Conformist;

namespace NHibernate.Test.NHSpecificTest.GH3164
{
public class ContentItemMapping : ClassMapping<ContentItem>
{
public ContentItemMapping()
{
Table("ContentItems");
Id(x => x.Id, m => m.Generator(Generators.Identity));
Property(x => x.Name);
Component(x => x.Head, x => x.Lazy(true));
}
}
}
55 changes: 55 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH3164/FixtureByCode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System.Linq;
using NHibernate.Cfg.MappingSchema;
using NHibernate.Mapping.ByCode;
using NUnit.Framework;
using NHibernate.Linq;

namespace NHibernate.Test.NHSpecificTest.GH3164
{
[TestFixture]
public class ByCodeFixture : TestCaseMappingByCode
{
protected override HbmMapping GetMappings()
{
var mapper = new ModelMapper();
mapper.AddMapping<ContentItemMapping>();
mapper.AddMapping<HeadMapping>();

return mapper.CompileMappingForAllExplicitlyAddedEntities();
}

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

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 FetchComponent()
{
using (var session = OpenSession())
{
var result = session.Query<ContentItem>().Fetch(i => i.Head).ToList();

Assert.That(result.Count, Is.EqualTo(1));
}
}
}
}
12 changes: 12 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH3164/Head.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;

namespace NHibernate.Test.NHSpecificTest.GH3164
{
[Serializable]
public class Head : IHead
{
public virtual string Title { get; set; }

public virtual bool DummyFieldToLoadEmptyComponent { get; }
}
}
24 changes: 24 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH3164/HeadMapping.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using NHibernate.Mapping.ByCode;
using NHibernate.Mapping.ByCode.Conformist;

namespace NHibernate.Test.NHSpecificTest.GH3164
{
public class HeadMapping : ComponentMapping<IHead>
{
public HeadMapping()
{
Class<Head>();
Property(x => x.Title, m =>
{
m.Column("PageTitle");
m.Lazy(true);
});
Property(x => x.DummyFieldToLoadEmptyComponent, m =>
{
m.Access(Accessor.ReadOnly);
m.Formula("1");
});
Lazy(true);
}
}
}
10 changes: 10 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH3164/IHead.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace NHibernate.Test.NHSpecificTest.GH3164
{

public interface IHead
{
string Title { get; set; }

bool DummyFieldToLoadEmptyComponent { get; }
}
}
9 changes: 3 additions & 6 deletions src/NHibernate/Persister/Entity/AbstractEntityPersister.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1673,14 +1673,11 @@ private string PropertySelectFragment(string name, string suffix, ICollection<st
}

var columnNames = SubclassPropertyColumnNameClosure[index];
// Formulas will have all null values
if (columnNames.All(o => o == null))
{
columnNames = SubclassPropertyFormulaTemplateClosure[index];
}
var formulas = SubclassPropertyFormulaTemplateClosure[index];

foreach (var columnName in columnNames)
for (var i = 0; i < columnNames.Length; i++)
{
var columnName = columnNames[i] ?? formulas[i];
fetchColumnsAndFormulas.Add(columnName);
}
}
Expand Down