简体   繁体   English

是否可以通过Composite-ID的“多对一”键进行导航?

[英]Is navigation through composite-id's key-many-to-one possible?

Is it possible to navigate through the key-many-to-one associations of a composite-id in Nhibernate? 是否可以在Nhibernate中浏览复合ID的键多对一关联?

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. 我想通过查询中的StructureUser类访问Structure的“低”属性。 I tried every usage of the Criteria API I could think of but always an error. 我尝试了可能想到的Criteria API的所有用法,但始终会出错。 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. crit1中的错误来自数据库,并指出“ struc_1.Low在此上下文中无效”,因为NHibernate不会在sql命令中生成限制所需的联接。 The error in crit2 comes from NHibernate, telling me that it can't access the "Structure.Low" property on StructureUser. crit2中的错误来自NHibernate,告诉我它无法访问StructureUser上的“ Structure.Low”属性。

I got around this problem by declaring the composite-id with key-property elements and declaring the relationships with normal many-to-one elements. 我通过声明带有键属性元素的Composite-id并声明了与普通多对一元素的关系来解决这个问题。

Is there another solution to this problem? 这个问题还有其他解决方案吗?

have you tried 你有没有尝试过

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

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" . 就像原始海报所说的那样,您需要在要别名的组合键上的元素上添加多对一关系, 并且需要将属性“ insert”和“ update”设置为“ false”

Then you can keep it in the composite-id, you don't need to change it into a key-property element. 然后,您可以将其保留在Composite-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>

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

...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM