问题
Given the object model, mapping, and code below, I want to achieve the following:
On SAVE
of new parent Party objects, I'd like for the parent to persist child PartyNames as well. SO ideally I can just do:
var party = new Party();
party.AddPartyName(_developerName)
using(var tx = session.BeginTransaction){
_session.Save (party);
tx.Commit()
}
But my understanding from the last question I asked on this is that while this is doable with inverse="false", I really want to define the children set using inverse="true" is that after saving the Party I need to explicitly save each child:
foreach (var name in party.Names{ _session.Save(name)}
below is saying a Party and PartyName to have a many to one relatioship. An I think the cascade=all i the Party.hbm sshould be having NHib save the child PartyName(s).
On Retrieval
What I really want to do is just:
IList<Party> found;
using(var tx = _session.BeginTransaction())}
found = _session.QueryOver<T>().List());
tx.Commit()
}
I want the PartyNames to be hydrated, but they are not.
So, how can I improve my mapping / code to make the Save / Fetch happen the way I want to?
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>
Object model code
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;
}
}
来源:https://stackoverflow.com/questions/6797101/nhibernate-bidirectional-mapping