Skip to content

Commit 3f74632

Browse files
bahusoidhazzik
authored andcommitted
Fix missing alias for subquery entity references in update statements (#2218)
Fixes #1015
1 parent 03500af commit 3f74632

File tree

5 files changed

+68
-8
lines changed

5 files changed

+68
-8
lines changed

src/NHibernate.Test/Async/Hql/AggregateFunctionsWithSubSelectTest.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
namespace NHibernate.Test.Hql
1717
{
1818
using System.Threading.Tasks;
19-
using System.Threading;
2019
[TestFixture]
2120
public class AggregateFunctionsWithSubSelectTestAsync : TestCaseMappingByCode
2221
{
@@ -88,7 +87,7 @@ protected override bool AppliesTo(Dialect.Dialect dialect)
8887
[TestCase("MIN", 2)]
8988
[TestCase("MAX", 2)]
9089
[TestCase("AVG", 2d)]
91-
public async Task TestAggregateFunctionAsync(string functionName, object result, CancellationToken cancellationToken = default(CancellationToken))
90+
public async Task TestAggregateFunctionAsync(string functionName, object result)
9291
{
9392
var query = "SELECT " +
9493
" d.Id, " +
@@ -107,14 +106,14 @@ protected override bool AppliesTo(Dialect.Dialect dialect)
107106
using (var session = OpenSession())
108107
using (var transaction = session.BeginTransaction())
109108
{
110-
var results = await (session.CreateQuery(query).ListAsync(cancellationToken));
109+
var results = await (session.CreateQuery(query).ListAsync());
111110

112111
Assert.That(results, Has.Count.EqualTo(1));
113112
var tuple = results[0] as object[];
114113
Assert.That(tuple, Is.Not.Null);
115114
Assert.That(tuple, Has.Length.EqualTo(2));
116115
Assert.That(tuple[1], Is.EqualTo(result));
117-
await (transaction.CommitAsync(cancellationToken));
116+
await (transaction.CommitAsync());
118117
}
119118
}
120119
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
//------------------------------------------------------------------------------
2+
// <auto-generated>
3+
// This code was generated by AsyncGenerator.
4+
//
5+
// Changes to this file may cause incorrect behavior and will be lost if
6+
// the code is regenerated.
7+
// </auto-generated>
8+
//------------------------------------------------------------------------------
9+
10+
11+
using System.Linq;
12+
using NHibernate.Linq;
13+
using NUnit.Framework;
14+
15+
namespace NHibernate.Test.NHSpecificTest.NH2951
16+
{
17+
using System.Threading.Tasks;
18+
[TestFixture]
19+
public class FixtureAsync : BugTestCase
20+
{
21+
protected override void OnTearDown()
22+
{
23+
using (ISession session = OpenSession())
24+
using (ITransaction transaction = session.BeginTransaction())
25+
{
26+
session.Delete("from System.Object");
27+
28+
session.Flush();
29+
transaction.Commit();
30+
}
31+
}
32+
33+
[Test]
34+
public async Task UpdateWithSubqueryToJoinedSubclassAsync()
35+
{
36+
using (ISession session = OpenSession())
37+
using (ITransaction transaction = session.BeginTransaction())
38+
{
39+
40+
var c = new Customer { Name = "Bob" };
41+
await (session.SaveAsync(c));
42+
43+
var i = new Invoice { Amount = 10 };
44+
await (session.SaveAsync(i));
45+
46+
await (session.FlushAsync());
47+
await (transaction.CommitAsync());
48+
}
49+
50+
using (ISession session = OpenSession())
51+
using (session.BeginTransaction())
52+
{
53+
// Using (select c.Id ...) works.
54+
string hql = "update Invoice i set i.Customer = (select c from Customer c where c.Name = 'Bob')";
55+
56+
int result = await (session.CreateQuery(hql).ExecuteUpdateAsync());
57+
58+
Assert.AreEqual(1, result);
59+
}
60+
}
61+
}
62+
}

src/NHibernate.Test/Async/NHSpecificTest/NH750/Fixture.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public async Task DeviceOfDriveAsync()
108108
await (t.CommitAsync());
109109
}
110110

111-
await (VerifyResultAsync(2,2, msg: "modified collection"));
111+
await (VerifyResultAsync(2, 2, msg: "modified collection"));
112112

113113
async Task VerifyResultAsync(int expectedInCollection, int expectedInDb, string msg)
114114
{

src/NHibernate.Test/NHSpecificTest/NH2951/Fixture.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ protected override void OnTearDown()
2020
}
2121

2222
[Test]
23-
[Ignore("Not working.")]
2423
public void UpdateWithSubqueryToJoinedSubclass()
2524
{
2625
using (ISession session = OpenSession())
@@ -49,4 +48,4 @@ public void UpdateWithSubqueryToJoinedSubclass()
4948
}
5049
}
5150
}
52-
}
51+
}

src/NHibernate/Hql/Ast/ANTLR/Tree/FromElement.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ public virtual string GetIdentityColumn()
505505
{
506506
propertyName = NHibernate.Persister.Entity.EntityPersister.EntityID;
507507
}
508-
if (Walker.StatementType == HqlSqlWalker.SELECT)
508+
if (Walker.StatementType == HqlSqlWalker.SELECT || Walker.IsSubQuery)
509509
{
510510
cols = GetPropertyMapping(propertyName).ToColumns(table, propertyName);
511511
}

0 commit comments

Comments
 (0)