开发者

How to map a bag with composite key with NHibernate

开发者 https://www.devze.com 2023-04-13 06:13 出处:网络
Im trying to map two objects with NHibernate This is my first object \"Asociado\" composed by \"Justificaciones\", next to it is \"Justificacion\" which has a composed key

Im trying to map two objects with NHibernate

This is my first object "Asociado" composed by "Justificaciones", next to it is "Justificacion" which has a composed key

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

this is the mapping i did, but is not working

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


&l开发者_Python百科t;?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>


i think the problem is the column mapped twice <id name="Id" column="CASOCIADO" /> and <column name="CASOCIADO"/>

Map Justificacion as Component

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

pro: - you dont need the id on Justificacion anymore con: - you have to Query for Justificacion always over the parent Asociado but this shouldn't be a problem

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号