NHibernate命名查询参数编号@ p1 @ p2等 [英] NHibernate Named Query Parameter Numbering @p1 @p2 etc

查看:75
本文介绍了NHibernate命名查询参数编号@ p1 @ p2等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个同事最近遇到了一个问题,他将一个日期参数传递给一个命名查询.在查询中,该参数被使用两次,一次在表达式中,一次在GROUP BY子句中.令我们惊讶的是,我们发现NHibernate使用了两个变量,并两次将单个命名参数发送为@ p1和@ p2.此行为导致SQL查询失败,通常为"select子句中的列不在group by子句中"(我对此进行了解释).

A colleague recently ran into a problem where he was passing in a single date parameter to a named query. In the query, the parameter was being used twice, once in an expression and once in a GROUP BY clause. Much to our surprise, we discovered that NHibernate used two variables and sent the single named parameter in twice, as @p1 and @p2. This behaviour caused SQL to fail the query, with the usual "a column in the select clause is not in the group by clause" (I paraphrase ofcourse).

这种行为正常吗?可以更改吗?在我看来,如果您有一个像:startDate这样的参数名称,则无论您在查询中引用:startDate多少次,NHibernate都只需要传递@ p1即可.

Is this behaviour normal? Can it be changed? Seems to me that if you have a parameter name like :startDate, NHibernate only needs to pass in @p1 no matter how many times you might refer to :startDate in the query.

有任何评论吗?

通过使用另一个子查询来克服SQL解析错误来解决此问题.

The problem was worked around by using another sub-query to overcome the SQL parsing error.

推荐答案

例如,如果您使用的数据库不支持命名参数,则NHibernate将使用位置参数,并复制该值.

That can happen, for example, if the DB you are using does not support named parameters, in which case NHibernate uses positional ones, and it copies the value.

这篇关于NHibernate命名查询参数编号@ p1 @ p2等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆