使用 nhibernate 按子集合中对象的值进行过滤
这是设置。
NHibernate、Fluent NHibenrate 和 Nhibernate Linq
涉及的实体有:
- Fault - 发生故障的记录
- Alarm - 有关故障的信息,将其视为故障类型(所有可能发生的故障的列表)
- AlarmDescription - 人类可读的描述,每种语言都有一个
故障有一个警报,警报有一组描述,系统中的每种语言都有一个描述。
一个报警可以被多种故障引用。
当用户搜索其中一个参数时,他们可以根据描述来搜索(和排序)故障。这意味着传递要使用的特定语言。
要完成的 SQL 上手起来非常简单:
SELECT f.*, a.*, d.Description
FROM Fault f
JOIN Alarm a ON f.Alarm_id = a.Id
JOIN AlarmDescription d ON a.Id = d.Alarm_id AND d.Language = @lang
上面的查询会给我所有的错误、它们的警报和所选语言的描述。
然而让 Nhibernate 生成这样的查询被证明是困难的。
因此,它归结为过滤器之一是主对象的子对象的子集合。我尝试让它与 Linq2Nhibenrate、HQL 一起工作,并尝试让 Native SQL 也能工作。本机 SQL 似乎最有可能成功,但我不知道如何正确映射别名。我愿意接受任何解决方案,包括更改域模型。这个已经把我难住了。
Here's the setup.
NHibernate, Fluent NHibenrate and Nhibernate Linq
The entities invoved are
- Fault - a record of a fault occuring
- Alarm - information about the fault, think of it as a fault type ( a list of all possible faults that can occur )
- AlarmDescription - human readable description, one for each language
A fault has an alarm and alarm has a collection of descriptions, one for each langague in the system.
An alarm can be referenced by many faults.
When a user searches one of the paramters they can search ( and order ) faults by is description. Which means passing down the specific language to use.
The SQL to accomplish is brain dead simple to get started :
SELECT f.*, a.*, d.Description
FROM Fault f
JOIN Alarm a ON f.Alarm_id = a.Id
JOIN AlarmDescription d ON a.Id = d.Alarm_id AND d.Language = @lang
The above query would give me all fault, their alarm and the descriptions for the selected langauge.
However getting Nhibernate to generate such a query is proving difficult.
So it boils down to one of the filters being a Child Collection of Child Object of the main object. I have tried to get this working with Linq2Nhibenrate, HQL and trying to get Native SQL to work as well. Native SQL seems the most likley to succeed but I cannot figure out how to get the aliases to map correctly. I'm up for any solution including changing the domain model. This one has had me stumped.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个可能的 HQL:
它检索元组列表 (
object[2]
),其中第一个元素是故障(带有初始化的警报),第二个元素是所选语言的描述文本。Here's a possible HQL:
This retrieves a list of tuples (
object[2]
) where the first element is a Fault (with an initialized Alarm), and the second one is the description text for the selected language.