如何将NHibernate实体映射到查询 [英] How to map an NHibernate entity to a query

查看:97
本文介绍了如何将NHibernate实体映射到查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了许多这样的示例,据我所知,我的HBM文件遵循相同的模式,但是没有用.首先,文件:

I've seen a number of examples of this, and as far as I can tell my HBM file follows the same pattern, but it's not working. First, the file:

<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping default-cascade="save-update" xmlns="urn:nhibernate-mapping-2.2">
  <class name="ThinAir" mutable="false" lazy="true" >
    <id name="JobId">
      <generator class="native" />
    </id>
    <property name="UserLogin"/>
    <property name="UserEmail"/>
    <property name="DateProcessed"/>
    <loader query-ref="myquery"/>
  </class>
  <sql-query name="myquery">
    <return class="ThinAir">
      <return-property name="JobID" column="JobId"/>
      <return-property name="userLogin" column="UserLogin"/>
      <return-property name="DateProcessed" column="DateProcessed"/>
      <return-property name="userEmail" column="UserEmail"/>
    </return>
    <![CDATA[
    SELECT DISTINCT JobID,
    userLogin,
    DateProcessed,
    useremail
    FROM         dbo.someothertable
    ]]>
  </sql-query>
</hibernate-mapping>

"myquery"本身就可以工作.也就是说,如果我打电话

"myquery" in-and-of-itself, works. That is to say, if I call

var x = session.GetNamedQuery("myquery").List();

我得到了 ThinAir 对象的正确列表.

I get a correct List of ThinAir objects.

但是,当我尝试获取这样的 ThinAirs 列表时:

But, when I try to get a list of ThinAirs like this:

var submissions = session.CreateCriteria<ThinAir>().List<ThinAir>();

我知道

测试方法testThinAir抛出异常: NHibernate.Exceptions.GenericADOException:无法执行查询 [选择this_.JobId作为JobId16_0_,this_.UserLogin作为UserLogin16_0_,this_.UserEmail作为UserEmail16_0_,this_.Date处理为DateProc4_16_0_ FROM ThinAir this_]

Test method testThinAir threw exception: NHibernate.Exceptions.GenericADOException: could not execute query [ SELECT this_.JobId as JobId16_0_, this_.UserLogin as UserLogin16_0_, this_.UserEmail as UserEmail16_0_, this_.DateProcessed as DateProc4_16_0_ FROM ThinAir this_ ]

我对此现象的解释是NH忽略了我的<loader>标记,因此尝试从基础表中加载数据,默认情况下,该表假定其名为 ThinAir ,因为那是名称的实体类,只有 任何 ThinAir 表,因此出现错误消息.

My interpretation of this phenomenon is that NH is ignoring my <loader> tag and so trying to load the data from the underlying table, which by default it assumes to be named ThinAir because that's the name of the entity class, only there isn't any ThinAir table, hence the error message.

这种解释正确吗?无论如何,我在做什么错了,我该怎么做对呢?

Is that interpretation correct? And in any case, what am I doing wrong and how can I do it right?

谢谢.

迈克尔

推荐答案

一种实现此目的的方法是将映射从 query 移到subselect:

One way how to achieve this, would be to move the mapping from a query into the subselect:

<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping default-cascade="save-update" xmlns="urn:nhibernate-mapping-2.2">
  <class name="ThinAir" mutable="false" lazy="true" >

  <subselect>
    <![CDATA[
    SELECT DISTINCT JobID,
    userLogin,
    DateProcessed,
    useremail
    FROM         dbo.someothertable
    ]]>
  </subselect>

  ... // rest of the mapping

这篇关于如何将NHibernate实体映射到查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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