Skip to content

Commit 04886b4

Browse files
committed
Restore old behavior for implicit join
1 parent 8875e00 commit 04886b4

File tree

6 files changed

+36
-20
lines changed

6 files changed

+36
-20
lines changed

src/NHibernate.DomainModel/Container.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ public Glarch Glarch
9292
private IDictionary<string, Ternary> _ternaryMap;
9393
//<set> mapping
9494
private ISet<Ternary> _ternarySet;
95+
private One _one;
9596

9697
public virtual IList<Simple> OneToMany
9798
{
@@ -129,6 +130,12 @@ public virtual long Id
129130
set { _id = value; }
130131
}
131132

133+
public virtual One One
134+
{
135+
get => _one;
136+
set => _one = value;
137+
}
138+
132139
public virtual IList<Contained> Bag
133140
{
134141
get { return _bag; }

src/NHibernate.DomainModel/Container.hbm.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
>
2323
<generator class="native" />
2424
</id>
25-
25+
<one-to-one name="One" />
2626
<list
2727
name="OneToMany"
2828
lazy="true"

src/NHibernate.Test/Async/Legacy/ParentChildTest.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -359,8 +359,8 @@ public async Task CollectionQueryAsync()
359359
if (!TestDialect.SupportsEmptyInsertsOrHasNonIdentityNativeGenerator)
360360
Assert.Ignore("Support of empty inserts is required");
361361

362-
ISession s = OpenSession();
363-
ITransaction t = s.BeginTransaction();
362+
using var s = OpenSession();
363+
using var t = s.BeginTransaction();
364364

365365
Simple s1 = new Simple();
366366
s1.Name = "s";
@@ -383,10 +383,15 @@ public async Task CollectionQueryAsync()
383383
l.Add(null);
384384
l.Add(s2);
385385
c.ManyToMany = l;
386+
c.One = new One { Value = "one" };
387+
await (s.SaveAsync(c.One));
386388
await (s.SaveAsync(c));
387389

388390
Assert.AreEqual(1,
389391
(await (s.CreateQuery("select c from c in class ContainerX, s in class Simple where c.OneToMany[2] = s").ListAsync
392+
())).Count);
393+
Assert.AreEqual(1,
394+
(await (s.CreateQuery("select c from c in class ContainerX, s in class Simple where c.OneToMany[2] = s and c.One != null").ListAsync
390395
())).Count);
391396
Assert.AreEqual(1,
392397
(await (s.CreateQuery("select c from c in class ContainerX, s in class Simple where c.ManyToMany[2] = s").
@@ -424,13 +429,13 @@ public async Task CollectionQueryAsync()
424429
"select c from c in class ContainerX where c.ManyToMany[ c.OneToMany[0].Count ].Name = 's'").ListAsync())).
425430
Count);
426431

432+
await (s.DeleteAsync(c.One));
427433
await (s.DeleteAsync(c));
428434
await (s.DeleteAsync(s1));
429435
await (s.DeleteAsync(s2));
430436
await (s.DeleteAsync(s3));
431437

432438
await (t.CommitAsync());
433-
s.Close();
434439
}
435440

436441
[Test]

src/NHibernate.Test/Legacy/ParentChildTest.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -348,8 +348,8 @@ public void CollectionQuery()
348348
if (!TestDialect.SupportsEmptyInsertsOrHasNonIdentityNativeGenerator)
349349
Assert.Ignore("Support of empty inserts is required");
350350

351-
ISession s = OpenSession();
352-
ITransaction t = s.BeginTransaction();
351+
using var s = OpenSession();
352+
using var t = s.BeginTransaction();
353353

354354
Simple s1 = new Simple();
355355
s1.Name = "s";
@@ -372,10 +372,15 @@ public void CollectionQuery()
372372
l.Add(null);
373373
l.Add(s2);
374374
c.ManyToMany = l;
375+
c.One = new One { Value = "one" };
376+
s.Save(c.One);
375377
s.Save(c);
376378

377379
Assert.AreEqual(1,
378380
s.CreateQuery("select c from c in class ContainerX, s in class Simple where c.OneToMany[2] = s").List
381+
().Count);
382+
Assert.AreEqual(1,
383+
s.CreateQuery("select c from c in class ContainerX, s in class Simple where c.OneToMany[2] = s and c.One != null").List
379384
().Count);
380385
Assert.AreEqual(1,
381386
s.CreateQuery("select c from c in class ContainerX, s in class Simple where c.ManyToMany[2] = s").
@@ -413,13 +418,13 @@ public void CollectionQuery()
413418
"select c from c in class ContainerX where c.ManyToMany[ c.OneToMany[0].Count ].Name = 's'").List().
414419
Count);
415420

421+
s.Delete(c.One);
416422
s.Delete(c);
417423
s.Delete(s1);
418424
s.Delete(s2);
419425
s.Delete(s3);
420426

421427
t.Commit();
422-
s.Close();
423428
}
424429

425430
[Test]

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,7 @@ public void SetOrigin(FromElement origin, bool manyToMany)
590590
}
591591
else
592592
{
593-
if (this.IsImplied || Walker.IsInFrom || Walker.IsInSelect)
593+
if ((IsImplied && !JoinSequence.IsThetaStyle) || Walker.IsInFrom || Walker.IsInSelect)
594594
{
595595
origin.AddChild(this);
596596
}

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

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,7 @@ public FromElement CreateCollection(IQueryableCollection queryableCollection,
193193
if (elementType.IsEntityType)
194194
{
195195
// A collection of entities...
196-
elem = CreateEntityAssociation(role, roleAlias, joinType);
197-
//TODO: Investigate why not implicit indexed join is incorrectly generated and get rid of it
198-
if (indexed && elem.IsImplied)
199-
elem.JoinSequence.SetUseThetaStyle(true);
196+
elem = CreateEntityAssociation(role, roleAlias, joinType, indexed);
200197
}
201198
else if (elementType.IsComponentType)
202199
{
@@ -339,7 +336,8 @@ public FromElement CreateEntityJoin(
339336
private FromElement CreateEntityAssociation(
340337
string role,
341338
string roleAlias,
342-
JoinType joinType)
339+
JoinType joinType,
340+
bool implicitJoin)
343341
{
344342
FromElement elem;
345343
IQueryable entityPersister = (IQueryable)_queryableCollection.ElementPersister;
@@ -353,7 +351,7 @@ private FromElement CreateEntityAssociation(
353351
Log.Debug("createEntityAssociation() : One to many - path = {0} role = {1} associatedEntityName = {2}", _path, role, associatedEntityName);
354352
}
355353

356-
var joinSequence = CreateJoinSequence(roleAlias, joinType);
354+
var joinSequence = CreateJoinSequence(roleAlias, joinType, implicitJoin);
357355

358356
elem = CreateJoin(associatedEntityName, roleAlias, joinSequence, (EntityType) _queryableCollection.ElementType, false);
359357
elem.UseFromFragment |= elem.IsImplied && elem.Walker.IsSubQuery;
@@ -365,7 +363,7 @@ private FromElement CreateEntityAssociation(
365363
Log.Debug("createManyToMany() : path = {0} role = {1} associatedEntityName = {2}", _path, role, associatedEntityName);
366364
}
367365

368-
elem = CreateManyToMany(role, associatedEntityName, roleAlias, entityPersister, (EntityType)_queryableCollection.ElementType, joinType);
366+
elem = CreateManyToMany(role, associatedEntityName, roleAlias, entityPersister, (EntityType)_queryableCollection.ElementType, joinType, implicitJoin);
369367
_fromClause.Walker.AddQuerySpaces(_queryableCollection.CollectionSpaces);
370368
}
371369
elem.CollectionTableAlias = roleAlias;
@@ -408,15 +406,16 @@ private FromElement CreateManyToMany(
408406
string roleAlias,
409407
IEntityPersister entityPersister,
410408
EntityType type,
411-
JoinType joinType)
409+
JoinType joinType,
410+
bool implicitJoin)
412411
{
413412
FromElement elem;
414413
SessionFactoryHelperExtensions sfh = _fromClause.SessionFactoryHelper;
415414

416415
if (_inElementsFunction /*implied*/ )
417416
{
418417
// For implied many-to-many, just add the end join.
419-
JoinSequence joinSequence = CreateJoinSequence(roleAlias, joinType);
418+
JoinSequence joinSequence = CreateJoinSequence(roleAlias, joinType, implicitJoin);
420419
elem = CreateJoin(associatedEntityName, roleAlias, joinSequence, type, true);
421420
}
422421
else
@@ -428,16 +427,16 @@ private FromElement CreateManyToMany(
428427
string[] secondJoinColumns = sfh.GetCollectionElementColumns(role, roleAlias);
429428

430429
// Add the second join, the one that ends in the destination table.
431-
JoinSequence joinSequence = CreateJoinSequence(roleAlias, joinType);
430+
JoinSequence joinSequence = CreateJoinSequence(roleAlias, joinType, implicitJoin);
432431
joinSequence.AddJoin(sfh.GetElementAssociationType(_collectionType), tableAlias, joinType, secondJoinColumns);
433432
elem = CreateJoin(associatedEntityName, tableAlias, joinSequence, type, false);
434433
elem.UseFromFragment = true;
435434
}
436435
return elem;
437436
}
438437

439-
//TODO: Investigate why not implicit indexed join is incorrectly generated and get rid of implicitJoin parameter
440-
private JoinSequence CreateJoinSequence(string roleAlias, JoinType joinType, bool implicitJoin = false)
438+
//TODO: Investigate why not implicit indexed collection join is incorrectly generated and get rid of implicitJoin parameter (check IndexNode.Resolve)
439+
private JoinSequence CreateJoinSequence(string roleAlias, JoinType joinType, bool implicitJoin)
441440
{
442441
SessionFactoryHelperExtensions sessionFactoryHelper = _fromClause.SessionFactoryHelper;
443442
string[] joinColumns = Columns;

0 commit comments

Comments
 (0)