使用子查询获取订单和订单行计数

发布于 2024-11-28 14:18:22 字数 1730 浏览 0 评论 0原文

我正在将旧应用程序移植到 Nhibernate。
旧应用程序广泛使用 ORACLE 软件包,我想摆脱它。
我已经开始映射一些表,事情似乎运行得很好。
现在,我有了这个查询,我希望能够通过 QueryOver ... 或类似的东西来管理它:

SELECT
    Orders.*
    (SELECT COUNT(*) FROM OrderLines
        WHERE OrderLines.CompanyCode = Orders.CompanyCode
              AND OrderLines.OrderNumber = Orders.OrderNumber
              AND NOT (OrderLines.OCLSCOM = 'Y' AND OrderLines.OCLSSEQ = 0)
              AND OrderLines.Status = 'R') OrderLinesCount
    FROM
        Orders
    WHERE
        AND Orders.CompanyCode = [CompanyCode];

[CompanyCode] 是一个过滤器。

我必须映射文件(Orders 和 OrderLines),我的关联如下所示:

<class name="Order" table="Orders">
    ...
    <set name="OrderLines" access="field.pascalcase-underscore" inverse="true" lazy="extra" cascade="none">
      <key>
        <column name="OrderNumber" not-null="true"/>
        <column name="CompanyCode" not-null="true"/>
      </key>
      <one-to-many class="OrderLine" not-found ="ignore"/>
    </set>
</class>

我的 Orders 表的主键是 CompanyCodeOrderNumber

我想查询订单并获取每个订单的行数。

我已经实现了我想要添加公式属性的目标(感谢 Ayende)关于订单映射:

<property name="OrderLinesCount" formula="(SELECT COUNT(*) FROM OrderLines WHERE OrderLines.CompanyCode = CompanyCode AND OrderLines.OrderNumber = OrderNumber AND NOT (OrderLines.OCLSCOM = 'Y' AND OrderLines.OCLSSEQ = 0) AND OrderLines.Status = 'R')" />

但我担心有一天我的客户可能会决定更改那些令人讨厌的过滤器,而我将被迫重新编译整个项目。

有没有办法通过子查询(QueryOver)获得相同的结果?

预先感谢您的帮助。

I am in process to port an old App to Nhibernate.
The old application uses ORACLE packages extensively and I want to get rid of that.
I've started to map few tables and things seem to work very well.
Now, I've got this query which I would like to be able to manage via QueryOver ... or something similar:

SELECT
    Orders.*
    (SELECT COUNT(*) FROM OrderLines
        WHERE OrderLines.CompanyCode = Orders.CompanyCode
              AND OrderLines.OrderNumber = Orders.OrderNumber
              AND NOT (OrderLines.OCLSCOM = 'Y' AND OrderLines.OCLSSEQ = 0)
              AND OrderLines.Status = 'R') OrderLinesCount
    FROM
        Orders
    WHERE
        AND Orders.CompanyCode = [CompanyCode];

[CompanyCode] is a filter.

I've got to mapping files (Orders and OrderLines) and my association looks like this:

<class name="Order" table="Orders">
    ...
    <set name="OrderLines" access="field.pascalcase-underscore" inverse="true" lazy="extra" cascade="none">
      <key>
        <column name="OrderNumber" not-null="true"/>
        <column name="CompanyCode" not-null="true"/>
      </key>
      <one-to-many class="OrderLine" not-found ="ignore"/>
    </set>
</class>

The primary key for my Orders table is CompanyCode and OrderNumber.

I would like to query the Orders and fetch the number of lines for each order.

I've achieve what I want adding a formula property (thanks Ayende for that) on the Order mapping:

<property name="OrderLinesCount" formula="(SELECT COUNT(*) FROM OrderLines WHERE OrderLines.CompanyCode = CompanyCode AND OrderLines.OrderNumber = OrderNumber AND NOT (OrderLines.OCLSCOM = 'Y' AND OrderLines.OCLSSEQ = 0) AND OrderLines.Status = 'R')" />

but I am scared my customer might decided to change those nasty filters one day and I would be forced to recompile the whole project.

Is there a way to achieve the same result with a subquery (QueryOver) ?

Thanks in advance for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

天煞孤星 2024-12-05 14:18:22

可能不完全是您正在寻找的内容,但您是否考虑过集合上的 where 子句。您可以调用 order.OrderLinesFiltered.Count 来获取该值

<set name="OrderLinesFiltered" table="OrderLines" 
access="field.pascalcase-underscore" inverse="true" lazy="extra"
cascade="none" 
where=" NOT (OCLSCOM = 'Y' AND OCLSSEQ = 0) AND Status = 'R' ">
  <key>
    <column name="OrderNumber" not-null="true"/>
    <column name="CompanyCode" not-null="true"/>
  </key>
  <one-to-many class="OrderLine" not-found ="ignore"/>
</set> 

Might not be exactly what you are looking for but have you considered a where clause on the collection. You can call order.OrderLinesFiltered.Count to get the value

<set name="OrderLinesFiltered" table="OrderLines" 
access="field.pascalcase-underscore" inverse="true" lazy="extra"
cascade="none" 
where=" NOT (OCLSCOM = 'Y' AND OCLSSEQ = 0) AND Status = 'R' ">
  <key>
    <column name="OrderNumber" not-null="true"/>
    <column name="CompanyCode" not-null="true"/>
  </key>
  <one-to-many class="OrderLine" not-found ="ignore"/>
</set> 
撕心裂肺的伤痛 2024-12-05 14:18:22

最后,我决定使用公式属性:

<property name="OrderLinesCount" formula="(SELECT COUNT(*) FROM OrderLines WHERE OrderLines.CompanyCode = CompanyCode AND OrderLines.OrderNumber = OrderNumber AND NOT (OrderLines.OCLSCOM = 'Y' AND OrderLines.OCLSSEQ = 0) AND OrderLines.Status = 'R')" />

有一天,如果我需要更改过滤器,我可能会考虑使用 nHibernate 过滤器。

At the end I've decided to go for the formula property:

<property name="OrderLinesCount" formula="(SELECT COUNT(*) FROM OrderLines WHERE OrderLines.CompanyCode = CompanyCode AND OrderLines.OrderNumber = OrderNumber AND NOT (OrderLines.OCLSCOM = 'Y' AND OrderLines.OCLSSEQ = 0) AND OrderLines.Status = 'R')" />

One day, if I'll need to change filter I might think to use nHibernate filters.

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