我在我的项目中使用 Nhibernate 2.1.0。
我有一个带有属性 Path 的 Item 类和以下命名查询:
<hibernate-mapping xmlns='urn:nhibernate-mapping-2.2'>
<sql-query name='updateUNC'>
<query-param name='oldUNC' type='String'/>
<query-param name='newUNC' type='String'/>
<![CDATA[
update Item s set
s.Path= replace(s.Path,:oldUNC, :newUNC),
]]>
</sql-query>
</hibernate-mapping>
我使用以下代码执行命名查询:
session.GetNamedQuery("updateUNC")
.SetString("oldUNC", "old")
.SetString("newUNC", "new")
.ExecuteUpdate();
但我得到以下异常:
NHibernate.Hql.Ast.ANTLR.QuerySyntaxException was unhandled
Message="Can't determine SqlType of parameter name=oldUNC, expectedType=Unknow\n Possible cause: wrong case-sensitive property-name."
Source="NHibernate"
StackTrace:
at NHibernate.Hql.Ast.ANTLR.Exec.BasicExecutor.Execute(QueryParameters parameters, ISessionImplementor session) in C:\Projects\Nhibernate\nhibernate2.1.0\src\NHibernate\Hql\Ast\ANTLR\Exec\BasicExecutor.cs:line 66
at NHibernate.Hql.Ast.ANTLR.QueryTranslatorImpl.ExecuteUpdate(QueryParameters queryParameters, ISessionImplementor session) in C:\Projects\Nhibernate\nhibernate2.1.0\src\NHibernate\Hql\Ast\ANTLR\QueryTranslatorImpl.cs:line 149
at NHibernate.Engine.Query.HQLQueryPlan.PerformExecuteUpdate(QueryParameters queryParameters, ISessionImplementor session) in C:\Projects\Nhibernate\nhibernate2.1.0\src\NHibernate\Engine\Query\HQLQueryPlan.cs:line 328
at NHibernate.Impl.StatelessSessionImpl.ExecuteUpdate(String query, QueryParameters queryParameters) in C:\Projects\Nhibernate\nhibernate2.1.0\src\NHibernate\Impl\StatelessSessionImpl.cs:line 962
at NHibernate.Impl.QueryImpl.ExecuteUpdate() in C:\Projects\Nhibernate\nhibernate2.1.0\src\NHibernate\Impl\QueryImpl.cs:line 120
at Catalog.Repository.ItemsRepository.ChangeUNC(String oldUNC, String newUNC) in C:\Projects\Catalog\Catalog\Repository\ItemsRepository.cs:line 148
我还尝试使用 SetParameter<string> 和 SetParameter(":oldUNC", "old" NHibernateUtil.String) 设置参数值,但它也因同样的错误而失败。就像 NHibernate 只是忽略我说参数值是字符串:(。