映射到同一表的两个类(一个只读)必须以正确的顺序排列吗? [英] Two classes mapped to same table(one readonly) must be in correct order?

查看:46
本文介绍了映射到同一表的两个类(一个只读)必须以正确的顺序排列吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑将这两个类映射到同一张表.一个是通过mutable ="false"只读的.

Consider these two classes mapped to the same table. One is readonly via mutable="false".

<class name="Funder" table="funder">
    <id name="id">
      <generator class="identity" />
    </id>
    <property name="funder_name" />
    <property name="contact_name" />
    <property name="addr_line_1" />
    <property name="addr_line_2" />
    <property name="addr_line_3" />
    <property name="city" />
    <many-to-one name="state" column="state_id" foreign-key="FK_funder_state_id" fetch="join" />
    <property name="zip_code" length="10" />
    <property name="phone_number" length="30" />

    <property name="create_dt" update="false" not-null="true" />
    <many-to-one name="create_by" column="create_by" not-null="true" update="false" foreign-key="FK_funder_create_by" fetch="join" />
    <property name="last_update_dt" insert="false" />
    <many-to-one name="last_update_by" insert="false" foreign-key="FK_funder_last_update_by" fetch="join" />

  </class>

  <class name="FunderSimple" table="funder" schema-action="none" mutable="false">
    <id name="id">
      <generator class="identity" />
    </id>
    <property name="funder_name" />
    <property name="contact_name" />
    <property name="phone_number" />
  </class>

如果我在Funder映射之前迁移了FunderSimple映射,我的架构将不能正确生成.如果我将其保留在上面,则可以使用.

If I move the FunderSimple mapping before the Funder mapping my schema does not generate correctly. If I leave it as is above, it works.

这是设计使然吗?似乎schema-action ="none"坚持使用table_name,以后到同一表的映射将不会生成该模式.

Is this by design? It seems as though the schema-action="none" sticks to the table_name and later mappings to the same table will not generate the schema.

之所以这样,是因为我有另一个名为Contract的类,该类具有对出纳者表的外键.但是,从合同对象进行引用时,我不需要所有的资助者列.

I'm doing it like this because I have another class named Contract which has a foreign key to the funder table. However, I don't need all the funder columns when referencing from the contract object.

<many-to-one name="funder_simple" column="funder_id"  foreign-key="FK_contract_funder_id" fetch="join" />

Funder不继承FunderSimple.

Funder does not inherit from FunderSimple.

我应该使用其他技术从外键表中仅获取列的子集吗?多对一设置外键的唯一方法吗?

Should I be using a different technique to fetch only a subset of columns from a foreign key table? Is many-to-one the only way to setup a foreign key?

使用2.1.0.4000版本

using version 2.1.0.4000

推荐答案

在这种情况下,我改用投影. 我从未将两种类型映射到同一张表(除非出于继承原因).

For such situations, I use projections instead. I've never mapped two types to the same table (unless for inheritance reasons).

所以,在这种情况下我要做的是:

So, what I do in such a situation is:

创建FunderSimple类,并将其导入,以便NHibernate知道:

create the FunderSimple class, and import it so that it is known by NHibernate:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
    <import class="MyNamespace.FunderSimple" />
</hibernate-mapping>

完成此操作后,可以使用ICriteria API在"Funder"类型上创建查询,但是可以指定希望NHibernate返回FunderSimple实例. 这样,NHibernate足够聪明以生成简化的SQL查询,该查询仅检索填充FunderSimple类的实例所需的列.

Once you've done this, you can create a query on your 'Funder' type, with the ICriteria API, but, you could specify that you would like NHibernate to return instances of FunderSimple. By doing so, NHibernate is smart enough to generate a simplified SQL query, that only retrieves the columns that are necessary to populate instances of the FunderSimple class.

这是这样完成的:

ICriteria crit = session.CreateCriteria (typeof(Funder));
// add some expressions ...
crit.Add ( ... );

// Now, set the projection, and specify that FunderSimple should be returned
crit.SetProjection (Projections.ProjectionList()
                         .Add (Projections.Property ("Id"), "Id")
                         .Add (Projections.Property ("funder_name"), "funder_name")
                         .Add (Projections.Property ("phone_number"), "phone_number"));

crit.SetResultTransformer (Transformers.AliasToBean (typeof(FunderSimple)));

crit.List <FunderSimple>();

这篇关于映射到同一表的两个类(一个只读)必须以正确的顺序排列吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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