是否可以通过复合 ID 的键进行多对一的导航?
是否可以在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你有没有尝试过
have you tried
我有类似的问题。
正如原始海报所说,您需要在要别名的复合键元素上添加多对一关系,并且需要将属性“insert”和“update”设置为“false”< /强>。
然后你可以将其保留在composite-id中,不需要将其更改为key-property元素。
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.