Skip to content

Commit 993da7c

Browse files
committed
NH-2923 - Fix exception when get count of extra lazy indexed collections
1 parent ad35d6b commit 993da7c

File tree

4 files changed

+108
-3
lines changed

4 files changed

+108
-3
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
using System.Linq;
2+
using NHibernate.Cfg.MappingSchema;
3+
using NHibernate.Linq;
4+
using NHibernate.Mapping.ByCode;
5+
using NUnit.Framework;
6+
7+
namespace NHibernate.Test.NHSpecificTest.NH2923
8+
{
9+
public class ExtraLazyFixture : TestCaseMappingByCode
10+
{
11+
private object bobId;
12+
13+
protected override HbmMapping GetMappings()
14+
{
15+
var mapper = new ModelMapper();
16+
mapper.Class<Parent>(rc =>
17+
{
18+
rc.Id(x => x.Id, m => m.Generator(Generators.GuidComb));
19+
rc.Property(x => x.Name);
20+
rc.List(x => x.Children,
21+
m =>
22+
{
23+
m.Lazy(CollectionLazy.Extra);
24+
m.Cascade(Mapping.ByCode.Cascade.All | Mapping.ByCode.Cascade.DeleteOrphans);
25+
m.Inverse(true);
26+
},
27+
relation => relation.OneToMany());
28+
});
29+
mapper.Class<Child>(rc =>
30+
{
31+
rc.Id(x => x.Id, m => m.Generator(Generators.GuidComb));
32+
rc.Property(x => x.Name);
33+
rc.ManyToOne(x => x.Parent);
34+
});
35+
36+
return mapper.CompileMappingForAllExplicitlyAddedEntities();
37+
}
38+
39+
protected override void OnSetUp()
40+
{
41+
using (var session = OpenSession())
42+
using (var transaction = session.BeginTransaction())
43+
{
44+
var e1 = new Parent { Name = "Bob" };
45+
bobId = session.Save(e1);
46+
47+
var e2 = new Parent { Name = "Sally" };
48+
session.Save(e2);
49+
50+
session.Flush();
51+
transaction.Commit();
52+
}
53+
}
54+
55+
protected override void OnTearDown()
56+
{
57+
using (var session = OpenSession())
58+
using (var transaction = session.BeginTransaction())
59+
{
60+
session.Delete("from System.Object");
61+
62+
session.Flush();
63+
transaction.Commit();
64+
}
65+
}
66+
67+
[Test]
68+
public void ShouldNotThrowException()
69+
{
70+
using (var session = OpenSession())
71+
using (session.BeginTransaction())
72+
{
73+
var bob = session.Get<Parent>(bobId);
74+
75+
int? count = null;
76+
Assert.DoesNotThrow(() =>
77+
{
78+
count = bob.Children.Count;
79+
});
80+
Assert.NotNull(count);
81+
Assert.That(count.Value, Is.EqualTo(0));
82+
}
83+
}
84+
}
85+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace NHibernate.Test.NHSpecificTest.NH2923
5+
{
6+
public class Parent
7+
{
8+
public virtual Guid Id { get; set; }
9+
public virtual string Name { get; set; }
10+
public virtual ICollection<Child> Children { get; set; }
11+
}
12+
13+
public class Child
14+
{
15+
public virtual Guid Id { get; set; }
16+
public virtual string Name { get; set; }
17+
public virtual Parent Parent { get; set; }
18+
}
19+
}

src/NHibernate.Test/NHibernate.Test.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,8 @@
670670
<Compile Include="NHSpecificTest\BagWithLazyExtraAndFilter\Domain.cs" />
671671
<Compile Include="NHSpecificTest\BagWithLazyExtraAndFilter\Fixture.cs" />
672672
<Compile Include="Component\Basic\ComponentWithUniqueConstraintTests.cs" />
673+
<Compile Include="NHSpecificTest\NH2923\Models.cs" />
674+
<Compile Include="NHSpecificTest\NH2923\ExtraLazyFixture.cs" />
673675
<Compile Include="NHSpecificTest\NH3405\Fixture.cs" />
674676
<Compile Include="NHSpecificTest\NH3428\Entity.cs" />
675677
<Compile Include="NHSpecificTest\NH3428\Fixture.cs" />

src/NHibernate/Persister/Collection/AbstractCollectionPersister.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -858,9 +858,8 @@ private SqlString GenerateSelectSizeString(ISessionImplementor sessionImplemento
858858
protected virtual string GetCountSqlSelectClause()
859859
{
860860
// NH: too many "if" when each collection can have its persister
861-
return isCollectionIntegerIndex
862-
? (string.Format("max({0}) + 1", IndexColumnNames[0]))
863-
: (HasIndex ? string.Format("count({0})", GetIndexCountExpression()) : string.Format("count({0})", ElementColumnNames[0]));
861+
if (isCollectionIntegerIndex) return string.Format("coalesce(max({0}) + 1, 0)", IndexColumnNames[0]); // Do we need this "optimization"?
862+
return string.Format("count({0})", HasIndex ? GetIndexCountExpression() : ElementColumnNames[0]);
864863
}
865864

866865
private string GetIndexCountExpression()

0 commit comments

Comments
 (0)