I have to show Employee and his Project in the drop-down list for the employee on the right trip in tabular format. At the top, I need to show the number of records. So, I make two requests: one for receiving the invoice and another for receiving the employee and his project. Finally, the total bill comes employee * project .
If an employee has 3 projects, then he is considered equal to 3. But I only need the number of employees. And I retrieve a separate Employee object, this employee object has a Project list.
I used .setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY) to do this.
Please help me get only the number of employees instead of the project * employee , I am struggling with this.
My COde is,
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 use.
source share