I am having an HQL query
String q = "From TMainTable where a= ? and b= ? and c=?"
Query q = session.createQuery(q).setParameter(0,1).setParameter(1,2).setParameter(2,3);
int count = getCount(q);
List<TMainTable> list = q.setFirstResult(pageNo).setMaxResults(maxLimit).list()
public int getCount(Query q){
return q.list().size();
}
But the getCount method is taking so much of time. Say my table has 10000 rows and I am getting only 20 records at a time with the actual count. What is the fastest method to get count using the Query object.
I have tried this in the getCount function
public Long getCount(Query q){
String queryString = q.getQueryString();
String countQueryString = "Select count(*) From "+queryString.substring(0, q.getQueryString().indexOf(" From"));
System.out.println(countQueryString);
Long c= (Long)ctx.getSession().createQuery(countQueryString).uniqueResult();
return c;
}
But how can I set the parameters in the new query ? Is there a way I can alter only the query string in Query Object.