Skip to content

Fix fetching many-to-many with subclasses #3252

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Mar 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion src/NHibernate.Test/Async/NHSpecificTest/NH2174/Fixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ protected override void OnSetUp()
{
var doc = new Document {Id_Base = 1, Id_Doc = 2};
session.Save(doc);
session.Save(new DocumentDetailDocument {Id_Base = 1, Id_Doc = 2, Id_Item = 1, ReferencedDocument = doc});
var detail = new DocumentDetailDocument {Id_Base = 1, Id_Doc = 2, Id_Item = 1, ReferencedDocument = doc};
session.Save(detail);

doc.RefferedDetailsManyToMany.Add(detail);
transaction.Commit();
}
}
Expand All @@ -53,6 +55,14 @@ public async Task LinqFetchAsync()
}
}

[Test(Description = "GH-3239")]
public async Task LinqFetchManyToManyAsync()
{
using var session = OpenSession();
var result = await (session.Query<Document>().Fetch(x => x.RefferedDetailsManyToMany).FirstAsync());
Assert.That(result.RefferedDetailsManyToMany, Has.Count.EqualTo(1));
}

[Test]
public async Task QueryOverFetchAsync()
{
Expand All @@ -63,6 +73,14 @@ public async Task QueryOverFetchAsync()
}
}

[Test(Description = "GH-3239")]
public async Task QueryOverFetchManyToManyAsync()
{
using var session = OpenSession();
var result = await (session.QueryOver<Document>().Fetch(SelectMode.Fetch, x => x.RefferedDetailsManyToMany).SingleOrDefaultAsync());
Assert.That(result.RefferedDetailsManyToMany, Has.Count.EqualTo(1));
}

[Test]
public async Task LazyLoadAsync()
{
Expand All @@ -73,5 +91,13 @@ public async Task LazyLoadAsync()
Assert.That(result.RefferedDetails.Count, Is.EqualTo(1));
}
}

[Test]
public async Task LazyLoadManyToManyAsync()
{
using var session = OpenSession();
var result = await (session.Query<Document>().FirstAsync());
Assert.That(result.RefferedDetailsManyToMany.Count, Is.EqualTo(1));
}
}
}
1 change: 1 addition & 0 deletions src/NHibernate.Test/NHSpecificTest/NH2174/Entity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public override int GetHashCode()
private int _id_Doc;
private int _id_base;
public virtual IList<DocumentDetailDocument> RefferedDetails { get; set; } = new List<DocumentDetailDocument>();
public virtual IList<DocumentDetailDocument> RefferedDetailsManyToMany { get; set; } = new List<DocumentDetailDocument>();

public int Id_Doc
{
Expand Down
28 changes: 27 additions & 1 deletion src/NHibernate.Test/NHSpecificTest/NH2174/Fixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ protected override void OnSetUp()
{
var doc = new Document {Id_Base = 1, Id_Doc = 2};
session.Save(doc);
session.Save(new DocumentDetailDocument {Id_Base = 1, Id_Doc = 2, Id_Item = 1, ReferencedDocument = doc});
var detail = new DocumentDetailDocument {Id_Base = 1, Id_Doc = 2, Id_Item = 1, ReferencedDocument = doc};
session.Save(detail);

doc.RefferedDetailsManyToMany.Add(detail);
transaction.Commit();
}
}
Expand All @@ -42,6 +44,14 @@ public void LinqFetch()
}
}

[Test(Description = "GH-3239")]
public void LinqFetchManyToMany()
{
using var session = OpenSession();
var result = session.Query<Document>().Fetch(x => x.RefferedDetailsManyToMany).First();
Assert.That(result.RefferedDetailsManyToMany, Has.Count.EqualTo(1));
}

[Test]
public void QueryOverFetch()
{
Expand All @@ -52,6 +62,14 @@ public void QueryOverFetch()
}
}

[Test(Description = "GH-3239")]
public void QueryOverFetchManyToMany()
{
using var session = OpenSession();
var result = session.QueryOver<Document>().Fetch(SelectMode.Fetch, x => x.RefferedDetailsManyToMany).SingleOrDefault();
Assert.That(result.RefferedDetailsManyToMany, Has.Count.EqualTo(1));
}

[Test]
public void LazyLoad()
{
Expand All @@ -62,5 +80,13 @@ public void LazyLoad()
Assert.That(result.RefferedDetails.Count, Is.EqualTo(1));
}
}

[Test]
public void LazyLoadManyToMany()
{
using var session = OpenSession();
var result = session.Query<Document>().First();
Assert.That(result.RefferedDetailsManyToMany.Count, Is.EqualTo(1));
}
}
}
13 changes: 13 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/NH2174/Mappings.hbm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,18 @@
</key>
<one-to-many class="DocumentDetailDocument" />
</bag>

<bag name="RefferedDetailsManyToMany" lazy="true" generic="true" cascade="all">
<key>
<column name="id_Doc_detail" />
<column name="id_base_detail" />
</key>
<many-to-many class="DocumentDetailDocument" >
<column name="id_item" />
<column name="Version" />
<column name="id_Doc" />
<column name="id_base" />
</many-to-many>
</bag>
</class>
</hibernate-mapping>
17 changes: 12 additions & 5 deletions src/NHibernate/Engine/TableGroupJoinHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,18 @@ private static bool NeedsTableGroupJoin(IReadOnlyList<IJoin> joins, SqlString[]

foreach (var join in joins)
{
var entityPersister = GetEntityPersister(join.Joinable);
var entityPersister = GetEntityPersister(join.Joinable, out var isManyToMany);
if (entityPersister?.HasSubclassJoins(includeSubclasses && isSubclassIncluded(join.Alias)) != true)
continue;

if (hasWithClause)
return true;

if (entityPersister.ColumnsDependOnSubclassJoins(join.RHSColumns))
if (!isManyToMany // many-to-many keys are stored in separate table
&& entityPersister.ColumnsDependOnSubclassJoins(join.RHSColumns))
{
return true;
}
}

return false;
Expand All @@ -91,14 +94,16 @@ private static SqlString GetTableGroupJoinWithClause(SqlString[] withClauseFragm
var isAssociationJoin = lhsColumns.Length > 0;
if (isAssociationJoin)
{
var entityPersister = GetEntityPersister(first.Joinable);
var entityPersister = GetEntityPersister(first.Joinable, out var isManyToMany);
string rhsAlias = first.Alias;
string[] rhsColumns = first.RHSColumns;
for (int j = 0; j < lhsColumns.Length; j++)
{
fromFragment.Add(lhsColumns[j])
.Add("=")
.Add(entityPersister?.GenerateTableAliasForColumn(rhsAlias, rhsColumns[j]) ?? rhsAlias)
.Add((entityPersister == null || isManyToMany) // many-to-many keys are stored in separate table
? rhsAlias
: entityPersister.GenerateTableAliasForColumn(rhsAlias, rhsColumns[j]))
.Add(".")
.Add(rhsColumns[j]);
if (j != lhsColumns.Length - 1)
Expand All @@ -111,12 +116,14 @@ private static SqlString GetTableGroupJoinWithClause(SqlString[] withClauseFragm
return fromFragment.ToSqlString();
}

private static AbstractEntityPersister GetEntityPersister(IJoinable joinable)
private static AbstractEntityPersister GetEntityPersister(IJoinable joinable, out bool isManyToMany)
{
isManyToMany = false;
if (!joinable.IsCollection)
return joinable as AbstractEntityPersister;

var collection = (IQueryableCollection) joinable;
isManyToMany = collection.IsManyToMany;
return collection.ElementType.IsEntityType ? collection.ElementPersister as AbstractEntityPersister : null;
}

Expand Down