NHibernate:将用户类型对象映射到单独的表

发布于 2024-09-16 00:31:48 字数 799 浏览 2 评论 0原文

让我们从这个映射开始:

<component name="Location">
  ...
  <property name="Settings" type="JsonUserType,...">
    <column name="LocationSettingsType" />
    <column name="LocationSettingsData" />
  </property>
</component>

这映射到

TABLE Primary (
    ...
    LocationSettingsType,
    LocationSettingsData
    ...
)

现在

class Location {
    ...
    object Settings { get; set; }
}

,我想将设置提取到一个单独的表中(因为它们很少出现在此处)。
所以我得到

TABLE Primary (
    ...
    LocationSettingsId,
    ...
)

TABLE Settings (
    Id,
    Type,
    Data
)

我可以保持我的 C# 类相同吗?

更新: 这不是多对一关系。与之前一样,每个位置都有零个或一个设置,并且每个设置最多属于一个位置。

Let's start with this mapping:

<component name="Location">
  ...
  <property name="Settings" type="JsonUserType,...">
    <column name="LocationSettingsType" />
    <column name="LocationSettingsData" />
  </property>
</component>

This maps to

TABLE Primary (
    ...
    LocationSettingsType,
    LocationSettingsData
    ...
)

and

class Location {
    ...
    object Settings { get; set; }
}

Now, I want to extract settings into a separate table (because they are seldom here).
So I get

TABLE Primary (
    ...
    LocationSettingsId,
    ...
)

TABLE Settings (
    Id,
    Type,
    Data
)

Can I keep my C# classes the same?

Update: This is not a many-to-one relationship. As before, each location has zero or one settings, and each settings belong to at most one location.

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

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

发布评论

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

评论(3

筑梦 2024-09-23 00:31:48

我相信与此最接近的是

映射元素;详细信息在这篇文章中进行了解释。

I believe the closest thing that exists to this is the <map> mapping element; details are explained in this article.

草莓味的萝莉 2024-09-23 00:31:48

如果您希望主表和设置表上存在一对多关系,则必须首先设置外键约束。然后,您将使用 XML 中的 bag 属性来映射您的表。每个表都会有一个实体

另请参阅 NHibernate/FluentNHibernate 属性包 上的此问题。

我还建议您购买NHibernate 2 for Beginners一书。这对我帮助很大。

If you want a one to many relationship on the Primary and Settings tables, you'll have to set a foreign key constraint first. Then you'll use the bag property in XML to map your tables. You will have an entity for each table.

See also this question on NHibernate/FluentNHibernate Property Bag.

I also recommend you purchase the NHibernate 2 for Beginners book. It helped me alot.

无所的.畏惧 2024-09-23 00:31:48

这是一个老问题,但我遇到了同样的问题,并寻求我来到这里的解决方案。

component 元素可以将多个列映射到多个对象模型。

join 元素可以将多个表映射到一个对象模型。

主要问题是,虽然组件无法映射模型所属的不同表中的列,但联接无法将不同表列映射到不同的对象模型。

我找到的解决方案是使用两者来实现不同表的映射列到多个对象:

<class name="Primary" table="Primary">
    <id name="Id">
        <generator class="identity"/>
    </id>
    <property name="Name" />
    ...
    <join table="Settings">
        <key column="PrimaryId"/>

        <component name="Location">
          ...
          <property name="Settings" type="JsonUserType,...">
            <column name="LocationSettingsType" />
            <column name="LocationSettingsData" />
          </property>
        </component>
    </join>
</class>

参考:

NHibernate 连接映射元素

NHibernate 组件映射元素

This is an old question but i had the same issue and looking to a solution I came here.

the component element can map several columns to several object models.

the join element can map several tables to an object model.

The main problem is that while the component cannot map columns from a different table where the model belong, the join cannot map the different table columns to a different object model.

The solution I found is to use both to achieve the map column of a different table to several object:

<class name="Primary" table="Primary">
    <id name="Id">
        <generator class="identity"/>
    </id>
    <property name="Name" />
    ...
    <join table="Settings">
        <key column="PrimaryId"/>

        <component name="Location">
          ...
          <property name="Settings" type="JsonUserType,...">
            <column name="LocationSettingsType" />
            <column name="LocationSettingsData" />
          </property>
        </component>
    </join>
</class>

Reference:

NHibernate Join mapping element

NHibernate Component mapping element

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