Nhibernate复合关键问题 [英] Nhibernate composite key question

查看:73
本文介绍了Nhibernate复合关键问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a table called person_skills like so:

person_id,skill_type_id,base_score,misc_score

person_id, skill_type_id, base_score, misc_score

有一个查找表,其中包含ID,技能类型的名称.

There is a lookup table that contains id, name for skill_types.

现在棘手的是,我有一个person_id,skill_type_id的复合键.该表中将有很多条目,因为一个人可能具有5种技能.

Now the tricky thing is that I have a composite key for person_id, skill_type_id. There will be many entries within this table as a person may have 5 skills.

目前我有一个像这样的课程:

Currently I have got a class like so:

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

然后我有一个包含所有此类的类,如下所示:

Then I have a class to contain all this like below:

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

现在我不确定这是否是处理这种关系的最佳方法,最终我需要使人们能够链接到技能,而一个人却会技能很多.

Now im not sure if this is the best way to handle this relationship, ultimately I need to be able to give people a link to skills, there is one person to many skills.

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

I was thinking about just putting in an auto incrememnt id column and use that as the PK, but it doesn't seem ideal. I can change the models and the DB if required, but as this is used within an ajax part of a page I need to be able to change the skills and then update them into the database.

推荐答案

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

I did not find an actual question but I'll answer anyway. :)

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

You do not need a surrogate key for the person_skills table. Your composite key, consisting of person_id and skill_type_id, should be sufficient. I believe the following classes and mappings reflect what you are trying to accomplish here.

课程:

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)

Mappings: (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生成-我删除了不需要的输出):

Mappings (hbm, generated by FluentNHibernate - I removed output that is not required):

<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>

这篇关于Nhibernate复合关键问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆