簡體   English   中英

需要將選擇多個值的子查詢轉換為Nhibernate查詢(條件或hql)

[英]Need to convert a subquery that selects multiple values to an nhibernate query (criteria or hql)

我有以下查詢,我需要將其轉換為nhibernate:

SELECT  o.* 
FROM orders o
INNER JOIN 
(
        -- get the most recent orders based on end_date (this implies the same order can exist in the orders table more than once)
        SELECT o2.order_id, MAX(o2.end_date) AS max_end_date 
        FROM orders o2
        GROUP BY o2.order_id
) most_recent_orders 
                ON o.order_id=most_recent_orders.order_id
                AND o.end_date=most_recent_orders.max_end_date
-- of the most recent orders, get the ones that are complete                
WHERE o.is_complete=1

我知道hql不支持加入子查詢,這就是為什么它不起作用的原因。 我不能使用“ in”語句,因為子查詢正在選擇2個值。 我嘗試使用休眠文檔中的建議:

http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/queryhql.html#queryhql-tuple

from Cat as cat
where not ( cat.name, cat.color ) in (
    select cat.name, cat.color from DomesticCat cat
)

但這引發了錯誤,因為Sql Server不喜歡“ in”語句中的多個值。

任何幫助,將不勝感激! 我願意使用Criteria或Hql解決方案。

這是使用子查詢來實現相同

var maxDateQuery = DetachedCriteria.For<Order>()
    .Add(Restrictions.PropertyEq("OrderId", "order.OrderId"))
    .SetProjection(Projections.Property("EndDate"));

var results = session.CreateCriteria<Order>("order")
    .Add(Subqueries.Eq("EndDate",maxDateQuery))
    .Add(Restrictions.Eq("IsComplete", true))
    .List<Order>();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM