C# + Castle ActiveRecord:HasAndBelongsToMany 和集合
假设我在帖子和标签之间有多对多关系(使用 ActiveRecord 属性 HasAndBelongsToMany)(更改域对象名称以保护无辜者),并且我想要一个类似的方法
FindAllPostByTags(IList<Tag> tags)that returns all Posts that have all (not just some of) the Tags in the parameter. Any way I could accomplish this either with NHibernate Expressions or HQL? I've searched through the HQL documentation and couldn't find anything that suited my needs. I hope I'm just missing something obvious!
Let's say I have many-to-many relationship (using the ActiveRecord attribute HasAndBelongsToMany) between Posts and Tags (domain object names changed to protect the innocent), and I wanted a method like
FindAllPostByTags(IList<Tag> tags)
that returns all Posts that have all (not just some of) the Tags in the parameter. Any way I could accomplish this either with NHibernate Expressions or HQL? I've searched through the HQL documentation and couldn't find anything that suited my needs. I hope I'm just missing something obvious!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我刚刚遇到了同样的问题并尝试阅读 HQL 文档,但是某些功能似乎没有在 NHibernate 中实现(例如使用关键字)
我最终得到了这种解决方案:
意思是,动态构建HQL 对每个标签使用 join,然后在 WHERE 子句中进行选择。 这对我有用。 我尝试使用 DetachedCriteria 执行相同的操作,但在尝试多次连接表时遇到了麻烦。
I just had the same problem and tried to read the HQL-documentation, however some of the features doesn't seem to be implemented in NHibernate (with-keyword for example)
I ended up with this sort of solution:
Meaning, dynamically build the HQL using join for each tag, then make the selection in your WHERE clause. This worked for me. I tried doing the same thing with a DetachedCriteria but ran into trouble when trying to join the table multiple times.
我现在手头没有安装 Castle 的系统,所以我没有测试或编译它,但下面的代码应该可以完成您想要的操作。
I don't have a system at hand with a Castle install right now, so I didn't test or compile this, but the code below should about do what you want.
您也可以只使用
IN
语句,我尚未编译该语句,因此它可能会出现错误
You could also just use an
IN
statementI haven't compiled that so it could have errors