级联=“所有".不保存子实体 [英] Cascade ="all" not saving child entities

查看:94
本文介绍了级联=“所有".不保存子实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

认为下面的对象模型是说Party和PartyName具有多对一关系.我认为,Particle.hbm中的cascade = all应该让NHib保存子PartyName.

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

但这显然不是...

有人可以解释为什么PartyName不保存PartyName以及如何解决吗?

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

干杯,
贝里

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

    [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)]

推荐答案

使用名称<set> inverse=true的映射,您将必须在集合的每个成员上显式调用session.Save(partyNameObject).如果要在保存PartyObject时让NHibernate自动保存集合的成员,则需要将Names <set>反向属性更改为inverse=false.这告诉Nhibernate您希望Party控制Party和PartyName之间的关系.您还必须记住将每个partyNameObject添加到Party.Names集合中.否则,当您呼叫Session.Save(partyObject)时将不会保存它们.请记住,让父级控制关系可能很方便,但是如果您碰巧保存PartyObject而未加载PartyNames集合,则NHibernate会将其Party FK更新为Null.在这种情况下,在名称<set>上设置了某些Cascade选项,您可能还会发现Nhibernate删除它们.

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.

这篇关于级联=“所有".不保存子实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆