Cascade =“all” not saving child entities

这一生的挚爱 提交于 2019-12-11 11:33:29

问题


I think the object model 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).

But it clearly isn't...

Can someone explain why PartyName isn't being saved with Party and what to do to fix?

Cheers,
Berryl

MAPPING

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

<discriminator column="Type" not-null="true" type="string" />

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

<subclass 
  name="Smack.Core.TestingSupport.NHibernate.TestableDomain.SomeDopeyDomainModel.Student, Smack.Core.TestingSupport" 
  discriminator-value="Student"
  >
  <property name="Number" />

  <many-to-one 
    class="Smack.Core.TestingSupport.NHibernate.TestableDomain.SomeDopeyDomainModel.Course, Smack.Core.TestingSupport" 
    foreign-key="Course_FK" 
    name="Course">
    <column name="CourseId" index="CourseIndex" />
  </many-to-one>
</subclass>

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

Failing Test (and output)

    [Test]
    public void CanSaveAndLoad_AllProperties()
    {
        var partyName = NameSeeds.DevName;
        partyName.Party = _party;
        Assert.That(_party.Names.First(), Is.EqualTo(partyName));


        using (var tx = _session.BeginTransaction())
        {
            _session.Save(_party);
            tx.Commit();
        }
        _session.Clear();

        Party foundParty;
        using (var tx = _session.BeginTransaction())
        {
            foundParty = _session.Get<Party>(_party.Id); *** <=== name s/b saved!!
            tx.Commit();
        }
        PartyName foundName = foundParty.Names.First();
        //found.Look();

        Assert.That(foundName, Is.EqualTo(partyName));
        Assert.That(foundName.Party, Is.Not.Null);
        Assert.That(foundName.TheRequiredName, Is.EqualTo(partyName.TheRequiredName));
        Assert.That(foundName.EverythingElse, Is.EqualTo(partyName.EverythingElse));
        Assert.That(foundName.ContextUsed, Is.EqualTo(partyName.ContextUsed));
        Assert.That(foundName.Salutation, Is.EqualTo(partyName.Salutation));
        Assert.That(foundName.EffectivePeriod, Is.EqualTo(partyName.EffectivePeriod));
    }


NHibernate: INSERT INTO Parties (Type, PartyId) VALUES ('Parties.Domain.Party', @p0);@p0 = 32768 [Type: Int32 (0)]
NHibernate: SELECT party0_.PartyId as PartyId2_0_, party0_.Number as Number2_0_, party0_.CourseId as CourseId2_0_, party0_.Type as Type2_0_ FROM Parties party0_ WHERE party0_.PartyId=@p0;@p0 = 32768 [Type: Int32 (0)]

回答1:


With the mapping of the Names <set> inverse=true, you will have to explicitly call session.Save(partyNameObject) on each member of the collection. If you are looking to have NHibernate automatically save the members of the set when the PartyObject is saved, you need to change the Names <set> inverse attribute to inverse=false. This tells Nhibernate that you want Party to control the relationship between Party and PartyName. You must also remember to add each partyNameObject to the Party.Names collection. Otherwise, they won't be saved when you call Session.Save(partyObject). Keep in mind that having the parent control the relationship may be handy, but if you happen to save the PartyObject without having Loaded the PartyNames collection, NHibernate will update their Party FK to Null. In this scenario with certain Cascade options set on the Names <set>, you might find Nhibernate Deleting them as well.



来源:https://stackoverflow.com/questions/6793659/cascade-all-not-saving-child-entities

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!