Skip to content

Commit 091fa6b

Browse files
committed
Merge pull request #416 from janno-p/NH-3666
NH-3666 - ArgumentNullException when SetCacheable is used on custom query
2 parents bcc669e + d164bd6 commit 091fa6b

File tree

5 files changed

+126
-0
lines changed

5 files changed

+126
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace NHibernate.Test.NHSpecificTest.NH3666
7+
{
8+
public class Entity
9+
{
10+
public virtual int Id { get; set; }
11+
public virtual string Property { get; set; }
12+
}
13+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
using NUnit.Framework;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
7+
namespace NHibernate.Test.NHSpecificTest.NH3666
8+
{
9+
[TestFixture]
10+
public class Fixture : BugTestCase
11+
{
12+
protected override void OnSetUp()
13+
{
14+
using (var session = this.OpenSession())
15+
using (var transaction = session.BeginTransaction())
16+
{
17+
var entity1 = new Entity { Id = 1, Property = "Test1" };
18+
var entity2 = new Entity { Id = 2, Property = "Test2" };
19+
var entity3 = new Entity { Id = 3, Property = "Test3" };
20+
21+
session.Save(entity1);
22+
session.Save(entity2);
23+
session.Save(entity3);
24+
25+
transaction.Commit();
26+
}
27+
}
28+
29+
protected override void OnTearDown()
30+
{
31+
using (var session = this.OpenSession())
32+
using (var transaction = session.BeginTransaction())
33+
{
34+
session.Delete("from Entity");
35+
transaction.Commit();
36+
}
37+
}
38+
39+
[Test]
40+
public void CacheableDoesNotThrowExceptionWithNativeSQLQuery()
41+
{
42+
using (var session = this.OpenSession())
43+
using (var transaction = session.BeginTransaction())
44+
{
45+
var result = session.CreateSQLQuery("SELECT * FROM Entity WHERE Property = 'Test2'")
46+
.AddEntity(typeof(Entity))
47+
.SetCacheable(true)
48+
.List<Entity>();
49+
50+
CollectionAssert.IsNotEmpty(result);
51+
52+
Assert.AreEqual(1, result.Count);
53+
Assert.AreEqual(2, result[0].Id);
54+
}
55+
}
56+
57+
[Test]
58+
public void CacheableDoesNotThrowExceptionWithNamedQuery()
59+
{
60+
using (var session = this.OpenSession())
61+
using (var transaction = session.BeginTransaction())
62+
{
63+
var result = session.GetNamedQuery("QueryName")
64+
.SetCacheable(true)
65+
.SetString("prop", "Test2")
66+
.List<Entity>();
67+
68+
CollectionAssert.IsNotEmpty(result);
69+
70+
Assert.AreEqual(1, result.Count);
71+
Assert.AreEqual(2, result[0].Id);
72+
}
73+
}
74+
}
75+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
3+
assembly="NHibernate.Test"
4+
namespace="NHibernate.Test.NHSpecificTest.NH3666">
5+
6+
<class name="Entity" table="Entity">
7+
<id name="Id" access="property" column="Id" type="Int32" unsaved-value="0">
8+
<generator class="assigned" />
9+
</id>
10+
<property name="Property" type="String" />
11+
</class>
12+
13+
<sql-query name="QueryName">
14+
<return class="Entity" />
15+
SELECT * FROM Entity WHERE Property = :prop
16+
</sql-query>
17+
18+
</hibernate-mapping>

src/NHibernate.Test/NHibernate.Test.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,8 @@
732732
<Compile Include="NHSpecificTest\NH3570\BiFixture.cs" />
733733
<Compile Include="NHSpecificTest\NH3570\Model.cs" />
734734
<Compile Include="NHSpecificTest\NH3570\UniFixture.cs" />
735+
<Compile Include="NHSpecificTest\NH3666\Entity.cs" />
736+
<Compile Include="NHSpecificTest\NH3666\Fixture.cs" />
735737
<Compile Include="NHSpecificTest\NH3731\Entity.cs" />
736738
<Compile Include="NHSpecificTest\NH3731\FixtureByCode.cs" />
737739
<Compile Include="NHSpecificTest\NH2053\Cat.cs" />
@@ -3128,6 +3130,9 @@
31283130
<EmbeddedResource Include="NHSpecificTest\NH1291AnonExample\Mappings.hbm.xml" />
31293131
</ItemGroup>
31303132
<ItemGroup>
3133+
<EmbeddedResource Include="NHSpecificTest\NH3666\Mappings.hbm.xml">
3134+
<SubType>Designer</SubType>
3135+
</EmbeddedResource>
31313136
<EmbeddedResource Include="VersionTest\Db\MsSQL\ProductWithVersionAndLazyProperty.hbm.xml" />
31323137
<EmbeddedResource Include="NHSpecificTest\NH3754\Mappings.hbm.xml" />
31333138
<EmbeddedResource Include="LazyComponentTest\Person.hbm.xml" />

src/NHibernate/Loader/Custom/CustomLoader.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ public class CustomLoader : Loader
3838
private readonly LockMode[] lockModes;
3939
private readonly ResultRowProcessor rowProcessor;
4040

41+
private readonly bool[] includeInResultRow;
42+
4143
private IType[] resultTypes;
4244
private string[] transformerAliases;
4345

@@ -61,6 +63,8 @@ public CustomLoader(ICustomQuery customQuery, ISessionFactoryImplementor factory
6163
List<IType> resulttypes = new List<IType>();
6264
List<string> specifiedAliases = new List<string>();
6365

66+
List<bool> includeInResultRowList = new List<bool>();
67+
6468
int returnableCounter = 0;
6569
bool hasScalars = false;
6670

@@ -72,6 +76,7 @@ public CustomLoader(ICustomQuery customQuery, ISessionFactoryImplementor factory
7276
resulttypes.Add(scalarRtn.Type);
7377
specifiedAliases.Add(scalarRtn.ColumnAlias);
7478
resultColumnProcessors.Add(new ScalarResultColumnProcessor(scalarRtn.ColumnAlias, scalarRtn.Type));
79+
includeInResultRowList.Add(true);
7580
hasScalars = true;
7681
}
7782
else if (rtn is RootReturn)
@@ -87,6 +92,7 @@ public CustomLoader(ICustomQuery customQuery, ISessionFactoryImplementor factory
8792
specifiedAliases.Add(rootRtn.Alias);
8893
entityaliases.Add(rootRtn.EntityAliases);
8994
querySpaces.UnionWith(persister.QuerySpaces);
95+
includeInResultRowList.Add(true);
9096
}
9197
else if (rtn is CollectionReturn)
9298
{
@@ -111,6 +117,7 @@ public CustomLoader(ICustomQuery customQuery, ISessionFactoryImplementor factory
111117
entityaliases.Add(collRtn.ElementEntityAliases);
112118
querySpaces.UnionWith(elementPersister.QuerySpaces);
113119
}
120+
includeInResultRowList.Add(true);
114121
}
115122
else if (rtn is EntityFetchReturn)
116123
{
@@ -128,6 +135,7 @@ public CustomLoader(ICustomQuery customQuery, ISessionFactoryImplementor factory
128135
specifiedAliases.Add(fetchRtn.Alias);
129136
entityaliases.Add(fetchRtn.EntityAliases);
130137
querySpaces.UnionWith(persister.QuerySpaces);
138+
includeInResultRowList.Add(false);
131139
}
132140
else if (rtn is CollectionFetchReturn)
133141
{
@@ -153,6 +161,7 @@ public CustomLoader(ICustomQuery customQuery, ISessionFactoryImplementor factory
153161
entityaliases.Add(fetchRtn.ElementEntityAliases);
154162
querySpaces.UnionWith(elementPersister.QuerySpaces);
155163
}
164+
includeInResultRowList.Add(false);
156165
}
157166
else
158167
{
@@ -170,6 +179,7 @@ public CustomLoader(ICustomQuery customQuery, ISessionFactoryImplementor factory
170179
resultTypes = resulttypes.ToArray();
171180
transformerAliases = specifiedAliases.ToArray();
172181
rowProcessor = new ResultRowProcessor(hasScalars, resultColumnProcessors.ToArray());
182+
includeInResultRow = includeInResultRowList.ToArray();
173183
}
174184

175185
public ISet<string> QuerySpaces
@@ -289,6 +299,11 @@ protected override IResultTransformer ResolveResultTransformer(IResultTransforme
289299
return HolderInstantiator.ResolveResultTransformer(null, resultTransformer);
290300
}
291301

302+
protected override bool[] IncludeInResultRow
303+
{
304+
get { return includeInResultRow; }
305+
}
306+
292307
public override IList GetResultList(IList results, IResultTransformer resultTransformer)
293308
{
294309
// meant to handle dynamic instantiation queries...(Copy from QueryLoader)

0 commit comments

Comments
 (0)