NHibernate的 - 如何映射到一个没有表(自定义的SQL查询)类 [英] NHibernate - How to map to a class that has no table (for custom sql queries)

查看:705
本文介绍了NHibernate的 - 如何映射到一个没有表(自定义的SQL查询)类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  

更新 - 编辑过的配置以增强可读性   在SO

您好,

我一直在学习NHibernate的一两天,但被陷在一个点上。

我需要能够执行自定义存储过程和使用的NHibernate将它们回映射到域类。

我有这个工作,其中的自定义查询映射回映射到数据库表,如图许多NHibernate的例子对象的情景(见下文第一部分)。

不过在配置为下面的第二部分,查询拉从目标表中只有2列。出于这个原因,我创建了一个自定义的对象,这样的NHibernate有事返回值映射到。自定义对象的属性具有相同的名称从自定义过程的返回列。

当我运行我的测试中,我得到这样一个例外:

  

NHibernate.MappingException:无   持留为:   Proj.DataEntityTracker.Domain.Entities.CustomObject

所以我想在SQL查询部分的映射是不够的NHibernate的返回值映射到对象的属性。

所以我的问题是 - 我怎么设置对此有没有相应的数据库表中,这样我可以在存储过程的结果映射到对象的映射


< XML版本=" 1.0"编码=" UTF-8英寸?>
<休眠映射的xmlns ="金塔:NHibernate的映射 -  2.2"
                   装配=" Proj.DataEntityTracker.Domain"
                   命名空间=" Proj.DataEntityTracker.Domain.Entities">

  <类名=" TrackedEntityProperty"表=" TrackedEntityProperties">
    < ID名称=" ID"类型="的Int32"未保存价值=" 0">
      <生成器类="天然">< /发电机>
    < / ID>
    <属性名=" TrackedEntityID" />
    <属性名="名称" />
    <属性名=" CreatedDate" />
    <属性名=" ChangedDate" />
    <属性名="数据类型" />
    <属性名=" CurrentValue的" />
    <属性名=" RequestPropertyValueQuestion" />
    <属性名=" NullResponseIsAcceptable" />
    <属性名="复制" />
    <属性名="频率" />
    <属性名=" IsActive" />
    <属性名="&请将isDeleted QUOT; />
    <属性名=" LastUpdateTaskGenerated" />
    <属性名=" LastUpdateTaskCompleted" />
    <属性名=" LastUpdateTaskCancelled" />
  < /类>

  < SQL查询的名称=" usp_GetTrackedEntityPropertiesDueForUpdate" >
    <返回别名=" usp_GetTrackedEntityPropertiesDueForUpdate"类=" TrackedEntityProperty">

      <返回,属性名=" ID"列=" ID" />
      <返回,属性名=" TrackedEntityID"列=" TrackedEntityID" />
      <返回,属性名="名称"列="名称" />
      <返回,属性名=" CreatedDate"列=" CreatedDate" />
      <返回,属性名=" ChangedDate"列=" ChangedDate" />
      <返回,属性名="数据类型"列="数据类型" />
      <返回,属性名=" CurrentValue的"列=" CurrentValue的" />
      <返回,属性名=" RequestPropertyValueQuestion"列=" RequestPropertyValueQuestion" />
      <返回,属性名=" NullResponseIsAcceptable"列=" NullResponseIsAcceptable" />
      <返回,属性名="复制"列="复制" />
      <返回,属性名="频率"列="频率" />
      <返回,属性名=" IsActive"列=" IsActive" />
      <返回,属性名="&请将isDeleted QUOT;列="&请将isDeleted QUOT; />
      <返回,属性名=" LastUpdateTaskGenerated"列=" LastUpdateTaskGenerated" />
      <返回,属性名=" LastUpdateTaskCompleted"列=" LastUpdateTaskCompleted" />
      <返回,属性名=" LastUpdateTaskCancelled"列=" LastUpdateTaskCancelled" />

    < /回报>

    EXEC usp_GetTrackedEntityPropertiesDueForUpdate:TrackedEntityID

  < / SQL查询>

  < SQL查询的名称=" usp_SomeCustomSproc">
    <返回别名=" usp_SomeCustomSproc"类=" CustomObject">

      <返回,属性名=" ID"列=" ID" />
      <返回,属性名="名称"列="名称" />

    < /回报>

    EXEC usp_SomeCustomSproc:TrackedEntityID

  < / SQL查询>

< /休眠映射>

解决方案

你要找的是什么预测。首先你需要修改你的查询

 < SQL查询的名称=usp_SomeCustomSproc>
    <返回标量列=ID类型=的Int32/>
    <返回标量列=名称TYPE =字符串/>

    EXEC usp_SomeCustomSproc:TrackedEntityID

  < / SQL查询>
 

然后在code,你把它指定一个结果变压器。该Alias​​ToBeanTransformer将列别名,并将它们映射到对象的属性。

  session.GetNamedQuery(usp_SomeCustomSproc)
       .SetInt32(TrackedEntityID,15)
       .SetResultTransformer(Transformers.Alias​​ToBean&其中; CustomObject>())
       .LIST< CustomObject>()
 

Update - Edited config for readability in SO

Hi,

I've been learning NHibernate for a day or two but getting stuck on one point.

I need to be able to execute custom stored procedures and use NHibernate to map them back to domain classes.

I have this working for the scenario where the custom query maps back to a object that maps to a database table, as shown by many a nhibernate example (See first section below).

However in the config for the second section below, the query pulls only 2 columns from the target table. For this reason, I have created a custom object so that NHibernate has something to map the return values to. The custom object properties have the same name as the return columns from the custom procedure.

When I run my tests I get an exception like:

NHibernate.MappingException: No persister for: Proj.DataEntityTracker.Domain.Entities.CustomObject

So I guess the mapping under sql-query section is not enough for NHibernate to map the return values to the object properties.

So my question is - how do I set up a mapping for which there is no equivalent table in the database such that I can map the results of a stored procedure to that object?


<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="Proj.DataEntityTracker.Domain"
                   namespace="Proj.DataEntityTracker.Domain.Entities">

  <class name="TrackedEntityProperty" table="TrackedEntityProperties">
    <id name="ID" type="Int32" unsaved-value="0">
      <generator class="native"></generator>
    </id>
    <property name="TrackedEntityID" />
    <property name="Name" />
    <property name="CreatedDate" />
    <property name="ChangedDate" />
    <property name="DataType" />
    <property name="CurrentValue" />
    <property name="RequestPropertyValueQuestion" />
    <property name="NullResponseIsAcceptable" />
    <property name="Duplication" />
    <property name="Frequency" />
    <property name="IsActive" />
    <property name="IsDeleted" />
    <property name="LastUpdateTaskGenerated" />
    <property name="LastUpdateTaskCompleted" />
    <property name="LastUpdateTaskCancelled" />
  </class>

  <sql-query name="usp_GetTrackedEntityPropertiesDueForUpdate" >
    <return alias="usp_GetTrackedEntityPropertiesDueForUpdate" class="TrackedEntityProperty">

      <return-property name="ID" column="ID" />
      <return-property name="TrackedEntityID" column="TrackedEntityID" />
      <return-property name="Name" column="Name" />
      <return-property name="CreatedDate" column="CreatedDate" />
      <return-property name="ChangedDate" column="ChangedDate" />
      <return-property name="DataType" column="DataType" />
      <return-property name="CurrentValue" column="CurrentValue" />
      <return-property name="RequestPropertyValueQuestion" column="RequestPropertyValueQuestion" />
      <return-property name="NullResponseIsAcceptable" column="NullResponseIsAcceptable" />
      <return-property name="Duplication" column="Duplication" />
      <return-property name="Frequency" column="Frequency" />
      <return-property name="IsActive" column="IsActive" />
      <return-property name="IsDeleted" column="IsDeleted" />
      <return-property name="LastUpdateTaskGenerated" column="LastUpdateTaskGenerated" />
      <return-property name="LastUpdateTaskCompleted" column="LastUpdateTaskCompleted" />
      <return-property name="LastUpdateTaskCancelled" column="LastUpdateTaskCancelled" />

    </return>

    exec usp_GetTrackedEntityPropertiesDueForUpdate :TrackedEntityID

  </sql-query>

  <sql-query name="usp_SomeCustomSproc">
    <return alias="usp_SomeCustomSproc" class="CustomObject">

      <return-property name="ID" column="ID" />
      <return-property name="Name" column="Name" />

    </return>

    exec usp_SomeCustomSproc :TrackedEntityID

  </sql-query>

</hibernate-mapping>

解决方案

What you're looking for are projections. First of all you need to modify your query

  <sql-query name="usp_SomeCustomSproc">
    <return-scalar column="Id" type="Int32"/>
    <return-scalar column="Name" type="String"/>

    exec usp_SomeCustomSproc :TrackedEntityID

  </sql-query>

Then in the code where you call it you specify a result transformer. The AliasToBeanTransformer will take column aliases and map them to properties on the object.

session.GetNamedQuery("usp_SomeCustomSproc")
       .SetInt32("TrackedEntityID", 15)
       .SetResultTransformer(Transformers.AliasToBean<CustomObject>())
       .List<CustomObject>()

这篇关于NHibernate的 - 如何映射到一个没有表(自定义的SQL查询)类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆