是否可以通过复合 ID 的键进行多对一的导航?

发布于 2024-08-01 15:23:21 字数 1242 浏览 4 评论 0原文

是否可以在 Nhibernate 中浏览复合 ID 的键多对一关联?

我有一些使用以下设置映射的(旧)表:

<class name="StructureUser">
    <composite-id>
        <key-many-to-one name="Structure" class="Structure" column="STRUKTUR_ID" />
        <key-many-to-one name="User" class="User" column="USERID" />
    </composite-id>
    ...
</class>

<class name="Structure">
    <id name="Id" column="id" type="Int32" >
        <generator class="native"/>
    </id>
    <property name="Low" column="low" type="Int32" />
    ...
</class>

我想通过查询中的 StructureUser 类访问 Structure 的“Low”属性。 我尝试了我能想到的 Criteria API 的所有用法,但总是出错。 以下是我尝试过的两个查询:

ICriteria crit1 = Session.CreateCriteria(typeof(StructureUser))
    .CreateAlias("Structure", "struc")
    .Add(Restrictions.Le("struc.Low", 123));

ICriteria crit2 = Session.CreateCriteria(typeof(StructureUser))
    .Add(Restrictions.Le("Structure.Low", 123));

crit1 中的错误来自数据库,并表示“struc_1.Low 在此上下文中无效”,因为 NHibernate 不会生成 sql 命令中的限制所需的联接。 crit2 中的错误来自 NHibernate,告诉我它无法访问 StructureUser 上的“Structure.Low”属性。

我通过使用 key-property 元素声明 compoto-id 并声明与普通多对一元素的关系来解决这个问题。

这个问题还有其他解决方案吗?

Is it possible to navigate through the key-many-to-one associations of a composite-id in Nhibernate?

I have a few (legacy) tables that I mapped with the following settings:

<class name="StructureUser">
    <composite-id>
        <key-many-to-one name="Structure" class="Structure" column="STRUKTUR_ID" />
        <key-many-to-one name="User" class="User" column="USERID" />
    </composite-id>
    ...
</class>

<class name="Structure">
    <id name="Id" column="id" type="Int32" >
        <generator class="native"/>
    </id>
    <property name="Low" column="low" type="Int32" />
    ...
</class>

I want to access the "Low" property of Structure through the StructureUser class in a query. I tried every usage of the Criteria API I could think of but always an error. Here are two of the queries I tried:

ICriteria crit1 = Session.CreateCriteria(typeof(StructureUser))
    .CreateAlias("Structure", "struc")
    .Add(Restrictions.Le("struc.Low", 123));

ICriteria crit2 = Session.CreateCriteria(typeof(StructureUser))
    .Add(Restrictions.Le("Structure.Low", 123));

The error in crit1 comes from the database and says, that "struc_1.Low is not valid in this context", because NHibernate doesn't generate the join needed for the restriction in the sql command.
The error in crit2 comes from NHibernate, telling me that it can't access the "Structure.Low" property on StructureUser.

I got around this problem by declaring the composite-id with key-property elements and declaring the relationships with normal many-to-one elements.

Is there another solution to this problem?

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

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

发布评论

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

评论(2

活雷疯 2024-08-08 15:23:21

你有没有尝试过

ICriteria crit1 = Session.CreateCriteria(typeof(StructureUser))
    .CreateCriteria("Structure", "struc")
    .Add(Restrictions.Le("struc.Low", 123));

have you tried

ICriteria crit1 = Session.CreateCriteria(typeof(StructureUser))
    .CreateCriteria("Structure", "struc")
    .Add(Restrictions.Le("struc.Low", 123));
请叫√我孤独 2024-08-08 15:23:21

我有类似的问题。

正如原始海报所说,您需要在要别名的复合键元素上添加多对一关系,并且需要将属性“insert”和“update”设置为“false”< /强>。

然后你可以将其保留在composite-id中,不需要将其更改为key-property元素。

<class name="StructureUser">
<composite-id>
    <key-many-to-one name="Structure" class="Structure" column="STRUKTUR_ID" />
    <key-many-to-one name="User" class="User" column="USERID" />
</composite-id>

<many-to-one name="Structure" class="Structure" column="STRUKTUR_ID" insert="false" update="false" />

...

I had a similar problem.

As the original poster said, you need to add a many-to-one relationship on the element from the composite key you want to alias and you need to set the properties "insert" and "update" to "false".

Then you can keep it in the composite-id, you don't need to change it into a key-property element.

<class name="StructureUser">
<composite-id>
    <key-many-to-one name="Structure" class="Structure" column="STRUKTUR_ID" />
    <key-many-to-one name="User" class="User" column="USERID" />
</composite-id>

<many-to-one name="Structure" class="Structure" column="STRUKTUR_ID" insert="false" update="false" />

...

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