级联=“全部”不保存子实体

发布于 2024-11-25 21:14:47 字数 3573 浏览 2 评论 0原文

我认为下面的对象模型表示 Party 和 PartyName 具有多对一的关系。我认为,Cascade=all i Party.hbm 应该让 NHib 保存子 PartyName。

但显然不是......

有人可以解释为什么 PartyName 没有与 Party 一起保存以及如何修复吗?

干杯,
Berryl

在此处输入图像描述

映射

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

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

enter image description here

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

橘虞初梦 2024-12-02 21:14:47

通过名称 inverse=true 的映射,您必须对集合的每个成员显式调用 session.Save(partyNameObject)。如果您希望 NHibernate 在保存 PartyObject 时自动保存集合的成员,则需要将 Names inverse 属性更改为 inverse=false 。这告诉 Nhibernate 您希望 Party 控制 Party 和 PartyName 之间的关系。您还必须记住将每个 partyNameObject 添加到 Party.Names 集合中。否则,当您调用 Session.Save(partyObject) 时,它们将不会被保存。请记住,让父级控制关系可能会很方便,但如果您碰巧在没有加载 PartyNames 集合的情况下保存 PartyObject,NHibernate 会将其 Party FK 更新为 Null。在这种情况下,如果在名称 上设置了某些 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文