-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: how to get the count for the exsit HQL without query.list()
PostPosted: Thu Jan 24, 2013 2:18 am 
Newbie

Joined: Thu Jan 24, 2013 2:07 am
Posts: 2
I have a problem: when I paging the data with HQL, I need to get the total count of result, but query.list() return all the data, I just went a number.
the HQL is:
select s from com.XXX.XXX.Staff s left join s.rolesSet rs where rs.name =:roleName and s.sex =:sexName

if it's SQL, such as:
select s from table_Staff s left join table_role rs on rs.staffId = s.id where rs.name =:roleName and s.sex =:sexName
I can get the count with:
select count(*) from (select s from table_Staff s left join table_role rs on rs.staffId = s.id where rs.name =:roleName and s.sex =:sexName)

but HQL can't use sub-select at from.
so how can I get the cout of HQL with paramater without do query.list();


Top
 Profile  
 
 Post subject: Re: how to get the count for the exsit HQL without query.list()
PostPosted: Fri Jan 25, 2013 3:48 am 
Newbie

Joined: Thu Jan 24, 2013 2:07 am
Posts: 2
dontyer wrote:
I have a problem: when I paging the data with HQL, I need to get the total count of result, but query.list() return all the data, I just went a number.
the HQL is:
select s from com.XXX.XXX.Staff s left join s.rolesSet rs where rs.name =:roleName and s.sex =:sexName

if it's SQL, such as:
select s from table_Staff s left join table_role rs on rs.staffId = s.id where rs.name =:roleName and s.sex =:sexName
I can get the count with:
select count(*) from (select s from table_Staff s left join table_role rs on rs.staffId = s.id where rs.name =:roleName and s.sex =:sexName)

but HQL can't use sub-select at from.
so how can I get the cout of HQL with paramater without do query.list();


nobody knows?
I have a stupid solution:
protected final Integer getQueryResultCount(String hql, Map<String, Object> parameterMap)
throws Exception {
hql = getCountSql(hql,parameterMap, this.getHibernateTemplate().getSessionFactory()); //change to sql
hql = hql.replaceAll(" and", " and ");
return this.getHibernateTemplate().findCountHQL(hql);
}
/**
* 将hql用参数替换后,转成sql语句(change by dong 20130124)
* @param originalHql
* @param parameterMap
* @param sessionFactory
* @return
* @throws Exception
*/
protected String getCountSql(String originalHql, Map<String, Object> parameterMap,
org.hibernate.SessionFactory sessionFactory) throws Exception {
originalHql = packageHQLParamaterData(originalHql, parameterMap); // Fill up the paramaters '= :workes' as '='values''
QueryTranslatorImpl queryTranslator = new QueryTranslatorImpl(originalHql, originalHql,
Collections.EMPTY_MAP, (org.hibernate.engine.SessionFactoryImplementor)sessionFactory);
queryTranslator.compile(Collections.EMPTY_MAP, false);
String sql = queryTranslator.getSQLString().replace("(", "").replace(")", "");
return "SELECT COUNT(*) AS countNumber FROM ( " + sql.replace("or*","or ") + " )";
}

/**
* 替换hql的参数
* @param originalHql
* @param parameterMap
* @return
*/
private String packageHQLParamaterData(String originalHql,
Map<String, Object> parameterMap) {
for(String key:parameterMap.keySet()){
if(parameterMap.get(key) instanceof Integer || parameterMap.get(key) instanceof Long){
originalHql = originalHql.replace(" = :"+key+" ", " = "+parameterMap.get(key)+" ");
originalHql = originalHql.replace(" != :"+key+" ", " != "+parameterMap.get(key)+" ");
originalHql = originalHql.replace(" == :"+key+")", " == "+parameterMap.get(key)+" )");
originalHql = originalHql.replace(" != :"+key+")", " != "+parameterMap.get(key)+" )");
}else if(parameterMap.get(key) instanceof Boolean){
String val = (Boolean)parameterMap.get(key) ? "Y" : "N";
originalHql = originalHql.replace(" = :"+key+" ", " = '"+val+"' ");
originalHql = originalHql.replace(" != :"+key+" ", " != '"+val+"' ");
originalHql = originalHql.replace(" = :"+key+")", " = '"+val+"' )");
originalHql = originalHql.replace(" != :"+key+")", " != '"+val+"' )");
}else{
originalHql = originalHql.replace(" = :"+key+" ", " = '"+(String)parameterMap.get(key)+"'");
originalHql = originalHql.replace(" != :"+key+" ", " != '"+(String)parameterMap.get(key)+"'");
originalHql = originalHql.replace(" like :"+key+" ", " like '"+(String)parameterMap.get(key)+"'");
originalHql = originalHql.replace(" = :"+key+")", " = '"+(String)parameterMap.get(key)+"' )");
originalHql = originalHql.replace(" != :"+key+")", " != '"+(String)parameterMap.get(key)+"' )");
originalHql = originalHql.replace(" like :"+key+")", " like '"+(String)parameterMap.get(key)+"' )");
originalHql = originalHql.replace(" in (:"+key+") ", " in ("+(String)parameterMap.get(key)+")");
}
}
return originalHql;
}

this a bad ideal, Errors all over the place at the beginning, but lucky, it work well now.

so somebody have batter ideal, can share with me, thanks


Top
 Profile  
 
 Post subject: Re: how to get the count for the exsit HQL without query.list()
PostPosted: Fri Jan 25, 2013 7:38 am 
Hibernate Team
Hibernate Team

Joined: Fri Sep 09, 2011 3:18 am
Posts: 295
Hi,
have you tried the following?
Object count = session.createQuery("select count(s) from table_Staff s left join table_role rs on rs.staffId = s.id where rs.name =:roleName and s.sex =:sexName").setParameter(...).uniqueResult();


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.