Skip to content

Partial fix fetching lazy property after Select in Linq #3392

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 3 commits into from
Aug 4, 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
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,18 @@ public async Task TestLinqFetchPropertyAsync()
AssertFetchProperty(person);
}

[Test]
public async Task TestLinqFetchPropertyAfterSelectAsync()
{
using var s = OpenSession();
var owner = await (s.Query<Animal>()
.Select(a => a.Owner)
.Fetch(o => o.Image)
.FirstOrDefaultAsync(o => o.Id == 1));

AssertFetchProperty(owner);
}

private static void AssertFetchProperty(Person person)
{
Assert.That(person, Is.Not.Null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,18 @@ public void TestLinqFetchProperty()
AssertFetchProperty(person);
}

[Test]
public void TestLinqFetchPropertyAfterSelect()
{
using var s = OpenSession();
var owner = s.Query<Animal>()
.Select(a => a.Owner)
.Fetch(o => o.Image)
.FirstOrDefault(o => o.Id == 1);

AssertFetchProperty(owner);
}

private static void AssertFetchProperty(Person person)
{
Assert.That(person, Is.Not.Null);
Expand Down
11 changes: 11 additions & 0 deletions src/NHibernate/Linq/IntermediateHqlTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,17 @@ public void AddFromClause(HqlTreeNode from)
_root.NodesPreOrder.OfType<HqlFrom>().First().AddChild(from);
}

internal HqlTreeNode GetFromNodeByAlias(string alias) =>
_root.NodesPreOrder
.First(x => x.AstNode.Type == HqlSqlWalker.FROM).Children
.First(x => GetNodeAlias(x) == alias);

private static string GetNodeAlias(HqlTreeNode fromNode) =>
fromNode.Children
.Select(x => x.AstNode)
.First(x => x.Type == HqlSqlWalker.ALIAS)
.Text;

internal HqlRange GetFromRangeClause()
{
return _root.NodesPreOrder.OfType<HqlFrom>().First().Children.OfType<HqlRange>().FirstOrDefault();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Linq;
using NHibernate.Hql.Ast;
using NHibernate.Hql.Ast.ANTLR;
using NHibernate.Persister.Entity;
using NHibernate.Type;
using Remotion.Linq.EagerFetching;
Expand All @@ -21,7 +22,7 @@ public void Process(FetchRequestBase resultOperator, QueryModelVisitor queryMode

public void Process(FetchRequestBase resultOperator, QueryModelVisitor queryModelVisitor, IntermediateHqlTree tree, string sourceAlias)
{
Process(resultOperator, queryModelVisitor, tree, null, sourceAlias);
Process(resultOperator, queryModelVisitor, tree, tree.GetFromNodeByAlias(sourceAlias), sourceAlias);
}

private void Process(
Expand Down Expand Up @@ -68,13 +69,12 @@ private void Process(
propType = metadata.GetPropertyType(resultOperator.RelationMember.Name);
}
}

if (propType != null && !propType.IsAssociationType)
{
if (currentNode == null)
{
currentNode = tree.GetFromRangeClause()
?? throw new InvalidOperationException($"Property {resultOperator.RelationMember.Name} cannot be fetched for this type of query.");
throw new InvalidOperationException($"Property {resultOperator.RelationMember.Name} cannot be fetched for this type of query.");
}

currentNode.AddChild(tree.TreeBuilder.Fetch());
Expand All @@ -85,12 +85,13 @@ private void Process(
{
if (componentType == null)
{
componentType = propType as ComponentType;
if (componentType == null)
if (!propType.IsComponentType)
{
throw new InvalidOperationException(
$"Property {innerFetch.RelationMember.Name} cannot be fetched from a non component type property {resultOperator.RelationMember.Name}.");
}

componentType = (ComponentType) propType;
}

var subTypeIndex = componentType.GetPropertyIndex(innerFetch.RelationMember.Name);
Expand Down