在nHibernate命名查询中使用replace sql函数
我在我的项目中使用 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
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只是猜测:您是否尝试在映射文件中写入小写字符串?
Just a guess: Did you try to write string with lower case in the mapping file?