Skip to content

Commit b0a6016

Browse files
author
Matt
committed
add unit test and fix for NH-3614
when getting the identifier in the selectrewiter it was returning a null constant when there was no class metadata, as it the case for type string
1 parent 49c6b89 commit b0a6016

File tree

5 files changed

+93
-2
lines changed

5 files changed

+93
-2
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.NH3614
7+
{
8+
public class Entity
9+
{
10+
public virtual int Id { get; protected set; }
11+
public virtual IList<string> SomeStrings { get; set; }
12+
}
13+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using NUnit.Framework;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using NHibernate.Linq;
7+
8+
namespace NHibernate.Test.NHSpecificTest.NH3614
9+
{
10+
[TestFixture]
11+
public class Fixture : BugTestCase
12+
{
13+
[Test]
14+
public void CanProjectListOfStrings()
15+
{
16+
int id;
17+
using (var s = OpenSession())
18+
using (var tx = s.BeginTransaction())
19+
{
20+
var testEntity = new Entity
21+
{
22+
SomeStrings = new List<string> { "Hello", "World" }
23+
};
24+
s.Save(testEntity);
25+
26+
tx.Commit();
27+
28+
id = testEntity.Id;
29+
}
30+
31+
using (var s = OpenSession())
32+
{
33+
var result = s.Query<Entity>()
34+
.Where(x => x.Id == id)
35+
.Select(x => x.SomeStrings)
36+
.ToList();
37+
38+
Assert.AreEqual(1, result.Count);
39+
40+
Assert.AreEqual(2, result.Single().Count);
41+
}
42+
43+
using (var s = OpenSession())
44+
using (var tx = s.BeginTransaction())
45+
{
46+
s.Delete("from Entity");
47+
tx.Commit();
48+
}
49+
}
50+
}
51+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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.NH3614">
5+
6+
<class name="Entity">
7+
<id name="Id">
8+
<generator class="native" />
9+
</id>
10+
11+
<bag name="SomeStrings" table="SomeStrings" cascade="all-delete-orphan" >
12+
<key>
13+
<column name="ParentId" />
14+
</key>
15+
<element type="System.String, mscorlib">
16+
<column name="StringValue" />
17+
</element>
18+
</bag>
19+
</class>
20+
21+
</hibernate-mapping>

src/NHibernate.Test/NHibernate.Test.csproj

Lines changed: 4 additions & 1 deletion
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\NH3614\Entity.cs" />
674+
<Compile Include="NHSpecificTest\NH3614\Fixture.cs" />
673675
<Compile Include="NHSpecificTest\NH3505\Student.cs" />
674676
<Compile Include="NHSpecificTest\NH3505\Teacher.cs" />
675677
<Compile Include="NHSpecificTest\NH3505\Fixture.cs" />
@@ -2901,6 +2903,7 @@
29012903
<EmbeddedResource Include="NHSpecificTest\NH1291AnonExample\Mappings.hbm.xml" />
29022904
</ItemGroup>
29032905
<ItemGroup>
2906+
<EmbeddedResource Include="NHSpecificTest\NH3614\Mappings.hbm.xml" />
29042907
<EmbeddedResource Include="NHSpecificTest\NH3505\Mappings.hbm.xml" />
29052908
<EmbeddedResource Include="NHSpecificTest\NH3428\Mappings.hbm.xml" />
29062909
<EmbeddedResource Include="NHSpecificTest\NH3408\Mappings.hbm.xml" />
@@ -3438,4 +3441,4 @@ if exist hibernate.cfg.xml (del hibernate.cfg.xml)
34383441
if exist "$(ProjectDir)hibernate.cfg.xml" (copy "$(ProjectDir)hibernate.cfg.xml" "hibernate.cfg.xml")
34393442
copy /y "..\..\..\NHibernate.DomainModel\ABC.hbm.xml" "ABC.hbm.xml"</PostBuildEvent>
34403443
</PropertyGroup>
3441-
</Project>
3444+
</Project>

src/NHibernate/Linq/NestedSelects/NestedSelectRewriter.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,9 @@ private static LambdaExpression MakePredicate(int index)
217217

218218
private static Expression GetIdentifier(ISessionFactory sessionFactory, Expression expression)
219219
{
220+
if (expression.Type.IsPrimitive || expression.Type == typeof(string))
221+
return expression;
222+
220223
var classMetadata = sessionFactory.GetClassMetadata(expression.Type);
221224
if (classMetadata == null)
222225
return Expression.Constant(null);
@@ -257,4 +260,4 @@ private static System.Type GetElementType(System.Type type)
257260
return elementType;
258261
}
259262
}
260-
}
263+
}

0 commit comments

Comments
 (0)