使用 nhibernate 按子集合中对象的值进行过滤

发布于 2024-09-25 23:05:56 字数 757 浏览 2 评论 0原文

这是设置。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猥︴琐丶欲为 2024-10-02 23:05:56

这是一个可能的 HQL:

select f, d.Description
from Fault f
join fetch f.Alarm a
join a.Descriptions d
where d.Language = :lang

它检索元组列表 (object[2]),其中第一个元素是故障(带有初始化的警报),第二个元素是所选语言的描述文本。

Here's a possible HQL:

select f, d.Description
from Fault f
join fetch f.Alarm a
join a.Descriptions d
where d.Language = :lang

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文