nhibernate将sql存储过程映射到实体

发布于 2024-11-14 07:39:34 字数 3514 浏览 2 评论 0原文

编辑: 我找到了另一种解决方案,通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

用心笑 2024-11-21 07:39:34

通过添加此属性来更改 persistence.xml:

<property name="hibernate.query.factory_class" value="org.hibernate.hql.classic.ClassicQueryTranslatorFactory" />

我希望这会有所帮助,如果不让我知道您的 sql 版本是什么!?

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 !?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文