Skip to content

NH-3666 - ArgumentNullException when SetCacheable is used on custom query #416

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 3 commits into from
Apr 15, 2015
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
13 changes: 13 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/NH3666/Entity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NHibernate.Test.NHSpecificTest.NH3666
{
public class Entity
{
public virtual int Id { get; set; }
public virtual string Property { get; set; }
}
}
75 changes: 75 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/NH3666/Fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NHibernate.Test.NHSpecificTest.NH3666
{
[TestFixture]
public class Fixture : BugTestCase
{
protected override void OnSetUp()
{
using (var session = this.OpenSession())
using (var transaction = session.BeginTransaction())
{
var entity1 = new Entity { Id = 1, Property = "Test1" };
var entity2 = new Entity { Id = 2, Property = "Test2" };
var entity3 = new Entity { Id = 3, Property = "Test3" };

session.Save(entity1);
session.Save(entity2);
session.Save(entity3);

transaction.Commit();
}
}

protected override void OnTearDown()
{
using (var session = this.OpenSession())
using (var transaction = session.BeginTransaction())
{
session.Delete("from Entity");
transaction.Commit();
}
}

[Test]
public void CacheableDoesNotThrowExceptionWithNativeSQLQuery()
{
using (var session = this.OpenSession())
using (var transaction = session.BeginTransaction())
{
var result = session.CreateSQLQuery("SELECT * FROM Entity WHERE Property = 'Test2'")
.AddEntity(typeof(Entity))
.SetCacheable(true)
.List<Entity>();

CollectionAssert.IsNotEmpty(result);

Assert.AreEqual(1, result.Count);
Assert.AreEqual(2, result[0].Id);
}
}

[Test]
public void CacheableDoesNotThrowExceptionWithNamedQuery()
{
using (var session = this.OpenSession())
using (var transaction = session.BeginTransaction())
{
var result = session.GetNamedQuery("QueryName")
.SetCacheable(true)
.SetString("prop", "Test2")
.List<Entity>();

CollectionAssert.IsNotEmpty(result);

Assert.AreEqual(1, result.Count);
Assert.AreEqual(2, result[0].Id);
}
}
}
}
18 changes: 18 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/NH3666/Mappings.hbm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="NHibernate.Test"
namespace="NHibernate.Test.NHSpecificTest.NH3666">

<class name="Entity" table="Entity">
<id name="Id" access="property" column="Id" type="Int32" unsaved-value="0">
<generator class="assigned" />
</id>
<property name="Property" type="String" />
</class>

<sql-query name="QueryName">
<return class="Entity" />
SELECT * FROM Entity WHERE Property = :prop
</sql-query>

</hibernate-mapping>
5 changes: 5 additions & 0 deletions src/NHibernate.Test/NHibernate.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,8 @@
<Compile Include="NHSpecificTest\NH3570\BiFixture.cs" />
<Compile Include="NHSpecificTest\NH3570\Model.cs" />
<Compile Include="NHSpecificTest\NH3570\UniFixture.cs" />
<Compile Include="NHSpecificTest\NH3666\Entity.cs" />
<Compile Include="NHSpecificTest\NH3666\Fixture.cs" />
<Compile Include="NHSpecificTest\NH3731\Entity.cs" />
<Compile Include="NHSpecificTest\NH3731\FixtureByCode.cs" />
<Compile Include="NHSpecificTest\NH2053\Cat.cs" />
Expand Down Expand Up @@ -3128,6 +3130,9 @@
<EmbeddedResource Include="NHSpecificTest\NH1291AnonExample\Mappings.hbm.xml" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="NHSpecificTest\NH3666\Mappings.hbm.xml">
<SubType>Designer</SubType>
</EmbeddedResource>
<EmbeddedResource Include="VersionTest\Db\MsSQL\ProductWithVersionAndLazyProperty.hbm.xml" />
<EmbeddedResource Include="NHSpecificTest\NH3754\Mappings.hbm.xml" />
<EmbeddedResource Include="LazyComponentTest\Person.hbm.xml" />
Expand Down
15 changes: 15 additions & 0 deletions src/NHibernate/Loader/Custom/CustomLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public class CustomLoader : Loader
private readonly LockMode[] lockModes;
private readonly ResultRowProcessor rowProcessor;

private readonly bool[] includeInResultRow;

private IType[] resultTypes;
private string[] transformerAliases;

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

List<bool> includeInResultRowList = new List<bool>();

int returnableCounter = 0;
bool hasScalars = false;

Expand All @@ -72,6 +76,7 @@ public CustomLoader(ICustomQuery customQuery, ISessionFactoryImplementor factory
resulttypes.Add(scalarRtn.Type);
specifiedAliases.Add(scalarRtn.ColumnAlias);
resultColumnProcessors.Add(new ScalarResultColumnProcessor(scalarRtn.ColumnAlias, scalarRtn.Type));
includeInResultRowList.Add(true);
hasScalars = true;
}
else if (rtn is RootReturn)
Expand All @@ -87,6 +92,7 @@ public CustomLoader(ICustomQuery customQuery, ISessionFactoryImplementor factory
specifiedAliases.Add(rootRtn.Alias);
entityaliases.Add(rootRtn.EntityAliases);
querySpaces.UnionWith(persister.QuerySpaces);
includeInResultRowList.Add(true);
}
else if (rtn is CollectionReturn)
{
Expand All @@ -111,6 +117,7 @@ public CustomLoader(ICustomQuery customQuery, ISessionFactoryImplementor factory
entityaliases.Add(collRtn.ElementEntityAliases);
querySpaces.UnionWith(elementPersister.QuerySpaces);
}
includeInResultRowList.Add(true);
}
else if (rtn is EntityFetchReturn)
{
Expand All @@ -128,6 +135,7 @@ public CustomLoader(ICustomQuery customQuery, ISessionFactoryImplementor factory
specifiedAliases.Add(fetchRtn.Alias);
entityaliases.Add(fetchRtn.EntityAliases);
querySpaces.UnionWith(persister.QuerySpaces);
includeInResultRowList.Add(false);
}
else if (rtn is CollectionFetchReturn)
{
Expand All @@ -153,6 +161,7 @@ public CustomLoader(ICustomQuery customQuery, ISessionFactoryImplementor factory
entityaliases.Add(fetchRtn.ElementEntityAliases);
querySpaces.UnionWith(elementPersister.QuerySpaces);
}
includeInResultRowList.Add(false);
}
else
{
Expand All @@ -170,6 +179,7 @@ public CustomLoader(ICustomQuery customQuery, ISessionFactoryImplementor factory
resultTypes = resulttypes.ToArray();
transformerAliases = specifiedAliases.ToArray();
rowProcessor = new ResultRowProcessor(hasScalars, resultColumnProcessors.ToArray());
includeInResultRow = includeInResultRowList.ToArray();
}

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

protected override bool[] IncludeInResultRow
{
get { return includeInResultRow; }
}

public override IList GetResultList(IList results, IResultTransformer resultTransformer)
{
// meant to handle dynamic instantiation queries...(Copy from QueryLoader)
Expand Down