【问题标题】:NHibernate mapping of one class containing 2 references to the same entity一个类的 NHibernate 映射,其中包含对同一实体的 2 个引用
【发布时间】:2010-03-03 20:46:42
【问题描述】:

我有一个 person 类,然后有一个 family 类,其中我有一个属性父亲和一个 Person 类型的属性 Mother。

我有一个 Person 的数据库表和一个包含 FamilyId、FatherId、MotherId 的 Family 表,其中 FatherId 和 MotherId 是 Person 表中 PersonId 的外键。

您将如何在 NHibernate 中映射它?

【问题讨论】:

    标签: nhibernate nhibernate-mapping


    【解决方案1】:

    此映射表示您的表。

    public class Family
    {
        public virtual int Id { get; set; }
        public virtual Person Mother { get; set; }
        public virtual Person Father { get; set; }
    }
    
    <class name="Family">
        <id name="Id" column="FamilyId">
            <generator class="native" />
        </id>
        <many-to-one name="Mother" column="MotherId" />
        <many-to-one name="Father" column="FatherId" />
    </class>
    

    【讨论】:

      【解决方案2】:

      也许您有充分的理由在给定的上下文中进行设计。

      理论上,采用更松散耦合、更灵活、基于角色的设计通常会更好。

      我的意思是一个人是一个«党»,父亲和母亲是一个人可以扮演的«角色»(«党»的其他角色可能是员工、客户、朋友等) .

      我个人喜欢 Peter Coad 的 DNC 模式,以解决这个特殊的设计问题。一篇文章可以在这里找到:http://edn.embarcadero.com/article/32543

      几年前,我在一个大型 ERP 系统上工作,我在 C# + NHibernate 环境中引入了 DNC,所以我知道它在实践中也可以工作 ;-)

      有关角色模式/原型的深入分析,请参阅本书Enterprise patterns and MDA: building better software with archetype patterns and UML

      还有正在进行的研究以在语言级别解决此问题,而不是使用称为 DCI 的模式:http://www.artima.com/articles/dci_visionP.html

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-07-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-06-06
        • 2014-10-01
        • 2011-07-18
        相关资源
        最近更新 更多