NHibernate一对多不拯救孩子

发布于 2024-12-28 03:43:18 字数 1775 浏览 3 评论 0原文

class A {..} 
class ContainedA { property of type A and some extra information }
class B : A { collection of type ContainedA  }

正如您所知,这个想法是能够在多个 B 中包含 A 的单个实例,B 本身也是 A 类型,只是它可以保存其他 A 的

A 和 B 的映射

  <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
    namespace="REDACTED"
    assembly= "REDACTED">

    <class name="A" table="A" discriminator-value="1">
        <id name="Id" column="Id" type="int" access="field.camelcase-underscore">
            <generator class="identity" />
        </id>

        <discriminator column="Type" type="int"/>

        <subclass name="B" extends="A" discriminator-value="2">
            <bag name="ContainedAs" cascade="all">
                <key column="AInternalId"/>
                <one-to-many class="ContainedA"/>
            </bag>
        </subclass>

    </class>
</hibernate-mapping>

ContainedA 的映射

    <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
    namespace="REDACTED"
    assembly= "REDACTED">

    <class name="ContainedA" table="ContainedA">
        <id name="Id" type="int" access="field.camelcase-underscore" column="Id">
            <generator class="identity" />
        </id>
        <many-to-one name="A" class="A" column="ContainedAInternalId" cascade="save-update"/>
        <property name="SomeOtherInfoString" column="SomeOtherInfoString" not-null="true"/>
    </class>
</hibernate-mapping>

我的问题是,当我保存容器 B 它不会保存它的 ContainedAs 集合,而不是实际包含的 A。 这应该是鲁棒的,B 可以分配现有的和不存在的 As,我想执行单个会话。保存(B)并保存所有内容。

我们将非常感谢您在此事上的帮助。

编辑:在原始 HBM 之一中发现错误已修复,但仍然无法正常工作

class A {..} 
class ContainedA { property of type A and some extra information }
class B : A { collection of type ContainedA  }

As you can tell the idea is to be able to contain a single instance of A in several B's, B itself is also of type A only it can hold other A's

A and B's mapping

  <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
    namespace="REDACTED"
    assembly= "REDACTED">

    <class name="A" table="A" discriminator-value="1">
        <id name="Id" column="Id" type="int" access="field.camelcase-underscore">
            <generator class="identity" />
        </id>

        <discriminator column="Type" type="int"/>

        <subclass name="B" extends="A" discriminator-value="2">
            <bag name="ContainedAs" cascade="all">
                <key column="AInternalId"/>
                <one-to-many class="ContainedA"/>
            </bag>
        </subclass>

    </class>
</hibernate-mapping>

The mapping for ContainedA

    <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
    namespace="REDACTED"
    assembly= "REDACTED">

    <class name="ContainedA" table="ContainedA">
        <id name="Id" type="int" access="field.camelcase-underscore" column="Id">
            <generator class="identity" />
        </id>
        <many-to-one name="A" class="A" column="ContainedAInternalId" cascade="save-update"/>
        <property name="SomeOtherInfoString" column="SomeOtherInfoString" not-null="true"/>
    </class>
</hibernate-mapping>

My issue is that when I save the container B it's not saving it's ContainedAs collection and not the actual A's contained.
This is supposed to be robust, B can be assigned both existing and none existing As and i want to perform a single session.Save(B) and have everything saved.

Your help in the matter would be greatly appreciated.

EDIT: found an error in one of the original HBM's fixed it, still not working

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

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

发布评论

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

评论(2

伴我心暖 2025-01-04 03:43:18

除了映射之外,您还需要显示代码。但我注意到你没有设置反向属性。逆属性定义哪一方“拥有”该关系。这有点违反直觉,但您设置 inverse="true" 来声明另一方拥有该关系。

典型的一对多关系被映射,使得多方是反方。通过该映射,必须将包含的对象添加到集合(一侧),并在多侧的对象上设置对包含对象的引用。

You need to show the code in addition to the mapping. But I notice that you don't have the inverse attribute set. The inverse attribute defines which side "owns" the relationship. It's a bit counter-intuitive, but you set inverse="true" to declare that the other side owns the relationship.

A typical one-to-many relationship is mapped so that the many side is the inverse side. With that mapping, it's necessary to both add the contained object to the collection (one side) and set the reference to the containing object on the object one the many side.

惟欲睡 2025-01-04 03:43:18

这不是完整的答案,而是解决问题的解决方法。

(从我的评论复制)

“我通过将包从 B 移到 A 来解决了这个问题
具体子类映射,在我的代码中 B 本身就是一个基子类
有两种不同的实现,显然 Nhibernate 只是忽略了
包,因为它位于子类基础映射上。”

This isn't a complete answer, rather a workaround which resolved the issue.

(Copied from my comment)

"I've somewhat resolved the issue by moving the bag from B to a
concrete subclass mapping, in my code B is a base subclass itself that
has two different implementation, apparently Nhibernate just ignored
the bag because it was on a subclass base mapping."

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