NHibernate 一对一映射 - 奇怪的连接?

发布于 2024-10-22 05:50:42 字数 1783 浏览 3 评论 0原文

我在 NHibernate 一对一映射方面遇到一些问题。我正在使用 Fluent Nhibernate,并在这篇博客文章中建立了我的映射: http://brunoreis.com/tech/ Fluent-nhibernate-hasone-how-implement-one-to-one-relationship/

表格片段:

dbo.Store
---------
Id : int

dbo.CheckoutSettings
---------
StoreId :int (FK to dbo.Store.Id)

这是一些 HBM:

  <class xmlns="urn:nhibernate-mapping-2.2" schema="Management" mutable="true" name="Store" table="Streo">
    <id name="Id" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" unsaved-value="0">
      <column name="Id" />
      <generator class="native" />
    </id>
<one-to-one cascade="all" class="CheckoutSettings" constrained="false" name="CheckoutSettings" /> 
  </class>

  <class xmlns="urn:nhibernate-mapping-2.2" schema="Management" mutable="true" name="CheckoutSettings" table="CheckoutSettings">
    <id name="StoreId" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <column name="StoreId" />
      <generator class="foreign">
        <param name="property">Store</param>
      </generator>
    </id>
    <one-to-one class="Store" foreign-key="FK_CheckoutSettings_StoreId" name="Store" />
</class>

事情似乎在本地工作,但在我们的测试服务器上,我遇到保存错误,例如意外行数:0(预期:1)。另外,在加载过程中,我看到奇怪的 sql 连接:

select  (columns)
from    checkoutsettings c0
  left outer join store s on keys
  left outer join checkoutsettings c1 on keys
where c0.Storeid = id 

并且这不会返回任何内容,因为结账设置可能没有商店的行。

有什么想法吗?

I'm having some problems with NHibernate one to one mapping. I'm using Fluent Nhibernate, and basd my mappings on this blog post: http://brunoreis.com/tech/fluent-nhibernate-hasone-how-implement-one-to-one-relationship/

A snippit of the tables:

dbo.Store
---------
Id : int

dbo.CheckoutSettings
---------
StoreId :int (FK to dbo.Store.Id)

Here's some HBMs:

  <class xmlns="urn:nhibernate-mapping-2.2" schema="Management" mutable="true" name="Store" table="Streo">
    <id name="Id" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" unsaved-value="0">
      <column name="Id" />
      <generator class="native" />
    </id>
<one-to-one cascade="all" class="CheckoutSettings" constrained="false" name="CheckoutSettings" /> 
  </class>

  <class xmlns="urn:nhibernate-mapping-2.2" schema="Management" mutable="true" name="CheckoutSettings" table="CheckoutSettings">
    <id name="StoreId" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <column name="StoreId" />
      <generator class="foreign">
        <param name="property">Store</param>
      </generator>
    </id>
    <one-to-one class="Store" foreign-key="FK_CheckoutSettings_StoreId" name="Store" />
</class>

Things seem to work locally, but on our test server I get errors saving, such as Unexpected row count: 0 (expected: 1). Also during loading I see odd sql joins:

select  (columns)
from    checkoutsettings c0
  left outer join store s on keys
  left outer join checkoutsettings c1 on keys
where c0.Storeid = id 

And this doesn't return anything as checkout settings may not have a row for the store.

Any ideas?

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

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

发布评论

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

评论(2

二智少女 2024-10-29 05:50:43

您应该将 CheckoutSettings 到 Store 的关系映射为多对一,并将 unique 设置为 true:

<many-to-one class="Store" name="Store" unique="true" column="StoreId" />

从 Store 到 CheckoutSettings 使用带有 property-ref 的一对一:

<one-to-one cascade="all" class="CheckoutSettings" constrained="false" property-ref="Store" name="CheckoutSettings" />

您还应该注意,因为 Store 实体中的 CheckoutSettings-property可以为 null 当您获取商店时,NHibernate 会生成到 CheckoutSettings 表的联接。这是因为 NHibernate 必须知道数据库中是否存在用于获取的存储的 CheckoutSettings,以便在不存在时将 CheckoutSettings-property 设置为 null。

You should map the relation from CheckoutSettings to Store as many-to-one with unique set to true:

<many-to-one class="Store" name="Store" unique="true" column="StoreId" />

From Store to CheckoutSettings use one-to-one with property-ref:

<one-to-one cascade="all" class="CheckoutSettings" constrained="false" property-ref="Store" name="CheckoutSettings" />

You should also note that because the CheckoutSettings-property in the Store entity can be null NHibernate generates a join to CheckoutSettings table when you fetch a store. This is because NHibernate must know is there CheckoutSettings for the fetched store in the database so that it can set the CheckoutSettings-property null if there isn't one.

愛上了 2024-10-29 05:50:43

经过进一步调查,我们发现一些触发器在插入和更新时做了奇怪的事情。一旦我们弄清楚了这一点,我们就能够做出改变来解决问题。

After further investigation we discovered that we had some triggers doing odd things on insert and update. Once we figured this out, we were able to make changes that resolved the issue.

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