NHibernate一对一映射,非主键
无法让 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是映射它的正确方法:
从 tableB 到 tableA 的引用将实现为:
这全部记录在 http://nhibernate.info/doc/nh/en/index.html#mapping-declaration-onetoone
This is the correct way to map it:
unique
.A reference from tableB to tableA would be implemented as:
This is all documented in http://nhibernate.info/doc/nh/en/index.html#mapping-declaration-onetoone
当您还为同一值定义一对一关系时,不需要将该属性指定为映射文件中的属性。我没有使用过一对一,所以可能还有另一个问题,但我会
从 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:
from tableA and see if that helps.