简体   繁体   English

独特的结果和具有多对多关系的分页

[英]distinct results and pagination with many-to-many relation

I have a M-to-M relation going from Nomination to User mapped on a "Nominee" table. 我有一个从提名到用户的M-M关系,映射到“提名”表上。 I have the following method to encapsulate results in a paging class called "ResultPage": 我有以下方法将结果封装在称为“ ResultPage”的分页类中:

protected ResultPage<T> findPageByCriteria(Criteria criteria, int page,
                                               int pageSize) {
        DataVerify.notNull(criteria);
        DataVerify.greaterThan(page, 0, "Invalid page number");
        DataVerify.isTrue(pageSize >= 0, "Invalid page size");
        if (logger.isDebugEnabled()) {
            logger.debug("Arguments: ");
            logger.debug("Page: " + page);
            logger.debug("Page size: " + pageSize);
        }
        int totalItems = 0;
        List<T> results = null;
        if (pageSize != 0) {
            totalItems = ((Number) criteria.setProjection(Projections.rowCount()).
                    uniqueResult()).intValue();
            criteria.setProjection(null);

            criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
            criteria.addOrder(Order.desc("id"));

            results = criteria.setFirstResult((page-1) * pageSize).
                    setMaxResults(pageSize).list();
        } else {
            results = criteria.setFirstResult((page-1) * pageSize).
                    list();
            totalItems = results.size();
        }

        ResultPage<T> resultsPage = new ResultPage<T>(results, page,
                totalItems,
                (pageSize != 0) ? pageSize :
                        totalItems);
        if (logger.isDebugEnabled()){
            logger.debug("Total Results: " + resultsPage.getTotalItems());
        }
        return resultsPage;
    }

Now fetching is done right. 现在,提取已正确完成。 However my results count is not being consistent. 但是,我的结果计数并不一致。 This of course only happens when a "Nomination" has more than 1 user assigned to it. 当然,只有在“提名”中分配了多个用户时,才会发生这种情况。 It then counts the users instead of the root entity and thus I get totals of "1 to 22" per page instead of "1 to 25" like I have specified - as if there are 22 nominations but 25 users total. 然后,它计算的是用户而不是根实体,因此,我每页的总数为“ 1到22”,而不是我指定的“ 1到25”-好像有22个提名但共有25个用户。

Can I get some help for this? 我可以为此得到一些帮助吗? Let me know if I have to clarify. 让我知道是否需要澄清。

if anything this is the question that comes as closest as my problem: how to retrieve distinct root entity row count in hibernate? 如果有的话,这是和我的问题最接近的问题: 如何在休眠状态下检索不同的根实体行数?

The solution I use for this problem is to have a first query to only load the IDs of the root entities that satisfy the criteria (ie the IDs of your 25 nominations), and then issue a second query which loads the data of these 25 IDs, by doing a query like the following 我用于此问题的解决方案是让第一个查询仅加载满足条件的根实体的ID(即25个提名的ID),然后发出第二个查询,以加载这25个ID的数据,通过执行如下查询

select n from Nomination n 
[... joins and fetches] 
where n.id in (:ids)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM