I am getting problem while working with positional parameters in Nhbernate.
Criteria GroupProperty is emitting sql with both named and positional variables.
This
Workaround
There is a bug in NHibernate when using group by and SqlFunction parameters.
"If one applies Projections.GroupProperty(customProjection), the parameters in the projection are sent only once (for the SELECT clause), while the parameters in the GROUP BY clause are positional and missing in the query..."
(see)
Ran into the same bug and solved it by adding Custom SQL Functions to NHibernate at Runtime, (see)
The workaround moves constant parameters out of the Projections.SqlFunction call and into the definition of the custom function ("year_week").
Old failing:
Projections.GroupProperty(
Projections.Cast(NHibernateUtil.AnsiString,
Projections.SqlFunction("to_char", NHibernateUtil.AnsiChar,
Projections.Property(() => myAlias.Date),
Projections.Constant("IYYYIW") // Turns into "?" in group by
)
)
)
Work around:
Projections.GroupProperty(
Projections.Cast(NHibernateUtil.AnsiString,
Projections.SqlFunction("year_week", NHibernateUtil.AnsiChar,
Projections.Property(() => myAlias.Date)
// constant moved to function definition
)
)
)
Function "year_week" defined like this:
DialectExtensions.RegisterFunction(sessionFactory, "year_week", new SQLFunctionTemplate(NHibernateUtil.String, "TO_CHAR(?1,'IYYYIW')"));