Skip to content

Commit 2c7edab

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 f0644cf commit 2c7edab

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
@@ -74,11 +74,15 @@ protected override IQueryExpression ExpandParameters(IDictionary<string, TypedVa
7474
}
7575

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

8084
foreach (var pair in map)
81-
{
85+
{
8286
key.Append(' ');
8387
key.Append(pair.Key);
8488
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)