Skip to content

Commit ba574e9

Browse files
authored
Fix fetching lazy component with formula (#3171)
Fixes #3164
1 parent 7cc1307 commit ba574e9

File tree

9 files changed

+197
-8
lines changed

9 files changed

+197
-8
lines changed

build-common/NHibernate.props

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33

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

1010
<VersionPrefix Condition="'$(VersionPrefix)' == ''">$(NhVersion).$(VersionPatch)</VersionPrefix>
1111
<VersionSuffix Condition="'$(VersionSuffix)' != '' AND '$(BuildNumber)' != ''">$(VersionSuffix).$(BuildNumber)</VersionSuffix>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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 System.Linq;
12+
using NHibernate.Cfg.MappingSchema;
13+
using NHibernate.Mapping.ByCode;
14+
using NUnit.Framework;
15+
using NHibernate.Linq;
16+
17+
namespace NHibernate.Test.NHSpecificTest.GH3164
18+
{
19+
using System.Threading.Tasks;
20+
[TestFixture]
21+
public class ByCodeFixtureAsync : TestCaseMappingByCode
22+
{
23+
protected override HbmMapping GetMappings()
24+
{
25+
var mapper = new ModelMapper();
26+
mapper.AddMapping<ContentItemMapping>();
27+
mapper.AddMapping<HeadMapping>();
28+
29+
return mapper.CompileMappingForAllExplicitlyAddedEntities();
30+
}
31+
32+
protected override void OnSetUp()
33+
{
34+
using (var session = OpenSession())
35+
using (var transaction = session.BeginTransaction())
36+
{
37+
var e1 = new ContentItem { Name = "Test" };
38+
session.Save(e1);
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+
session.CreateQuery("delete from System.Object").ExecuteUpdate();
50+
51+
transaction.Commit();
52+
}
53+
}
54+
55+
[Test]
56+
public async Task FetchComponentAsync()
57+
{
58+
using (var session = OpenSession())
59+
{
60+
var result = await (session.Query<ContentItem>().Fetch(i => i.Head).ToListAsync());
61+
62+
Assert.That(result.Count, Is.EqualTo(1));
63+
}
64+
}
65+
}
66+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace NHibernate.Test.NHSpecificTest.GH3164
2+
{
3+
public class ContentItem
4+
{
5+
public virtual int Id { get; set; }
6+
public virtual string Name { get; set; }
7+
public virtual IHead Head { get; set; }
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using NHibernate.Mapping.ByCode;
2+
using NHibernate.Mapping.ByCode.Conformist;
3+
4+
namespace NHibernate.Test.NHSpecificTest.GH3164
5+
{
6+
public class ContentItemMapping : ClassMapping<ContentItem>
7+
{
8+
public ContentItemMapping()
9+
{
10+
Table("ContentItems");
11+
Id(x => x.Id, m => m.Generator(Generators.Identity));
12+
Property(x => x.Name);
13+
Component(x => x.Head, x => x.Lazy(true));
14+
}
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System.Linq;
2+
using NHibernate.Cfg.MappingSchema;
3+
using NHibernate.Mapping.ByCode;
4+
using NUnit.Framework;
5+
using NHibernate.Linq;
6+
7+
namespace NHibernate.Test.NHSpecificTest.GH3164
8+
{
9+
[TestFixture]
10+
public class ByCodeFixture : TestCaseMappingByCode
11+
{
12+
protected override HbmMapping GetMappings()
13+
{
14+
var mapper = new ModelMapper();
15+
mapper.AddMapping<ContentItemMapping>();
16+
mapper.AddMapping<HeadMapping>();
17+
18+
return mapper.CompileMappingForAllExplicitlyAddedEntities();
19+
}
20+
21+
protected override void OnSetUp()
22+
{
23+
using (var session = OpenSession())
24+
using (var transaction = session.BeginTransaction())
25+
{
26+
var e1 = new ContentItem { Name = "Test" };
27+
session.Save(e1);
28+
29+
transaction.Commit();
30+
}
31+
}
32+
33+
protected override void OnTearDown()
34+
{
35+
using (var session = OpenSession())
36+
using (var transaction = session.BeginTransaction())
37+
{
38+
session.CreateQuery("delete from System.Object").ExecuteUpdate();
39+
40+
transaction.Commit();
41+
}
42+
}
43+
44+
[Test]
45+
public void FetchComponent()
46+
{
47+
using (var session = OpenSession())
48+
{
49+
var result = session.Query<ContentItem>().Fetch(i => i.Head).ToList();
50+
51+
Assert.That(result.Count, Is.EqualTo(1));
52+
}
53+
}
54+
}
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
3+
namespace NHibernate.Test.NHSpecificTest.GH3164
4+
{
5+
[Serializable]
6+
public class Head : IHead
7+
{
8+
public virtual string Title { get; set; }
9+
10+
public virtual bool DummyFieldToLoadEmptyComponent { get; }
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using NHibernate.Mapping.ByCode;
2+
using NHibernate.Mapping.ByCode.Conformist;
3+
4+
namespace NHibernate.Test.NHSpecificTest.GH3164
5+
{
6+
public class HeadMapping : ComponentMapping<IHead>
7+
{
8+
public HeadMapping()
9+
{
10+
Class<Head>();
11+
Property(x => x.Title, m =>
12+
{
13+
m.Column("PageTitle");
14+
m.Lazy(true);
15+
});
16+
Property(x => x.DummyFieldToLoadEmptyComponent, m =>
17+
{
18+
m.Access(Accessor.ReadOnly);
19+
m.Formula("1");
20+
});
21+
Lazy(true);
22+
}
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace NHibernate.Test.NHSpecificTest.GH3164
2+
{
3+
4+
public interface IHead
5+
{
6+
string Title { get; set; }
7+
8+
bool DummyFieldToLoadEmptyComponent { get; }
9+
}
10+
}

src/NHibernate/Persister/Entity/AbstractEntityPersister.cs

+3-6
Original file line numberDiff line numberDiff line change
@@ -1673,14 +1673,11 @@ private string PropertySelectFragment(string name, string suffix, ICollection<st
16731673
}
16741674

16751675
var columnNames = SubclassPropertyColumnNameClosure[index];
1676-
// Formulas will have all null values
1677-
if (columnNames.All(o => o == null))
1678-
{
1679-
columnNames = SubclassPropertyFormulaTemplateClosure[index];
1680-
}
1676+
var formulas = SubclassPropertyFormulaTemplateClosure[index];
16811677

1682-
foreach (var columnName in columnNames)
1678+
for (var i = 0; i < columnNames.Length; i++)
16831679
{
1680+
var columnName = columnNames[i] ?? formulas[i];
16841681
fetchColumnsAndFormulas.Add(columnName);
16851682
}
16861683
}

0 commit comments

Comments
 (0)