how to retrieve distinct root entity row count in hibernate? -


i have show employee , project in dropdown employee in right ride of tabular format. in top need show number of records. so, doing 2 queries, 1 retrieving count , retrieving employee , project.finally, total count comes employee*project count.

if employee has 3 projects counts 3. need employee count. , retriving distinct employee object, employee object has list of project.

i used .setresulttransformer(criteriaspecification.distinct_root_entity) this.

please me employee count instead of employee*project, struggling 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 methods using.

there problem in hibernate when doing joins , trying count. problem distinct_root_entity removes equal rows, since did join, rows have different fields although same entity.

if want count , joins @ same time, put objects retrieved set. way duplicates go away. can set#size count.

or can use hql , write query manually.

maybe if edit question , add actual queries creating can further.

update

add after: list lst = criteria.list();

set<employee> employeeset = new hashset<employee>(); employeeset.addall(lst); return employeeset; 

Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -

PostgreSQL 9.x - pg_read_binary_file & inserting files into bytea -