【问题标题】:Nhibernate composite key questionNhibernate复合键问题
【发布时间】:2011-03-14 21:18:38
【问题描述】:

我有一个名为 person_skills 的表,如下所示:

person_id、skill_type_id、base_score、misc_score

有一个查找表,其中包含 id、skill_types 的名称。

现在棘手的是我有一个用于 person_id,skill_type_id 的复合键。由于一个人可能有 5 种技能,因此该表中会有很多条目。

目前我有这样的课程:

public class skill 
{
    int BaseScore {get;set;}
    int MiscScore {get;set;}
}

然后我有一个类来包含所有这些,如下所示:

public class person_skills
{
    int person_id {get;set;}
    IDictionary<skill_type, skill> skills {get;set;}
}

现在我不确定这是否是处理这种关系的最佳方式,最终我需要能够为人们提供技能链接,一个人对多种技能。

我正在考虑只是放入一个自动增量 id 列并将其用作 PK,但这似乎并不理想。如果需要,我可以更改模型和数据库,但由于这是在页面的 ajax 部分中使用的,我需要能够更改技能,然后将它们更新到数据库中。

【问题讨论】:

    标签: nhibernate composite-key


    【解决方案1】:

    我没有找到实际的问题,但无论如何我都会回答。 :)

    您不需要 person_skills 表的代理键。由 person_id 和 Skill_type_id 组成的复合键应该足够了。我相信以下类和映射反映了您在这里想要完成的工作。

    类:

    public class Person
    {
        public virtual int PersonId { get; set; }
        public virtual String Name { get; set; }
        public virtual IList<PersonSkills> Skills { get; set; }
    }
    
    public class SkillType
    {
        public virtual int SkillTypeId { get; set; }
        public virtual String SkillName { get; set; }
        public virtual IList<PersonSkills> Persons { get; set; }       
    }
    
    public class PersonSkills
    {
        public virtual int PersonId { get; set; }
        public virtual int SkillTypeId { get; set; }
        public virtual int BaseScore { get; set; }
        public virtual int MiscScore { get; set; }
    
        public override bool Equals(object obj)
        {
            if (ReferenceEquals(this, obj))
            {
                return true;
            }
    
            if (obj == null || !(obj is PersonSkills))
            {
                return false;
            }
            PersonSkills o = obj as PersonSkills;
    
            return (this.PersonId == o.PersonId
                && this.SkillTypeId == o.SkillTypeId);
        }
    
        public override int GetHashCode()
        {
            int hash = 13;
            hash = hash + this.PersonId.GetHashCode();
            hash = hash + this.SkillTypeId.GetHashCode();
            return hash;
        }
    }
    

    映射:(FluentNhibernate)

    public class PersonMap : ClassMap<Person>
    {
        public PersonMap()
        {
            Id(x => x.PersonId);
            Map(x => x.Name);
            HasMany(x => x.Skills)
                .KeyColumn("PersonId")
                .Cascade.All();
        }
    }
    
    public class SkillTypeMap : ClassMap<SkillType>
    {
        public SkillTypeMap()
        {
            Id(x => x.SkillTypeId);
            Map(x => x.SkillName);
            HasMany(x => x.Persons)
                .KeyColumn("SkillTypeId")
              .Cascade.All();
        }
    }
    
    public class PersonSkillsMap : ClassMap<PersonSkills>
    {
        public PersonSkillsMap()
        {
            CompositeId()
                .KeyProperty(x => x.PersonId)
                .KeyProperty(x => x.SkillTypeId);
            Map(x => x.BaseScore);
            Map(x => x.MiscScore);
        }
    }
    

    映射(hbm,由 FluentNHibernate 生成 - 我删除了不需要的输出):

    <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" >
      <class xmlns="urn:nhibernate-mapping-2.2" name="Person" table="Person">
        <id name="PersonId" type="int">
          <column name="PersonId" />
          <generator class="identity" />
        </id>
        <bag cascade="all" name="Skills" mutable="true">
          <key>
            <column name="PersonId" />
          </key>
          <one-to-many class="PersonSkills" />
        </bag>
        <property name="Name" type="String">
          <column name="Name" />
        </property>
      </class>
    </hibernate-mapping>
    
    <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" >
      <class xmlns="urn:nhibernate-mapping-2.2" name="SkillType" table="SkillType">
        <id name="SkillTypeId" type="int">
          <column name="SkillTypeId" />
          <generator class="identity" />
        </id>
        <bag cascade="all" name="Persons">
          <key>
            <column name="SkillTypeId" />
          </key>
          <one-to-many class="PersonSkills" />
        </bag>
        <property name="SkillName" type="String">
          <column name="SkillName" />
        </property>
      </class>
    </hibernate-mapping>
    
    <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" >
      <class xmlns="urn:nhibernate-mapping-2.2" name="PersonSkills" table="PersonSkills">
        <composite-id mapped="false" unsaved-value="undefined">
          <key-property name="PersonId" type="int">
            <column name="PersonId" />
          </key-property>
          <key-property name="SkillTypeId" type="int">
            <column name="SkillTypeId" />
          </key-property>
        </composite-id>
        <property name="BaseScore" type="int">
          <column name="BaseScore" />
        </property>
        <property name="MiscScore" type="int">
          <column name="MiscScore" />
        </property>
      </class>
    </hibernate-mapping>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多