我正在为一个项目设置 NHibernate,并且有一些疑问,由于它们的复杂性,我们将保留为存储过程。我希望能够使用 NHibernate 来调用存储过程,但遇到了一个我无法弄清楚的错误。由于我使用的是 Fluent NHibernate,所以我按照建议使用混合模式映射here http://wiki.fluentnhibernate.org/Current_NHibernate_Features_Not_Supported_In_Fluent。但是,当我运行该应用程序时,我收到“命名查询未知: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>
我调用存储过程的代码如下所示:
public Account Get()
{
return _conversation.Session
.GetNamedQuery("AccountsGetSingle")
.UniqueResult<Account>();
}
任何想法或想法将不胜感激。谢谢。
Update:@kibbled_bits 的建议让我得到了我正在寻找的最终结果(能够从 NHibernate 调用存储过程),但我仍然不知道为什么上面列出的方法不起作用。我仍然很好奇为什么,因为它可能为未来的问题提供有价值的见解。
When I have使用存储过程(只有当我被迫这样做时才会发生)。我更愿意使用以下方法来执行它们:
var list = Session.CreateSQLQuery("exec GetCustomerByNaturalKey ?, ?")
.AddEntity(typeof(Customer))
.SetInt32(0, customerNo)
.SetDateTime(1, createdDate)
.List<Customer>();
.SetInt32/DateTime 的第一个参数只是参数的序号位置。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)