hibrnate 两表 排序 sql

List<Qbdx> list = null;
String sql ="select q.id,name,baike,q.url,image,q.type,q.createdTime,q.updatedTime from 表一 as q ,表二as d  where q.id=d.qbdxId and type='"+num+"'";
if (!StringUtils.isEmpty(name)) {
sql += " and name LIKE '%" + name + "%'";
}
if(times.indexOf("3")==-1){
sql += " and q.createdTime  BETWEEN ? and ?   GROUP BY name  ORDER BY count(*) desc";
list = (List<Qbdx>) ((SQLQuery) getSession().createSQLQuery(sql).setParameter(0, dateFrom).setParameter(1, dateNow).setResultTransformer(Transformers.aliasToBean(Qbdx.class)))
.addScalar("id", StandardBasicTypes.LONG)// 此为数据类型
.addScalar("name", StandardBasicTypes.STRING)//
.addScalar("baike", StandardBasicTypes.STRING)//
.addScalar("url", StandardBasicTypes.STRING)//
.addScalar("image", StandardBasicTypes.STRING)//
.addScalar("type", StandardBasicTypes.INTEGER)//
.addScalar("createdTime", StandardBasicTypes.DATE)//
.addScalar("updatedTime", StandardBasicTypes.DATE)//
.list();
return list;
}else{
sql+="  GROUP BY q.name  ORDER BY count(*) desc";
list = (List<Qbdx>) ((SQLQuery) getSession().createSQLQuery(sql).setResultTransformer(Transformers.aliasToBean(Qbdx.class)))
.addScalar("id", StandardBasicTypes.LONG)//
.addScalar("name", StandardBasicTypes.STRING)//
.addScalar("baike", StandardBasicTypes.STRING)//
.addScalar("url", StandardBasicTypes.STRING)//
.addScalar("image", StandardBasicTypes.STRING)//
.addScalar("type", StandardBasicTypes.INTEGER)//
.addScalar("createdTime", StandardBasicTypes.DATE)//
.addScalar("updatedTime", StandardBasicTypes.DATE)//
.list();
return list;
}

你可能感兴趣的:(sql,排序,两表,hibrnate)