nhibernate将sql存储过程映射到实体
编辑: 我找到了另一种解决方案,通过 NHibernate 调用存储过程并将其映射到我的实体上:
var campaignsItems = nhSession.CreateSQLQuery("exec Select_List_Campaigns :currentLatDegrees, :currentLonDegrees, :radiusInMiles, :pageSize, :currentPage, :search, :isTop, :topDealPercente")
.SetParameter("currentLatDegrees", currentLatDegrees)
.SetParameter("currentLonDegrees", currentLonDegrees)
.SetParameter("radiusInMiles", radius)
.SetParameter("pageSize", pageSize)
.SetParameter("currentPage", page)
.SetParameter("search", search)
.SetParameter("isTop", isTop)
.SetParameter("topDealPercente", topDealPercente)
.SetResultTransformer(Transformers.AliasToBean<CampaignListModel>())
.List<CampaignListModel>();
如您所见,在这里我们可以使用 SetResultTransformer 帮助设置参数并将结果映射到我们的实体。如果类中的属性等于存储过程结果集中的列名称,那么一切都很好。
老问题:
我有一些存储过程,想要将我的实体映射到该存储过程的结果记录集。我的 hbm 文件:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<sql-query name="Select_Latest_Campaigns">
<return alias="cfl" class="Austradelia.Core.ProcedureResultEntities.CampaignForList, Austradelia.Core">
<return-property name="Id" column="Id"></return-property>
<return-property name="ShortDescription" column="ShortDescription"></return-property>
<return-property name="CampaignType" column="CampaignType"></return-property>
<return-property name="StartDate" column="StartDate"></return-property>
<return-property name="FinishDate" column="FinishDate"></return-property>
<return-property name="Quantity" column="Quantity"></return-property>
<return-property name="TotalRedeemsCount" column="TotalRedeemsCount"></return-property>
<return-property name="MerchantId" column="MerchantId"></return-property>
<return-property name="MerchantBusinessName" column="MerchantBusinessName"></return-property>
<return-property name="MerchantAddress" column="MerchantAddress"></return-property>
<return-property name="MerchantFollowersCount" column="MerchantFollowersCount"></return-property>
<return-property name="TipsCount" column="TipsCount"></return-property>
<return-property name="LikesCount" column="LikesCount"></return-property>
<return-property name="CategoryId" column="CategoryId"></return-property>
<return-property name="CategoryName" column="CategoryName"></return-property>
<return-property name="RowNumber" column="RowNumber"></return-property>
<return-property name="TotalCount" column="TotalCount"></return-property>
</return>
exec Select_Latest_Campaigns :currentLatDegrees, :currentLonDegrees, :radiusInMiles, :pageSize, :currentPage, :search
</sql-query>
</hibernate-mapping>
当我尝试执行此查询时,出现此异常:
Exception Details: NHibernate.HibernateException: Errors in named queries: {Select_Latest_Campaigns}
On:
Line 43: return configuration.BuildSessionFactory();
有什么问题吗?
Edited:
I found another solution to call stored procedure via NHibernate and map it on my entity:
var campaignsItems = nhSession.CreateSQLQuery("exec Select_List_Campaigns :currentLatDegrees, :currentLonDegrees, :radiusInMiles, :pageSize, :currentPage, :search, :isTop, :topDealPercente")
.SetParameter("currentLatDegrees", currentLatDegrees)
.SetParameter("currentLonDegrees", currentLonDegrees)
.SetParameter("radiusInMiles", radius)
.SetParameter("pageSize", pageSize)
.SetParameter("currentPage", page)
.SetParameter("search", search)
.SetParameter("isTop", isTop)
.SetParameter("topDealPercente", topDealPercente)
.SetResultTransformer(Transformers.AliasToBean<CampaignListModel>())
.List<CampaignListModel>();
As you can see, here we can Set parameters and map results to our Entity with SetResultTransformer help. If properties in your class equals to the name of the columns in your stored procedure result set then all be fine.
Old question:
I have some stored procedure and whant to map my entity to result record set of this stored procedure. My hbm file:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<sql-query name="Select_Latest_Campaigns">
<return alias="cfl" class="Austradelia.Core.ProcedureResultEntities.CampaignForList, Austradelia.Core">
<return-property name="Id" column="Id"></return-property>
<return-property name="ShortDescription" column="ShortDescription"></return-property>
<return-property name="CampaignType" column="CampaignType"></return-property>
<return-property name="StartDate" column="StartDate"></return-property>
<return-property name="FinishDate" column="FinishDate"></return-property>
<return-property name="Quantity" column="Quantity"></return-property>
<return-property name="TotalRedeemsCount" column="TotalRedeemsCount"></return-property>
<return-property name="MerchantId" column="MerchantId"></return-property>
<return-property name="MerchantBusinessName" column="MerchantBusinessName"></return-property>
<return-property name="MerchantAddress" column="MerchantAddress"></return-property>
<return-property name="MerchantFollowersCount" column="MerchantFollowersCount"></return-property>
<return-property name="TipsCount" column="TipsCount"></return-property>
<return-property name="LikesCount" column="LikesCount"></return-property>
<return-property name="CategoryId" column="CategoryId"></return-property>
<return-property name="CategoryName" column="CategoryName"></return-property>
<return-property name="RowNumber" column="RowNumber"></return-property>
<return-property name="TotalCount" column="TotalCount"></return-property>
</return>
exec Select_Latest_Campaigns :currentLatDegrees, :currentLonDegrees, :radiusInMiles, :pageSize, :currentPage, :search
</sql-query>
</hibernate-mapping>
When I try to execute this query, I have this exception:
Exception Details: NHibernate.HibernateException: Errors in named queries: {Select_Latest_Campaigns}
On:
Line 43: return configuration.BuildSessionFactory();
Any ideas what wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通过添加此属性来更改 persistence.xml:
我希望这会有所帮助,如果不让我知道您的 sql 版本是什么!?
change persistence.xml by adding this property:
i hope this help if not let me know what is you sql version !?