尝试使用 Fluent NHibernate 调用存储过程时出现命名查询未知错误
我正在为一个项目设置 NHibernate,并且有一些疑问,由于它们的复杂性,我们将保留为存储过程。我希望能够使用 NHibernate 来调用存储过程,但遇到了一个我无法弄清楚的错误。由于我使用的是 Fluent NHibernate,因此我按照此处的建议使用混合模式映射。但是,当我运行该应用程序时,出现“命名查询未知:AccountsGetSingle”异常,我不明白为什么。我认为我的 HBM 映射可能有问题,因为我不太熟悉使用它们,但我不确定。
我的 NHibernate 配置代码是:
private ISessionFactory CreateSessionFactory()
{
return Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2005
.ConnectionString((conn => conn.FromConnectionStringWithKey("CIDB")))
.ShowSql())
.Mappings(m =>
{
m.HbmMappings.AddFromAssemblyOf<Account>();
m.FluentMappings.AddFromAssemblyOf<Account>();
})
.BuildSessionFactory();
}
我的 hbm.xml 文件是:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<sql-query name="AccountsGetSingle">
<return alias="Account" class="Core, Account"></return>
exec AccountsGetSingle
</sql-query>
</hibernate-mapping>
我调用 sproc 的代码如下所示:
public Account Get()
{
return _conversation.Session
.GetNamedQuery("AccountsGetSingle")
.UniqueResult<Account>();
}
任何想法或想法将不胜感激。谢谢。
更新: @kibbled_bits 的建议让我得到了我正在寻找的最终结果(能够从 NHibernate 调用存储过程),但我仍然不知道为什么我上面列出的方法不不工作。我仍然很好奇为什么,因为它可能为未来的问题提供有价值的见解。
I'm working on setting up NHibernate for a project and I have a few queries that, due to their complexity, we will be leaving as stored procedures. I'd like to be able to use NHibernate to call the sprocs, but have run into an error I can't figure out. Since I'm using Fluent NHibernate I'm using mixed mode mapping as recommended here. However, when I run the app I get a "Named query not known: AccountsGetSingle" exception and I can't figure out why. I think I might have a problem with my HBM mapping since I'm not very familiar with using them but I'm not sure.
My NHibernate configuration code is:
private ISessionFactory CreateSessionFactory()
{
return Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2005
.ConnectionString((conn => conn.FromConnectionStringWithKey("CIDB")))
.ShowSql())
.Mappings(m =>
{
m.HbmMappings.AddFromAssemblyOf<Account>();
m.FluentMappings.AddFromAssemblyOf<Account>();
})
.BuildSessionFactory();
}
My hbm.xml file is:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<sql-query name="AccountsGetSingle">
<return alias="Account" class="Core, Account"></return>
exec AccountsGetSingle
</sql-query>
</hibernate-mapping>
And the code where I am calling the sproc looks like this:
public Account Get()
{
return _conversation.Session
.GetNamedQuery("AccountsGetSingle")
.UniqueResult<Account>();
}
Any thoughts or ideas would be appreciated. Thanks.
Update: @kibbled_bits's suggestion get me the end result that I'm looking for (Being able to call a Stored Procedure from NHibernate), but I still don't know why the approach I have listed above doesn't work. I'm still curious as to why since it might provide valuable insight into future problems.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当我必须使用存储过程时(只有当我被迫这样做时才会发生)。我更愿意使用以下方法来执行它们:
.SetInt32/DateTime 的第一个参数只是参数的序号位置。
When I have to use stored procedures (which only occurs when I'm forced to). I much rather use the following method to execute them:
The first parameter to .SetInt32/DateTime is just the ordinal position of the parameter.
我收到了相同的错误消息,为我解决的方法是确保我的 hbm.xml 文件属性“构建操作”设置为“嵌入式资源”,因此您可能想再试一次。
I got the same error message and what solved it for me was to ensure that my hbm.xml file property "Build action" was set to "Embedded Resource", so you may want to give it another try.
我已经多次被这个错误所困扰。
另外两个问题也可能导致此问题。
不添加 hbm 映射。
流利地我有以下内容。
我错过了视图类(它与带有映射信息的 hbm 文件配对)。我得到了错误。
当我添加新视图“typeof(CompanyAtoZListingView)”时,它工作正常。
还要检查 hbm 文件以确保参数正确。
I have been caught out by this error a number of times.
Two other issues can cause this.
Not adding the hbm mapping.
In fluent I have the following.
Where I miss the view class out (which pairs up with an hbm file with the mapping information). I get the error.
When I added the new view 'typeof(CompanyAtoZListingView)' it worked fine.
Also do check in the hbm file to make sure the parameters are correct.