Skip to content

Commit a21fb4a

Browse files
committed
Inject into CriteriaLoader info about projected entities
1 parent 43e4fd6 commit a21fb4a

14 files changed

+266
-270
lines changed

src/NHibernate.Test/Async/Criteria/EntityProjectionsTest.cs

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,54 @@ public async Task AliasedEntityProjectionAsync()
191191
}
192192
}
193193

194+
[Test]
195+
public async Task EntityProjectionAsSelectExpressionAsync()
196+
{
197+
using (var sqlLog = new SqlLogSpy())
198+
using (var session = OpenSession())
199+
{
200+
EntitySimpleChild child1 = null;
201+
child1 = await (session
202+
.QueryOver<EntityComplex>()
203+
.JoinAlias(ep => ep.Child1, () => child1)
204+
.Select(ec => child1.AsEntity())
205+
.Take(1).SingleOrDefaultAsync<EntitySimpleChild>());
206+
207+
Assert.That(child1, Is.Not.Null);
208+
Assert.That(NHibernateUtil.IsInitialized(child1), Is.True, "Object must be initialized");
209+
Assert.That(sqlLog.Appender.GetEvents().Length, Is.EqualTo(1), "Only one SQL select is expected");
210+
}
211+
}
212+
213+
[Test]
214+
public async Task EntityProjectionLockModeAsync()
215+
{
216+
var upgradeHint = Dialect.ForUpdateString;
217+
if(string.IsNullOrEmpty(upgradeHint))
218+
upgradeHint = this.Dialect.AppendLockHint(LockMode.Upgrade, string.Empty);
219+
if (string.IsNullOrEmpty(upgradeHint))
220+
{
221+
Assert.Ignore($"Upgrade hint is not supported by dialect {Dialect.GetType().Name}");
222+
}
223+
224+
using (var sqlLog = new SqlLogSpy())
225+
using (var session = OpenSession())
226+
{
227+
EntitySimpleChild child1 = null;
228+
child1 = await (session
229+
.QueryOver<EntityComplex>()
230+
.JoinAlias(ep => ep.Child1, () => child1)
231+
.Lock(() => child1).Upgrade
232+
.Select(Projections.Entity(() => child1))
233+
.Take(1).SingleOrDefaultAsync<EntitySimpleChild>());
234+
235+
Assert.That(child1, Is.Not.Null);
236+
Assert.That(NHibernateUtil.IsInitialized(child1), Is.True, "Object must be initialized");
237+
Assert.That(sqlLog.Appender.GetEvents().Length, Is.EqualTo(1), "Only one SQL select is expected");
238+
Assert.That(sqlLog.Appender.GetEvents()[0].RenderedMessage, Does.Contain(upgradeHint));
239+
}
240+
}
241+
194242
[Test]
195243
public async Task MultipleLazyEntityProjectionsAsync()
196244
{
@@ -313,6 +361,7 @@ class MultipleEntitiesResult
313361
public EntitySimpleChild Child2 { get; set; }
314362
public EntityComplex SameAsRootChild { get; set; }
315363
public EntitySimpleChild NullListElem { get; set; }
364+
public string Name { get; set; }
316365
}
317366

318367
[Test]
@@ -335,9 +384,11 @@ public async Task MultipleEntitiesProjectionsToResultTransformerAsync()
335384
.JoinAlias(ep => ep.SameTypeChild, () => sameAsRootChild)
336385
.JoinAlias(ep => ep.ChildrenList, () => nullListElem, JoinType.LeftOuterJoin)
337386
.Select(
338-
Projections.Alias(Projections.RootEntity(), nameof(r.Root)),
387+
Projections.RootEntity().WithAlias(nameof(r.Root)),
339388
Projections.Entity(() => child1),
389+
Projections.Property(() => child2.Name).As(nameof(r.Name)),
340390
Projections.Entity(() => child2),
391+
Projections.Property(() => child1.Id),
341392
Projections.Entity(() => sameAsRootChild),
342393
Projections.Entity(() => nullListElem)
343394
)
@@ -362,7 +413,8 @@ public async Task ReadOnlyProjectionAsync()
362413
{
363414
EntityComplex entityRoot = await (session
364415
.QueryOver<EntityComplex>()
365-
.Select(Projections.RootEntity().SetReadonly(true))
416+
.Select(Projections.RootEntity())
417+
.ReadOnly()
366418
.Take(1).SingleOrDefaultAsync());
367419

368420
Assert.That(session.IsReadOnly(entityRoot), Is.True, "Object must be loaded readonly.");

src/NHibernate.Test/Criteria/EntityProjectionsTest.cs

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,54 @@ public void AliasedEntityProjection()
180180
}
181181
}
182182

183+
[Test]
184+
public void EntityProjectionAsSelectExpression()
185+
{
186+
using (var sqlLog = new SqlLogSpy())
187+
using (var session = OpenSession())
188+
{
189+
EntitySimpleChild child1 = null;
190+
child1 = session
191+
.QueryOver<EntityComplex>()
192+
.JoinAlias(ep => ep.Child1, () => child1)
193+
.Select(ec => child1.AsEntity())
194+
.Take(1).SingleOrDefault<EntitySimpleChild>();
195+
196+
Assert.That(child1, Is.Not.Null);
197+
Assert.That(NHibernateUtil.IsInitialized(child1), Is.True, "Object must be initialized");
198+
Assert.That(sqlLog.Appender.GetEvents().Length, Is.EqualTo(1), "Only one SQL select is expected");
199+
}
200+
}
201+
202+
[Test]
203+
public void EntityProjectionLockMode()
204+
{
205+
var upgradeHint = Dialect.ForUpdateString;
206+
if(string.IsNullOrEmpty(upgradeHint))
207+
upgradeHint = this.Dialect.AppendLockHint(LockMode.Upgrade, string.Empty);
208+
if (string.IsNullOrEmpty(upgradeHint))
209+
{
210+
Assert.Ignore($"Upgrade hint is not supported by dialect {Dialect.GetType().Name}");
211+
}
212+
213+
using (var sqlLog = new SqlLogSpy())
214+
using (var session = OpenSession())
215+
{
216+
EntitySimpleChild child1 = null;
217+
child1 = session
218+
.QueryOver<EntityComplex>()
219+
.JoinAlias(ep => ep.Child1, () => child1)
220+
.Lock(() => child1).Upgrade
221+
.Select(Projections.Entity(() => child1))
222+
.Take(1).SingleOrDefault<EntitySimpleChild>();
223+
224+
Assert.That(child1, Is.Not.Null);
225+
Assert.That(NHibernateUtil.IsInitialized(child1), Is.True, "Object must be initialized");
226+
Assert.That(sqlLog.Appender.GetEvents().Length, Is.EqualTo(1), "Only one SQL select is expected");
227+
Assert.That(sqlLog.Appender.GetEvents()[0].RenderedMessage, Does.Contain(upgradeHint));
228+
}
229+
}
230+
183231
[Test]
184232
public void MultipleLazyEntityProjections()
185233
{
@@ -302,6 +350,7 @@ class MultipleEntitiesResult
302350
public EntitySimpleChild Child2 { get; set; }
303351
public EntityComplex SameAsRootChild { get; set; }
304352
public EntitySimpleChild NullListElem { get; set; }
353+
public string Name { get; set; }
305354
}
306355

307356
[Test]
@@ -324,9 +373,11 @@ public void MultipleEntitiesProjectionsToResultTransformer()
324373
.JoinAlias(ep => ep.SameTypeChild, () => sameAsRootChild)
325374
.JoinAlias(ep => ep.ChildrenList, () => nullListElem, JoinType.LeftOuterJoin)
326375
.Select(
327-
Projections.Alias(Projections.RootEntity(), nameof(r.Root)),
376+
Projections.RootEntity().WithAlias(nameof(r.Root)),
328377
Projections.Entity(() => child1),
378+
Projections.Property(() => child2.Name).As(nameof(r.Name)),
329379
Projections.Entity(() => child2),
380+
Projections.Property(() => child1.Id),
330381
Projections.Entity(() => sameAsRootChild),
331382
Projections.Entity(() => nullListElem)
332383
)
@@ -351,7 +402,8 @@ public void ReadOnlyProjection()
351402
{
352403
EntityComplex entityRoot = session
353404
.QueryOver<EntityComplex>()
354-
.Select(Projections.RootEntity().SetReadonly(true))
405+
.Select(Projections.RootEntity())
406+
.ReadOnly()
355407
.Take(1).SingleOrDefault();
356408

357409
Assert.That(session.IsReadOnly(entityRoot), Is.True, "Object must be loaded readonly.");

src/NHibernate/Async/Criterion/EntityProjectionType.cs

Lines changed: 0 additions & 106 deletions
This file was deleted.

0 commit comments

Comments
 (0)