nhibernate map sql stored procedure to entity
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?
change persistence.xml by adding this property:
<property name="hibernate.query.factory_class" value="org.hibernate.hql.classic.ClassicQueryTranslatorFactory" />
i hope this help if not let me know what is you sql version !?
精彩评论