使用子查询获取订单和订单行计数
我正在将旧应用程序移植到 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 表的主键是 CompanyCode 和 OrderNumber。
我想查询订单并获取每个订单的行数。
我已经实现了我想要添加公式属性的目标(感谢 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
可能不完全是您正在寻找的内容,但您是否考虑过集合上的 where 子句。您可以调用 order.OrderLinesFiltered.Count 来获取该值
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
最后,我决定使用公式属性:
有一天,如果我需要更改过滤器,我可能会考虑使用 nHibernate 过滤器。
At the end I've decided to go for the formula property:
One day, if I'll need to change filter I might think to use nHibernate filters.