How to get a separate number of rows of root entities in sleep mode?

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.

+4
source share
1 answer

In sleep mode, a problem occurs when combining and trying to count. The problem with DISTINCT_ROOT_ENTITY is that it deletes the same lines, but since you made the connection, some lines have different fields, although this is the same entity.

If you want to read and make connections at the same time, put all the objects in Set . Thus, duplicates disappear. Then you can do Set#size to count.

Or you can use HQL and write a query manually.

Perhaps if you edit your question and add the actual queries that you create, we can help you.

Update

Add this after: List lst = criteria.list();

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

All Articles