This content is also prepared according to the requirements of the business students. In fact, this small project is just graduated from the time to do, many times we want to perform the following sql/hql and then get an HTML table output:
Input: Select ID as number, name as name, age as aged from XXX
Output:
The requirement is that if the SQL changes, still show all the other name segments and data.
Because now hibernate used more widely, so the priority is to use Hibernate to achieve, the results found that if the entity mapping query statements, can be easily used: List query.getreturnaliases () to get aliases, However, we know that the query sometimes is very complex, not all hql, at this time with SQLQuery, surprised to hint that this method has not been implemented (the latest version of the Hibernate 3.3 has not been tested), the version is Hibernate 3.2, the corresponding code is:
DAO
/**
* 根据查询语句返回结果, 并包含结果的列名
* @param hql
* @param args
* @return
*/
public List queryAllForReport( final String hql, final Object... args) {
List list = getHibernateTemplate().executeFind (new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query query = session.createQuery(hql);
for(int i =0; i < args.length; i++) {
query.setParameter(i, args[i]);
}
List list = query.list();
list.add(0, query.getReturnAliases());
return list;
}
});
// Hibernate做count计算返回一般都是对象
return list;
}