Skip to content

Commit 4e529c0

Browse files
committed
Merge fix for NH-3455 (QueryOver + selecting a component + OrderBy = wrong OrderBy in SQL) plus NH-3644 (refactor of IProjection interface).
2 parents a26d295 + 1775cc2 commit 4e529c0

23 files changed

+234
-150
lines changed

releasenotes.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
Build 4.0.0.XXXX
2+
=============================
3+
4+
** Known BREAKING CHANGES from NH4.0.0.Alpha2 to 4.0.0.Alpha1
5+
6+
The interface IEnhancedProjection was removed and its methods moved to IProjection.
7+
Two other overloads of the GetColumnAliases() methods was removed from IProjection.
8+
19
Build 4.0.0.Alpha2
210
=============================
311

src/NHibernate.Test/Criteria/AddNumberProjection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public override SqlString ToSqlString(ICriteria criteria, int position, ICriteri
3636
.Add(" + ")
3737
.Add(criteriaQuery.NewQueryParameter(typedValue).Single())
3838
.Add(") as ")
39-
.Add(GetColumnAliases(0)[0])
39+
.Add(GetColumnAliases(0, criteria, criteriaQuery)[0])
4040
.ToSqlString();
4141
}
4242

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
3+
namespace NHibernate.Test.NHSpecificTest.NH3455
4+
{
5+
class Address
6+
{
7+
public virtual Guid Id { get; set; }
8+
public virtual string Street { get; set; }
9+
public virtual string City { get; set; }
10+
public virtual string Zip { get; set; }
11+
public virtual string State { get; set; }
12+
}
13+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
using NHibernate.Criterion;
2+
using NHibernate.Transform;
3+
using NUnit.Framework;
4+
5+
namespace NHibernate.Test.NHSpecificTest.NH3455
6+
{
7+
[TestFixture]
8+
public class Fixture : BugTestCase
9+
{
10+
protected override void OnSetUp()
11+
{
12+
using (var session = OpenSession())
13+
using (var transaction = session.BeginTransaction())
14+
{
15+
var e1 = new Person { Name = "Bob", Age = 31, Weight = 185, Address = new Address
16+
{
17+
City = "Abington",
18+
State = "VA",
19+
Street = "Avenue",
20+
Zip = "11121"
21+
}};
22+
session.Save(e1);
23+
24+
var e2 = new Person
25+
{
26+
Name = "Sally",
27+
Age = 22,
28+
Weight = 122,
29+
Address = new Address
30+
{
31+
City = "Olympia",
32+
State = "WA",
33+
Street = "Broad",
34+
Zip = "99989"
35+
}
36+
};
37+
session.Save(e2);
38+
39+
session.Flush();
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.Delete("from System.Object");
50+
51+
session.Flush();
52+
transaction.Commit();
53+
}
54+
}
55+
56+
[Test]
57+
public void OrderBySpecifiedPropertyWithQueryOver()
58+
{
59+
using (var session = OpenSession())
60+
using (session.BeginTransaction())
61+
{
62+
PersonDto dto = null;
63+
var people = session.QueryOver<Person>()
64+
.SelectList(b => b.Select(p => p.Id).WithAlias(() => dto.Id)
65+
.Select(p => p.Name).WithAlias(() => dto.Name)
66+
.Select(p => p.Address).WithAlias(() => dto.Address)
67+
.Select(p => p.Age).WithAlias(() => dto.Age))
68+
.OrderBy(p => p.Age)
69+
.Desc
70+
.TransformUsing(Transformers.AliasToBean<PersonDto>())
71+
.List<PersonDto>();
72+
73+
Assert.That(people.Count, Is.EqualTo(2));
74+
Assert.That(people, Is.Ordered.By("Age").Descending);
75+
}
76+
}
77+
78+
[Test]
79+
public void OrderBySpecifiedPropertyWithCriteria()
80+
{
81+
using (var session = OpenSession())
82+
using (session.BeginTransaction())
83+
{
84+
var selectList = Projections.ProjectionList()
85+
.Add(Projections.Property("Id"), "Id")
86+
.Add(Projections.Property("Name"), "Name")
87+
.Add(Projections.Property("Address"), "Address")
88+
.Add(Projections.Property("Age"), "Age");
89+
var order = new Order("Age", false);
90+
var people = session.CreateCriteria<Person>()
91+
.SetProjection(selectList)
92+
.AddOrder(order)
93+
.SetResultTransformer(Transformers.AliasToBean<PersonDto>())
94+
.List<PersonDto>();
95+
96+
Assert.That(people.Count, Is.EqualTo(2));
97+
Assert.That(people, Is.Ordered.By("Age").Descending);
98+
}
99+
}
100+
}
101+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernate.Test" namespace="NHibernate.Test.NHSpecificTest.NH3455">
3+
4+
<class name="Person">
5+
<id name="Id" generator="guid.comb" />
6+
<property name="Name" />
7+
<property name="Age" />
8+
<property name="Weight" />
9+
<component name="Address">
10+
<property name="Street" />
11+
<property name="City" />
12+
<property name="Zip" />
13+
<property name="State" />
14+
</component>
15+
</class>
16+
17+
</hibernate-mapping>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
3+
namespace NHibernate.Test.NHSpecificTest.NH3455
4+
{
5+
class Person
6+
{
7+
public virtual Guid Id { get; set; }
8+
public virtual string Name { get; set; }
9+
public virtual int Age { get; set; }
10+
public virtual int Weight { get; set; }
11+
public virtual Address Address { get; set; }
12+
}
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
3+
namespace NHibernate.Test.NHSpecificTest.NH3455
4+
{
5+
class PersonDto
6+
{
7+
public virtual Guid Id { get; set; }
8+
public virtual string Name { get; set; }
9+
public virtual int Age { get; set; }
10+
public virtual int Weight { get; set; }
11+
public virtual Address Address { get; set; }
12+
}
13+
}

src/NHibernate.Test/NHibernate.Test.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,10 @@
689689
<Compile Include="Component\Basic\ComponentWithUniqueConstraintTests.cs" />
690690
<Compile Include="NHSpecificTest\NH3620\Fixture.cs" />
691691
<Compile Include="NHSpecificTest\NH3620\TwoBlobs.cs" />
692+
<Compile Include="NHSpecificTest\NH3455\Address.cs" />
693+
<Compile Include="NHSpecificTest\NH3455\PersonDto.cs" />
694+
<Compile Include="NHSpecificTest\NH3455\Person.cs" />
695+
<Compile Include="NHSpecificTest\NH3455\Fixture.cs" />
692696
<Compile Include="NHSpecificTest\NH1082\SynchronizationThatThrowsExceptionAtBeforeTransactionCompletion.cs" />
693697
<Compile Include="NHSpecificTest\NH2756\Fixture.cs" />
694698
<Compile Include="NHSpecificTest\NH2756\Model.cs" />
@@ -3041,6 +3045,7 @@
30413045
<EmbeddedResource Include="NHSpecificTest\NH1291AnonExample\Mappings.hbm.xml" />
30423046
</ItemGroup>
30433047
<ItemGroup>
3048+
<EmbeddedResource Include="NHSpecificTest\NH3455\Mappings.hbm.xml" />
30443049
<EmbeddedResource Include="NHSpecificTest\NH3590\Mappings.hbm.xml" />
30453050
<EmbeddedResource Include="NHSpecificTest\NH3377\Mappings.hbm.xml" />
30463051
<EmbeddedResource Include="NHSpecificTest\NH2865\Mappings.hbm.xml" />

src/NHibernate/Criterion/AliasedProjection.cs

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
namespace NHibernate.Criterion
99
{
1010
[Serializable]
11-
public class AliasedProjection : IEnhancedProjection
11+
public class AliasedProjection : IProjection
1212
{
1313
private readonly IProjection projection;
1414
private readonly string alias;
@@ -41,23 +41,9 @@ public virtual IType[] GetTypes(String alias, ICriteria criteria, ICriteriaQuery
4141
: null;
4242
}
4343

44-
public virtual string[] GetColumnAliases(int loc)
45-
{
46-
return projection.GetColumnAliases(loc);
47-
}
48-
49-
public virtual string[] GetColumnAliases(string alias, int loc)
50-
{
51-
return this.alias.Equals(alias)
52-
? GetColumnAliases(loc)
53-
: null;
54-
}
55-
5644
public string[] GetColumnAliases(int position, ICriteria criteria, ICriteriaQuery criteriaQuery)
5745
{
58-
return projection is IEnhancedProjection
59-
? ((IEnhancedProjection)projection).GetColumnAliases(position, criteria, criteriaQuery)
60-
: this.GetColumnAliases(position);
46+
return projection.GetColumnAliases(position, criteria, criteriaQuery);
6147
}
6248

6349
public string[] GetColumnAliases(string alias, int position, ICriteria criteria, ICriteriaQuery criteriaQuery)

src/NHibernate/Criterion/AvgProjection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public override SqlString ToSqlString(ICriteria criteria, int loc, ICriteriaQuer
3131
parameter = criteriaQuery.GetColumn(criteria, propertyName);
3232
}
3333
string expression = string.Format("{0}(cast({1} as {2})) as {3}", aggregate, parameter, sqlType,
34-
GetColumnAliases(loc)[0]);
34+
GetColumnAliases(loc, criteria, criteriaQuery)[0]);
3535
return new SqlString(expression);
3636
}
3737

0 commit comments

Comments
 (0)