NHibernate一对一映射,非主键

发布于 2024-09-26 04:46:06 字数 951 浏览 11 评论 0原文

无法让 NHibernate 生成正确的查询。它继续使用我为一对一关系而加入的两个表的主键,并且我无法弄清楚如何在其中一个表中指定外键。

tableA      tableB
{ aID,      { bID,
  bID,        z,
  c,          y,
  d }         x }

因此 tableA 应该使用 tableA.bID = tableB.bID 连接到 tableB。如何在 tableA 的映射中指定这一点?我使用 tableA 类从 tableA 中检索一行,从 tableB 中检索一行,因为它是真正的一对一关系。

NHibernate 使用 tableA.aID = tableB.bID 生成连接表的 sql,这是错误的。

这不起作用:

<class name="tableA" table="tableA">
  <id name="aID" column="aID" />
  <property name="bID" column="bID" />
  <property name="c" column="c" />
  <property name="d" column="d" />
  <one-to-one name="otherThing" class="tableB" foreign-key="bID" />
</class>

<class name="tableB" table="tableB">
  <id name="bID" column="bID" />
  <property name="z" column="z" />
  <property name="y" column="y" />
  <property name="x" column="x" />
</class>

Can't get NHibernate to generate the correct query. It keeps using the primary keys of the two tables I'm joining for the one-to-one relationship, and I can't figure out how to specify the foreign key in one of the tables.

tableA      tableB
{ aID,      { bID,
  bID,        z,
  c,          y,
  d }         x }

so the tableA should join to tableB using tableA.bID = tableB.bID. How can I specify this in the mapping for tableA? I'm using the tableA class to retrieve a row from tableA and a row from tableB, as it is a real one to one relationship.

NHibernate generates the sql to join the tables using tableA.aID = tableB.bID, which is wrong.

This does not work:

<class name="tableA" table="tableA">
  <id name="aID" column="aID" />
  <property name="bID" column="bID" />
  <property name="c" column="c" />
  <property name="d" column="d" />
  <one-to-one name="otherThing" class="tableB" foreign-key="bID" />
</class>

<class name="tableB" table="tableB">
  <id name="bID" column="bID" />
  <property name="z" column="z" />
  <property name="y" column="y" />
  <property name="x" column="x" />
</class>

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

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

发布评论

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

评论(2

绻影浮沉 2024-10-03 04:46:06

这是映射它的正确方法:

<class name="tableA" table="tableA">
  ...
  <many-to-one name="otherThing" class="tableB" column="bID" unique="true" />
</class>
  • 我假设这确实是一对一的,因此是唯一的。
  • 您应该将每列映射一次。如果是关系,那么它就不是 int 属性。

从 tableB 到 tableA 的引用将实现为:

<class name="tableB" table="tableB">
  ...
  <one-to-one name="A" class="tableA" property-ref="otherThing" />
</class>

这全部记录在 http://nhibernate.info/doc/nh/en/index.html#mapping-declaration-onetoone

This is the correct way to map it:

<class name="tableA" table="tableA">
  ...
  <many-to-one name="otherThing" class="tableB" column="bID" unique="true" />
</class>
  • I'm assuming this is really one-to-one, hence the unique.
  • You should map each column ONCE. If it's a relationship, then it's not an int property.

A reference from tableB to tableA would be implemented as:

<class name="tableB" table="tableB">
  ...
  <one-to-one name="A" class="tableA" property-ref="otherThing" />
</class>

This is all documented in http://nhibernate.info/doc/nh/en/index.html#mapping-declaration-onetoone

巴黎夜雨 2024-10-03 04:46:06

当您还为同一值定义一对一关系时,不需要将该属性指定为映射文件中的属性。我没有使用过一对一,所以可能还有另一个问题,但我会

<property name="bID" column="bID" /> 

从 tableA 中删除该行:看看是否有帮助。

You shouldn't need to specifiy the property as a property in your mapping file when you're also defining a one-to-one relationship for the same value. I haven't used one-to-one, so there may be another issue, but I'd remove the line:

<property name="bID" column="bID" /> 

from tableA and see if that helps.

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