1

我有一个这样的 ICriteria 查询:

var contentCriteria = DetachedCriteria.For<InvoiceItem>();
var countCriteria = DetachedCriteria.For<InvoiceItem>();

if (model.CurrentPage <= 0) model.CurrentPage = 1;

if (model.OnlyShowErrors)
{
    contentCriteria.Add(Restrictions.Not(Restrictions.Eq("TroubleClass", TroubleClasses.Success)));
    countCriteria.Add(Restrictions.Not(Restrictions.Eq("TroubleClass", TroubleClasses.Success)));
}

if (!string.IsNullOrEmpty(model.BatchId))
{
    contentCriteria.Add(Restrictions.Eq("BatchId", model.BatchId));
    countCriteria.Add(Restrictions.Eq("BatchId", model.BatchId));
}

if (model.DocumentStartDate != null)
{
    contentCriteria.Add(Restrictions.Ge("DocumentDate", model.DocumentStartDate));
    countCriteria.Add(Restrictions.Ge("DocumentDate", model.DocumentStartDate));
}

if (model.DocumentEndDate != null)
{
    contentCriteria.Add(Restrictions.Le("DocumentDate", model.DocumentEndDate));
    countCriteria.Add(Restrictions.Le("DocumentDate", model.DocumentEndDate));
}

if (!string.IsNullOrEmpty(model.VendorId))
{
    contentCriteria.Add(Restrictions.Eq("VendorId", model.VendorId));
    countCriteria.Add(Restrictions.Eq("VendorId", model.VendorId));
}


using (var session = GetSession())
{

    var countC = countCriteria.GetExecutableCriteria(session)
        .SetProjection(Projections.CountDistinct("RecordId"));

    var contentC = contentCriteria
        .AddOrder(Order.Desc("PersistedTimeStamp"))
        .GetExecutableCriteria(session)
        .SetResultTransformer(Transformers.DistinctRootEntity)
        .SetFirstResult((model.CurrentPage * model.ItemsPerPage) - model.ItemsPerPage)
        .SetMaxResults(model.ItemsPerPage);

    var mq = session.CreateMultiCriteria()
        .Add("total", countC)
        .Add<InvoiceItem>("paged", contentC);

    model.Invoices = ((IEnumerable<InvoiceItem>)mq.GetResult("paged"));
    model.Invoices = model.Invoices
        .OrderBy(x => x.PersistedTimeStamp);

    model.TotalItems = (int)(mq.GetResult("total") as System.Collections.ArrayList)[0];
}
return model;

这会返回结果,但我希望结果是在 组中的model.ItemsPerPage,它很少是。我认为.SetResultTransformer(Transformers.DistinctRootEntity)转换是在.SetMaxResults(model.ItemsPerPage)限制之后运行的,我不知道为什么或如何解决它。有人可以启发我吗?

4

2 回答 2

0

您需要查看 NHibernate 生成的 SQL,因为这本质上不是 NHibernate 错误,而是 SQL 查询在一起应用时ROWNUM的行为。DISTINCT这一直是我们已知问题列表中的一个问题。

以下网址可能会启发您...

行号与不同

ROWNUM 的工作原理

于 2012-08-21T17:51:59.650 回答
0

所以这与这篇博文中所写的内容直接相关。此外,我遇到了 PostgreSQL 特定于平台的复杂性,它不允许 DISTINCT 有序集由不在 SELECT 列表中的东西排序。最终,我不得不对数据库进行两次调用,如下所示:

using (var session = GetSession())
{
    //I honestly hope I never have to reverse engineer this mess.  Pagination in NHibernate
    //when ordering by an additional column is a nightmare.
    var countC = countCriteria.GetExecutableCriteria(session)
        .SetProjection(Projections.CountDistinct("RecordId"));

    var contentOrdered = contentCriteria
        .SetProjection(Projections.Distinct(
            Projections.ProjectionList()
                .Add(Projections.Id())
                .Add(Projections.Property("PersistedTimeStamp"))
                ))
        .AddOrder(Order.Desc("PersistedTimeStamp"))
        .SetFirstResult((model.CurrentPage * model.ItemsPerPage) - model.ItemsPerPage)
        .SetMaxResults(model.ItemsPerPage);

    var contentIds = contentOrdered.GetExecutableCriteria(session)
        .List().OfType<IEnumerable<object>>()
        .Select(s => (Guid)s.First())
        .ToList();

    var contentC = DetachedCriteria.For<InvoiceItem>()
        .Add(Restrictions.In("RecordId", contentIds))
        .SetResultTransformer(Transformers.DistinctRootEntity);

    var mq = session.CreateMultiCriteria()
        .Add("total", countC)
        .Add("paged", contentC);

    model.Invoices = (mq.GetResult("paged") as System.Collections.ArrayList)
        .OfType<InvoiceItem>()
        .OrderBy(x => x.PersistedTimeStamp);

    model.TotalItems = (int)(mq.GetResult("total") as System.Collections.ArrayList)[0];
}
return model;

这并不漂亮,但它奏效了;我认为 NHibernate 的人们需要在这方面努力,让它变得更容易一些。

于 2012-08-21T18:36:57.720 回答