如何在休眠中检索不同的根实体行数? [英] how to retrieve distinct root entity row count in hibernate?

查看:35
本文介绍了如何在休眠中检索不同的根实体行数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须以正确的表格格式向员工展示Employee及其项目.在顶部,我需要显示记录数.因此,我正在做两个查询,一个用于检索计数,另一个用于检索雇员及其项目.最后,总数来自员工*项目计数.

I have to show Employee and his Project in dropdown to employee in the right ride of tabular format. In the top i need to show number of records. So, I am doing two queries, one for retrieving count and another for retrieving employee and his project.finally, the total count comes employee*project count.

如果一个员工有3个项目,那么它就计为3.但是我只需要员工数.我正在检索不同的 Employee 对象,该雇员对象具有 Project 的列表.

If an employee has 3 projects then it counts to 3. But I need only employee count. And I am retriving distinct Employee object, that employee object has list of Project.

我使用 .setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY)来获得这样的效果.

I used .setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY) to get like this.

请帮助我仅获取雇员人数,而不是 employee * project (雇员*项目).

Please help me to get only employee count instead of employee*project, I am struggling with this.

我的编码是,

public Collection fetchEmployeeWithProject(final List condition,
            final PaginationCriteria pageCriteria)
            throws DataAccessLayerException {
                 Session session = this.getPersManager().getCurrentSession();

        Criteria criteria = session.createCriteria(Employee.class, "employee")
                .createAlias(
                        "employee.empProject", "empProject",
                        CriteriaSpecification.LEFT_JOIN).createAlias(
                        "empProject.Project", "project",
                        CriteriaSpecification.LEFT_JOIN);
        criteria = this.addMultipleSeachCriteria(criteria, condition);
        this.buildPaginatedCriteria(criteria, pageCriteria);
        List lst = criteria.list();
        session.clear();
    return lst;
    }

protected Criteria buildPaginatedCriteria(Criteria criteria,
            PaginationCriteria pageCriteria) throws DataAccessLayerException {

        logger.debug(LOG_PREFIX + "buildPaginatedCriteria::Begin");

        if (pageCriteria != null) {

            if (!pageCriteria.isFetchAll()) {

                if (pageCriteria.getTotalRecords() == 0)
                    pageCriteria.setTotalRecords(((Integer) criteria
                            .setProjection(Projections.rowCount())
                            .uniqueResult()).intValue());
                criteria.setProjection(null);
                criteria.setFirstResult(
                        pageCriteria.getFirstRecordOfCurrentPage())
                        .setMaxResults(pageCriteria.getRecordsPerPage());
            }

            if (StringUtils.isNotBlank(pageCriteria.getSortBy()))
                criteria.addOrder(pageCriteria.isSortDescending() ? Order
                        .desc(pageCriteria.getSortBy()) : Order
                        .asc(pageCriteria.getSortBy()));

            if (StringUtils.isNotBlank(pageCriteria.getSecondarySortBy())) {
                criteria.addOrder(Order.asc(pageCriteria.getSecondarySortBy()));
            }

            if (pageCriteria.isCached()) {
                criteria.setCacheable(true).setCacheMode(CacheMode.NORMAL);
            }
        }
        criteria
                .setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);

        logger.debug(LOG_PREFIX + "buildPaginatedCriteria::End");

        return criteria;
    }

这些是我正在使用的方法.

These are the methods that I am using.

推荐答案

在进行联接并尝试计数时,Hibernate中存在问题. DISTINCT_ROOT_ENTITY 的问题在于,它删除了相等的行,但是由于您进行了联接,因此尽管某些行是相同的实体,但某些行却具有不同的字段.

There is a problem in Hibernate when doing joins and trying to count. The problem with DISTINCT_ROOT_ENTITY is that it removes equal rows, but since you did a join, some rows have different fields although it is the same entity.

如果要同时计数并进行联接,请将所有检索到的对象放入 Set 中.这样,重复项就会消失.然后,您可以执行 Set#size 进行计数.

If you want to count and do joins at the same time, put all the objects retrieved into a Set. This way the duplicates will go away. Then you can do Set#size to count.

或者您可以使用 HQL 和手动编写查询.

Or you can use HQL and write your query manually.

也许,如果您编辑问题并添加正在创建的实际查询,我们可以为您提供进一步的帮助.

Maybe if you edit your question and add the actual queries that you are creating we can help you further.

更新

在此之后添加: List lst = criteria.list();

Set<Employee> employeeSet = new HashSet<Employee>();
employeeSet.addAll(lst);
return employeeSet;

这篇关于如何在休眠中检索不同的根实体行数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆