NHibernate 父子 JOIN 并限制关系类型
我有一个类单位(例如公司),它与其他单位有很多关系。 我无法更改数据库结构。数据库看起来像:
Units:
UnitID
UnitName
...
UnitToUnitRelationships:
FromUnitID
ToUnitID
RelationshipType
我需要在查询时限制RelationshipType。我目前有一个带有 ISet ChildUnits 和 ISet ParentUnits 的 Unit 类(因为在不同的关系类型下,您可能有不同的父级)。我的域中不需要RelationshipType;我只会关心一种特定的类型。如果我编写自己的 SQL,我只需确保所有查询在 ON
或 WHERE
子句中都有 RelationshipType='FOO'
。
我正在使用 Fluent 进行映射;我现有的子映射看起来像这样:
HasManyToMany(x => x.ChildUnits).AsSet().Cascade.None().
Table("UnitToUnitRelationships").Not.LazyLoad().Fetch.Subselect().
ParentKeyColumn("ToUnitID").ChildKeyColumn("FromUnitID").BatchSize(1000);
我想我可能需要创建一个 UnitToUnitRelationship
类并搞乱它,但如果有更简单的方法,我当然更喜欢这样做,特别是因为如上所述,我不需要我域中的财产。
I have a class Unit (e.g. of a Company) which has many relationships with other Units. I cannot change the database structure. The db looks something like:
Units:
UnitID
UnitName
...
UnitToUnitRelationships:
FromUnitID
ToUnitID
RelationshipType
I need to restrict the RelationshipType when querying. I currently have a Unit class with a ISet ChildUnits and ISet ParentUnits (since under different relationship types you may have different parents). I have no need for the RelationshipType in my domain; I would only ever be concerned with one particular Type. If I were writing my own SQL, I would just make sure all my queries had RelationshipType='FOO'
either in the ON
or WHERE
clause.
I'm using Fluent to do the mapping; my existing child mapping looks like this:
HasManyToMany(x => x.ChildUnits).AsSet().Cascade.None().
Table("UnitToUnitRelationships").Not.LazyLoad().Fetch.Subselect().
ParentKeyColumn("ToUnitID").ChildKeyColumn("FromUnitID").BatchSize(1000);
I think I may need to create a UnitToUnitRelationship
class and mess with that, but if there's an easier way, I'd certainly prefer that, especially since as noted, I don't need the property in my domain.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该查看
.ChildWhere 的
应用于子表.Where()
和/或.ChildWhere()
方法.Where()
()因此,我认为您应该使用
.Where("RelationshipType='FOO'")
来解决您的问题。You should look at
.Where()
and/or.ChildWhere()
methods.Where()
aplyed to reference table.ChildWhere()
applyed to child tableSo, I think that you should use
.Where("RelationshipType='FOO'")
to resolve your issue.