在nHibernate命名查询中使用replace sql函数

发布于 2024-07-28 00:11:17 字数 2257 浏览 9 评论 0原文

我在我的项目中使用 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设置参数值 和 SetParameter(":oldUNC", "old" NHibernateUtil.String) 但它也因相同的错误而失败。 就像 NHibernate 只是忽略我说参数值是字符串:(。

I am using Nhibernate 2.1.0 in my project.

I have the an Item class with property Path and the following named query:

<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>

I execute the named query using the following code:

session.GetNamedQuery("updateUNC")
       .SetString("oldUNC", "old")
       .SetString("newUNC", "new")
       .ExecuteUpdate();

But I get the following exception:

 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

I also tried setting the parameter value using SetParameter<string> and SetParameter(":oldUNC", "old" NHibernateUtil.String) but it also failed with the same error. It is like NHibernate just ignore me saying thet teh parameter value is string :(.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心奴独伤 2024-08-04 00:11:17

只是猜测:您是否尝试在映射文件中写入小写字符串?

<query-param name='oldUNC' type='string'/>

Just a guess: Did you try to write string with lower case in the mapping file?

<query-param name='oldUNC' type='string'/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文