Skip to content

Commit 36d96d1

Browse files
Avoid re-translating NhLinqExpression.
NhLinqExpression instances may be translated multiple times in two cases: * When they have list parameters * When their plan is not cacheable, since it is looked-up two times in cache per execution, and (re)generated in case of cache miss.
1 parent 956bffe commit 36d96d1

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/NHibernate/Impl/ExpressionQueryImpl.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,15 @@ protected override IQueryExpression ExpandParameters(IDictionary<string, TypedVa
6767
}
6868

6969
//TODO: Do we need to translate expression one more time here?
70+
// This is not much an issue anymore: ExpressionQueryImpl are currently created only with NhLinqExpression
71+
// which do cache their translation. By the way, the false transmitted below as filter parameter to
72+
// Translate is wrong, since this ExpandParameters may also be called from ExpressionFilterImpl.
73+
// But that does not matter for NhLinqExpression.
7074
var newTree = ParameterExpander.Expand(QueryExpression.Translate(Session.Factory, false), map);
7175
var key = new StringBuilder(QueryExpression.Key);
7276

7377
foreach (var pair in map)
74-
{
78+
{
7579
key.Append(' ');
7680
key.Append(pair.Key);
7781
key.Append(':');

src/NHibernate/Linq/NhLinqExpression.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,14 @@ public NhLinqExpression(Expression expression, ISessionFactoryImplementor sessio
6868

6969
public IASTNode Translate(ISessionFactoryImplementor sessionFactory, bool filter)
7070
{
71+
if (ExpressionToHqlTranslationResults != null)
72+
{
73+
// Query has already been translated. Arguments do not really matters, because queries are anyway tied
74+
// to a single session factory and cannot switch from being a filter query (query on mapped collection)
75+
// or not.
76+
return ExpressionToHqlTranslationResults.Statement.AstNode;
77+
}
78+
7179
var requiredHqlParameters = new List<NamedParameterDescriptor>();
7280
var queryModel = NhRelinqQueryParser.Parse(_expression);
7381
var visitorParameters = new VisitorParameters(sessionFactory, _constantToParameterMap, requiredHqlParameters,

0 commit comments

Comments
 (0)