sql - how can get count of rows in hibernate when hql have group by? -


i have hql query have group .in pagination result want count of result show in pagination . in query donot have group .i write utility create count of query hql query

select u  personel u  u.lastname='azizkhani' 

i find main "from" keyword , substring hql , add count(*) , make query

select count(*)  personel u   u.lastname='azizkhani' 

when have query contain group can not

select u.lastname,count(*)  personel u  group u.lastname; 

count of query in sql

select count(*)     (          select u.lastname,count(*)           tbl_personel u           group  u.lastname     ) 

how can generate group query hql ??

i have genericrepository have method

public <u> pagingresult<u> getallgrid(string hql,map<string, object> params,pagingrequest searchoption); 

and developer call this

   string hqlquery = " select e personel e 1<>2 , e.lastname=:lastname";      hashmap<string, object> params = new hashmap<string, object>();     params.put("lastname", 'azizkhani');       return getallgrid(hqlquery, params, new pagingrequest( 0/*page*/, 10 /*size*/) ); 

in genericrepository return pagingresult object have property

public class pagingresult<t> {      private int totalelements;      @jsonproperty("rows")     private list<t> items;      public pagingresult() {      }      public pagingresult(int totalelements, list<t> items) {         super();         this.totalelements = totalelements;         this.items = items;     }       public int gettotalelements() {         return totalelements;     }      public void settotalelements(int totalelements) {         this.totalelements = totalelements;     }       public list<t> getitems() {         return items;     }      public void setitems(list<t> items) {         this.items = items;     }  } 

in genericrepository execute 2 query ,first 1 10 result , second totalrecords .developer send hql .i make hql totalcount . query dose not have "distinct" or "group by" make hql .but when hql have "distinct" , "group by" have problem .

public <u> pagingresult<u> getallgrid(string hql, map<string, object> params, pagingrequest searchoption) {         session session = getsession();         applydafaultauthorizefilter(session);           query query = session.createquery(hql);         if (searchoption != null) {             if (searchoption.getsize() > 0) {                 query.setfirstresult(searchoption.getpage() * searchoption.getsize());                 query.setmaxresults(searchoption.getsize());             }         }         if (params != null)             hqlutility.setqueryparameters(query, params);          list<u> list = query.getresultlist();          query countquery = session.createquery("select count(*) " + hqlutility.retrivecountqueryfromhql(hql));          if (params != null)             hqlutility.setqueryparameters(countquery, params);          int count = ((long) countquery.uniqueresult()).intvalue();         if (searchoption != null)             return new pagingresult<u>(searchoption.getpage(), count, searchoption.getsize(), list);         else             return new pagingresult<u>(0, count, 0, list);     }      public static stringbuffer retrivecountqueryfromhql(stringbuffer jql) {         if(jql.indexof("order by")>=0)             jql.replace(jql.indexof("order by"), jql.length(),"");         string mainquery = jql.tostring();          jql = new stringbuffer(jql.tostring().replace('\t', ' '));         int firstindexpbas = jql.indexof(")");         int firstindexpbaz = jql.lastindexof("(", firstindexpbas);         while (firstindexpbas > 0) {             (int = firstindexpbaz; < firstindexpbas + 1; i++)                 jql.replace(i, + 1, "*");             firstindexpbas = jql.indexof(")");             firstindexpbaz = jql.lastindexof("(", firstindexpbas);         }         int indexfrom = jql.indexof(" ");         return new stringbuffer(" " + mainquery.substring(indexfrom, jql.length()));     }      public void applydafaultauthorizefilter(session session) {         filter filter = session.enablefilter("defaultfilter");         filter.setparameter("userid", securityutility.getauthenticateduserid());         filter.setparameter("orgid", securityutility.getauthenticateduserorganization().getid());     } 

how can solve problem without change signature of genericrepository???

i think have solution convert hql sql , create native query select count(*) ( hql_to_sql) have 2 problem

  1. hql sql dose not have api support parameter
  2. hql sql dose not have api support hibernate filter

why not replace group by count(distinct)?

so instead of

select u tbl_personel u group u.lastname

you do

select count(distinct u.lastname) tbl_personel u


Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -