在分页后设置子查询条件的投影(SetFirstResult()和SetMaxResults())

时间:2011-11-07 18:18:35

标签: nhibernate nhibernate-criteria

假设我有这样的代码:

DetachedCriteria limitCriteria = CriteriaTransformer.Clone(criteria);
limitCriteria.SetProjection(Projections.Id()).SetFirstResult(30).SetMaxResults(10);

DetachedCriteria pagedCriteria = CriteriaTransformer.Clone(criteria);
pagedCriteria.Add(Subqueries.PropertyIn("Id", limitCriteria));

var entities = pagedCriteria.GetExecutableCriteria(session).List<SomeEntity>();

它会生成如下的Oracle SQL查询:

SELECT this_.TID  AS TID135_6_,
  . . . -- a lot of fields here
FROM SomeEntity this_,
  . . . -- a lot of tables here
WHERE 
    . . . -- a lot of conditions here
    AND this_.TID IN

  (SELECT *
  FROM
    (SELECT row_.*,
      rownum rownum_
    FROM
      (SELECT this_0_.TID AS y0_
      FROM EXECDESK.ORDER_EXECUTION_CONFIRMATION this_0_
      WHERE this_0_.OPUS_ORDER_EXECUTION_ID IS NULL
      ) row_
    WHERE rownum <= 40
    )
  WHERE rownum_ > 30
  ) 

查询执行因“太多值”消息而失败,因为IN子句中的SELECT返回两列(ID和rownum)。

我尝试在子查询语句上创建投影(没有成功),以使SQL类似于:

SELECT this_.TID  AS TID135_6_,
  . . . -- a lot of fields here
FROM SomeEntity this_,
  . . . -- a lot of tables here
WHERE 
    . . . -- a lot of conditions here
    AND this_.TID IN

( select y0_ from    
  (SELECT *
  FROM
    (SELECT row_.*,
      rownum rownum_
    FROM
      (SELECT this_0_.TID AS y0_
      FROM EXECDESK.ORDER_EXECUTION_CONFIRMATION this_0_
      WHERE this_0_.OPUS_ORDER_EXECUTION_ID IS NULL
      ) row_
    WHERE rownum <= 40
    )
  WHERE rownum_ > 30
  )    
)

我应该如何编写ICriteria查询才能获得ID列?

0 个答案:

没有答案