【问题标题】:nhibernate fluent mapping lazy load derived classesnhibernate fluent映射延迟加载派生类
【发布时间】:2012-04-29 12:54:23
【问题描述】:

是否可以在 nhibernate/fluent 中为每个具体映射获取一个表,以发出数据库语句来确定类的类型,然后发出另一个语句来获取详细信息,而不是在一个巨大的左连接 uber- 中加入所有子类表声明。

换句话说,子类详细信息可以“延迟”加载。

我有一个使用流利的 nhibernate 和 ClassMap/SubClassMap 映射类映射的类层次结构。每个派生类(无论如何大多数)都有自己的表。所以它是每个具体类的一个表。

我没有指定鉴别器值,因为它们在所有子类都包含在同一个表中时使用。然而,我在基类表中有一个整数值,指示它是哪种类型。 nhibernate 是否能够使用此数据并为派生类数据发出延迟加载。鉴别器值是数据库中的整数,对应于基类中的枚举。

例子。

public sealed class PartyMap : ClassMap<Party>
{
  public PartyMap()
  {
     Table("Party");
     // I thought this might do it, but it doesn't :-(
     Polymorphism.Explicit();
     Id(x => x.Id).Column("PartyId");
     Map(x => x.PartyType).CustomType<PartyType>().Column("PartyTypeId");
     Map( ...
     // if I specify this, nhibernate expects all the fields
     // to be in the base class table, above Table(...) is the only one used.
     // DiscriminateSubClassesOnColumn<int>("PartyTypeId", 0).AlwaysSelectWithValue();
   }
}

public class PersonMap : SubclassMap<Person>
{
    public PersonMap()
    {
        Table("Person");
        KeyColumn("PartyId");   
        // if I specify this, nhibernate expects all the fields
        // to be in the base class table, above Table(...) is ignored.      
        // DiscriminatorValue((int) PartyType.Person);
     }
}

【问题讨论】:

    标签: c# nhibernate fluent table-per-subclass


    【解决方案1】:

    简短回答:不,这是不可能的。 IIRC,即使您使用鉴别器,行为也完全相同。

    如果需要考虑太多的连接(因为您有很多继承类),您应该考虑使用Table per class hierarchyTable per sublass with discriminator(如果大多数类具有类似的属性集,其中只有少数完全不同)

    【讨论】:

    • 谢谢,我自己也得出了这个结论。我认为使用正确指定的索引应该没问题。我习惯于手工编写 sql,所以这些连接效率低下让我担心。我可能应该放松一下,让技术发挥作用,并在它们真正出现问题时担心效率低下。
    • 只要有分析数据支持,对代码效率有顾虑也不错。无论如何,ORM 都是有漏洞的抽象。但是您的方法是正确的 IMO。
    猜你喜欢
    • 1970-01-01
    • 2013-12-10
    • 1970-01-01
    • 1970-01-01
    • 2011-02-05
    • 2011-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多