【问题标题】:Subclass table mapping in nHibernatenHibernate 中的子类表映射
【发布时间】:2012-09-14 21:13:24
【问题描述】:

首先,对文字墙感到抱歉。

我有以下架构。请注意,User_Contact 可能在 ContactID 中包含 null。

在类级别,然后以以下方式实现

public abstract class User : EntityBase<Guid>, IAggregateRoot
{
    public User()
    {

    }

    public virtual string FirstName { get; set; }
    public virtual string LastName { get; set; }

    protected abstract override void Validate();
}

public class SiteUser : User
{
    public SiteUser() { }

    public virtual Guid ApplicationId { get; set; }
    public virtual string UserName { get; set; }
    public virtual string LoweredUserName { get; set; }
    public virtual string MobileAlias { get; set; }
    public virtual bool IsAnonymous { get; set; }
    public virtual DateTime LastActivityDate { get; set; }

    protected override void Validate()
    { 
        if (this.ApplicationId == Guid.Empty)
            base.AddBrokenRule(UserBusinessRules.ApplicationIdRequired);

        if (this.UserName.IsNullOrEmpty())
            base.AddBrokenRule(UserBusinessRules.UserNameRequired);

        if (this.LoweredUserName.IsNullOrEmpty())
            base.AddBrokenRule(UserBusinessRules.LoweredUserNameRequired);

        if (this.LastActivityDate == DateTime.MinValue)
            base.AddBrokenRule(UserBusinessRules.LastActivityDateRequired);

    }
}

public class SiteContact : SiteUser
{

    public SiteContact() 
    {
    }


    public virtual int ExternalID { get; set; }
    //All the rest...

    protected override void Validate()
    { 
        //validate
    }
}

所以基本上这个想法是每个SiteContact 都是SiteUser,但不是每个SiteUser 都是SiteContact

我完全陷入困境的是如何在 nHibernate 中映射这种关系。我似乎能够检索到SiteUser,但无法检索到SiteContact,原因很清楚。我实现的映射导致执行以下 sql。

SELECT this_.userid             AS UserId18_0_, 
       this_.firstname          AS FirstName18_0_, 
       this_.lastname           AS LastName18_0_, 
       //Blah blah.....
FROM   user_contact this_ 
       INNER JOIN contact this_1_ 
               ON this_.userid = this_1_.contactid  //Here is the error this should be ContactID = ContactID
       INNER JOIN aspnet_users this_2_ 
               ON this_.userid = this_2_.userid 
WHERE  ( CASE 
           WHEN this_.contactid IS NOT NULL THEN 1 
           ELSE 0 
         END ) = '1' 

但我终其一生都无法弄清楚如何在我的 hbm 映射文件中解决此问题。这是为简单起见省略了一些字段的文件。

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="MyProject.Namespaces.Model.Entities" assembly="MyProject.Namespaces.Model">


  <class name="User" table="User_Contact" lazy="true" abstract="true">

    <id name="ID" column="UserId" type="guid">
      <generator class="guid" />
    </id>

    <discriminator column="ContactID" formula="(CASE WHEN ContactID IS NOT NULL THEN 1 ELSE 0 END)" />

    <property name="FirstName">
      <column name="FirstName" sql-type="nvarchar(500)" not-null="true" />
    </property>

    <property name="LastName">
      <column name="LastName" sql-type="nvarchar(500)" not-null="true" />
    </property>

    <subclass name="SiteContact" discriminator-value="1">
      <join table="Contact">
        <key column="ContactID" /> //I assume the problem is here. I have tried adding foreign-key="ContactID" to no success

        <property name="ExternalID" insert="false" update="false">
          <column name="ExternalID" sql-type="int" not-null="true" />
        </property>

        //The rest of the mapped columns

      </join>
      <join table="aspnet_Users">
        <key column="UserId" />

        <property name="ApplicationId">
          <column name="ApplicationId" sql-type="guid" not-null="true" />
        </property>

        <property name="UserName">
          <column name="UserName" sql-type="nvarchar(256)" not-null="true" />
        </property>

        <property name="LoweredUserName">
          <column name="LoweredUserName" sql-type="nvarchar(256)" not-null="true" />
        </property>

        <property name="MobileAlias">
          <column name="MobileAlias" sql-type="nvarchar(16)" not-null="false" />
        </property>

        <property name="IsAnonymous">
          <column name="IsAnonymous" sql-type="bit" not-null="true" />
        </property>

        <property name="LastActivityDate">
          <column name="LastActivityDate" sql-type="datetime" not-null="true" />
        </property>

      </join>
    </subclass>

    <subclass name="SiteUser" discriminator-value="0">
      <join table="aspnet_Users">
        <key column="UserId" />

        //blah blah blah

      </join>
    </subclass>

  </class>

</hibernate-mapping>

希望有人能告诉我我是否在这个映射的正确轨道上,也许可以提供一些指导?

【问题讨论】:

    标签: c# nhibernate nhibernate-mapping subclass


    【解决方案1】:

    这似乎是一种非常复杂的映射方式。我不确定您希望在这种情况下使用继承获得什么好处,我建议改为使用组合(即,每个表都有一个类并映射关系)。

    话虽如此,你大概有你的理由,所以我建议以下来实现我认为你想要的:

    1. 将您的 SiteUser 类映射为基类,并使用从 User_Contact 到 aspnet_Users 的连接(因此不要将 User 作为映射中的基类)
    2. 然后将您的 SiteContact 映射为使用连接子类映射而不是子类的子类,因此您不需要使用鉴别器。

    【讨论】:

    • 其实你的思路是正确的,我们确实有我们想要采取这种方法的原因,但最终决定这正是你所说的“太复杂了”。因此,我们最终重组了架构。话虽如此,我仍然对如何实现这种映射感到好奇,看来您的解决方案会奏效。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-29
    相关资源
    最近更新 更多