如何使用NHibernate映射具有复合密钥的包

如何使用NHibernate映射具有复合密钥的包,nhibernate,composite-key,Nhibernate,Composite Key,我试图用NHibernate映射两个对象 这是我的第一个对象“Asociado”,由“justiveCaciones”组成,旁边是“justiveCacion”,它有一个组合键 public class Justificacion { private int _id; //(PK) private Asociado _asociado;(FK) public override bool Equals(object obj) { return

我试图用NHibernate映射两个对象

这是我的第一个对象“Asociado”,由“justiveCaciones”组成,旁边是“justiveCacion”,它有一个组合键

public class Justificacion
{
    private int _id; //(PK) 
    private Asociado _asociado;(FK)

    public override bool Equals(object obj)
    {

        return base.Equals(obj);

    }

    public override int GetHashCode()
    {
        return base.GetHashCode();
    }

    public override string ToString()
    {
        return base.ToString();
    }

    public virtual int Id
    {
        get { return _id; }
        set { _id = value; }
    }

    public virtual Asociado Asociado
    {
        get { return _asociado; }
        set { _asociado = value; }
    }
}

public class Asociado
{
    private int _id;
    private IList<Justificacion> _justificaciones;

    public virtual int Id
    {
        get { return this._id; }
        set { this._id = value; }
    }

    public virtual IList<Justificacion> Justificaciones
    {
        get { return _justificaciones; }
        set { _justificaciones = value; }
    }
}
公共类证明
{
私有整数_id;//(主键)
私人协会(FK)
公共覆盖布尔等于(对象对象对象)
{
返回基数等于(obj);
}
公共覆盖int GetHashCode()
{
返回base.GetHashCode();
}
公共重写字符串ToString()
{
返回base.ToString();
}
公共虚拟整数Id
{
获取{return\u id;}
设置{u id=value;}
}
公共虚拟Asociado Asociado
{
获取{return\u asociado;}
设置{u asociado=value;}
}
}
公共阶级协会
{
私人内部id;
私人律师事务所;
公共虚拟整数Id
{
获取{返回此。\u id;}
设置{this.\u id=value;}
}
公共虚拟图书馆
{
获取{return}
设置{u justificaciones=value;}
}
}
这是我做的映射,但不起作用

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="Trascend.Bolet.ObjetosComunes"
                   namespace="Trascend.Bolet.ObjetosComunes.Entidades">

  <class name="Justificacion" table="FAC_ASO_JUST">
    <composite-id>
      <key-property name="Id" column="CCARTA" type="int"></key-property>
      <key-property name="Asociado" column="CASOCIADO" type="int"></key-property>
    </composite-id>

    <many-to-one name="Asociado" class="Asociado">
      <column name="CASOCIADO"/>
    </many-to-one>


  </class>

</hibernate-mapping>


<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="Trascend.Bolet.ObjetosComunes"
                   namespace="Trascend.Bolet.ObjetosComunes.Entidades">

  <class name="Asociado" table="FAC_ASOCIADOS">
    <id name="Id" column="CASOCIADO" />

  <bag name="Justificaciones"
          fetch="join"
          inverse="true"
          cascade="save-update">
    <key>
      <column name="CCARTA"/>
      <column name="CASOCIADO"/>
    </key>
    <one-to-many class="Justificacion"/>
  </bag>

</hibernate-mapping>

我认为问题在于列映射了两次

作为组件的映射调整

<bag name="Justificaciones"
     fetch="join"
     inverse="true"
     cascade="save-update">
  <key column ="CASOCIADO"/>
  <composite-element class="Justificacion">
    <parent name="Asociado"/>
  </composite-element>
</bag>

赞成:-您不再需要
justification
上的id了 缺点:-您必须始终通过父级
Asociado
查询
justification
,但这应该不是问题

使用“base.Equals”和“base.GetHashCode”是不够的,NHibernate要求您出于某种原因覆盖复合ID类型。这是因为您需要提供一个基于ID值的哈希代码和等式。我通常将复合ID设置为单独的类型(通常是结构)。