nhibernate双向映射

时间:2011-07-22 23:19:22

标签: nhibernate nhibernate-mapping inverse nhibernate-cascade

鉴于下面的对象模型,映射和代码,我想实现以下目标:

在SAVE

新的父Party对象,我希望父母也能保留子PartyNames。理想情况下,我可以做到:

var party = new Party();
party.AddPartyName(_developerName)
using(var tx = session.BeginTransaction){
    _session.Save (party);
    tx.Commit()
}

但是我对last question I asked on this的理解是,虽然这对于inverse =“false”是可行的,但我真的想用inverse =“true”定义子集是在保存Party后我需要明确的保存每个孩子:

        foreach (var name in party.Names{ _session.Save(name)}

下面说的是Party和PartyName有多对一的关系。我认为cascade =所有我的Party.hbm应该让NHib保存孩子PartyName(s)。

On Retrieval

我真正想做的只是:

IList<Party> found;
using(var tx = _session.BeginTransaction())}
    found = _session.QueryOver<T>().List());
    tx.Commit()
}

我希望PartyNames能够保湿,但它们不是。

那么,我如何改进我的映射/代码以使Save / Fetch以我想要的方式发生?

enter image description here

MAPPING

<class name="Party" table="Parties">
<id name="Id">
  <column name="PartyId" />
  <generator class="hilo" />
</id>

...

<set access="field.camelcase-underscore" cascade="all" inverse="true" name="Names">
  <key foreign-key="Party_PartyName_FK">
    <column name="PartyNameId" />
  </key>
  <one-to-many class="Parties.Domain.Names.PartyName, Parties.Domain" />
</set>

                            

<many-to-one access="field.camelcase-underscore" class="Parties.Domain.Party" foreign-key="Party_FK" name="Party">
  <column name="PartyId" index="PartyIndex" not-null="true"/>
</many-to-one>
<property name="TheRequiredName" not-null="true" length="50"/>
<property name="EverythingElse" />
<property name="ContextUsed" length="50"/>
<property name="Salutation" length="20"/>
<property name="EffectivePeriod" type="Smack.Core.Data.NHibernate.UserTypes.DateRangeUserType, Smack.Core.Data">
  <column name="EffectiveStart"/>
  <column name="EffectiveEnd"/>
</property>

对象模型代码

public class Party : Entity
{
    ....

    #region Names

    /// <summary>One or more optional names.</summary>
    public virtual IEnumerable<PartyName> Names { get { return _InternalCollection_Names; } }

    protected internal virtual ICollection<PartyName> _InternalCollection_Names
    {
        get { return _names ?? (_names = new HashSet<PartyName>()); }
        set { _names = value; }
    }
    private ICollection<PartyName> _names;

    public virtual void AddPartyName(PartyName partyName)
    {
        if (partyName == null) throw new ArgumentNullException("partyName");

        // maintain association with PartyName, which will in turn add to our collection of names
        partyName.Party = this;
    }

    public virtual void RemovePartyName(PartyName partyName)
    {
        if (partyName == null) throw new ArgumentNullException("partyName");

        // maintain association with PartyName, which will in turn remove from our collection of names
        partyName.Party = null;
    }

    #endregion
}

public class PartyName : Entity, IEquatable<PersonName>
{

    /// <summary>Bi-directional relationship with the collection of partyNames kept by Party.</summary>
    [DomainSignature]
    public virtual Party Party
    {
        get { return _party; }
        set
        {
            if (_party != null)
            {
                // disassociate existing relationship
                _party._InternalCollection_Names.Remove(this);
            }
            _party = value;

            if (value != null)
            {
                //make the association
                _party._InternalCollection_Names.Add(this);
            }
        }
    }
    private Party _party;

}

}

0 个答案:

没有答案