Nhibernate 映射到同一个表的两个类(一个只读)的顺序是否正确?

Nhibernate 映射到同一个表的两个类(一个只读)的顺序是否正确?,nhibernate,nhibernate-mapping,Nhibernate,Nhibernate Mapping,考虑映射到同一个表的这两个类。一个是通过mutable=“false”只读的 如果我在Funder映射之前移动FunderSimple映射,则我的模式不会正确生成。如果我让它保持上面的状态,它就会工作 这是故意的吗?似乎schema action=“none”会固定在表的名称上,以后映射到同一个表将不会生成模式 我这样做是因为我有另一个名为Contract的类,它有一个funder表的外键。但是,当从合同对象引用时,我不需要所有的funder列 <many-to-one name="f

考虑映射到同一个表的这两个类。一个是通过mutable=“false”只读的


如果我在Funder映射之前移动FunderSimple映射,则我的模式不会正确生成。如果我让它保持上面的状态,它就会工作

这是故意的吗?似乎schema action=“none”会固定在表的名称上,以后映射到同一个表将不会生成模式

我这样做是因为我有另一个名为Contract的类,它有一个funder表的外键。但是,当从合同对象引用时,我不需要所有的funder列

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

出资人不从FunderSimple继承

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


对于这种情况,我使用2.1.0.4000版,而不是投影。 我从未将两种类型映射到同一个表(除非出于继承原因)

因此,在这种情况下,我要做的是:

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

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

完成此操作后,可以使用ICriteria API对“Funder”类型创建查询,但可以指定希望NHibernate返回FunderSimple的实例。 通过这样做,NHibernate足够聪明,可以生成一个简化的SQL查询,该查询只检索填充FunderSimple类实例所需的列

这是这样做的:

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>();
ICriteria crit=session.CreateCriteria(类型(出资人));
//添加一些表达式。。。
标准添加(…);
//现在,设置投影,并指定应该返回FunderSimple
crit.SetProjection(Projections.ProjectionList()
.Add(Projections.Property(“Id”),“Id”)
.Add(Projections.Property(“出资人姓名”),“出资人姓名”)
.添加(Projections.Property(“电话号码”),“电话号码”);
crit.SetResultTransformer(Transformers.AliasToBean(typeof(FunderSimple));
crit.List();

很有趣……但是,从合同映射的角度来看,这是如何工作的呢。我是否会将多对一更改为重Funder对象,并以某种方式将其映射为仅加载FunderSimple?合同应该与“Funder”有关系,因为“Funder”是您的实体。我认为FunderSimple只是一种帮助器/容器,用于显示“资助者”列表,而不需要完整的资助者实体。在检索合同实例时,为什么要检索“FunderSimple”实例?这样我就不必将所有出资人不需要的外键(如create_by和last_update_by键)都加入Staff表中……这反过来又会导致Staff表中的两个键的更多联接。就合同而言,FunderSimple字段是我唯一需要的字段。我怀疑每次获取合同时加入所有这些不需要的表会影响性能。但是,我只是希望有一种方法可以绕过这个问题,只查询我需要的东西。我在上面做的方法就像我想要的那样工作,但我不知道它是否得到支持,因为它的映射顺序非常脆弱。它确实很脆弱,我不会使用它。。。难道你不能对那些额外的实体使用延迟加载(Create\u By,LastUpdate\u By)?(我还跟踪审计信息,但我只跟踪表本身中的用户名;我不加入某种“Staff”表)。
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>();