Как сопоставить сумку с составным ключом с помощью NHibernate - PullRequest
0 голосов
/ 14 октября 2011

Я пытаюсь отобразить два объекта с помощью NHibernate

Это мой первый объект "Asociado", составленный "Justificaciones", рядом с ним "Justificacion", который имеет составной ключ

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

это отображение, которое я сделал, но не работает

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

1 Ответ

1 голос
/ 17 октября 2011

Я думаю, что проблема в том, что столбец отображается дважды <id name="Id" column="CASOCIADO" /> и <column name="CASOCIADO"/>

Карта Justificacion как компонент

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

pro: - вам больше не нужен идентификатор на Justificacion con: - вы должны запросить Justificacion всегда над родителем Asociado, но это не должно быть проблемой

...