String hSql = "select delegateGroup.groupName,count(delegateGroup),(select count(*) from " +t_DelegateGroup+" ) from " + t_DelegateGroup + " as delegateGroup , "+t_AdviceContent+" as adviceContent where delegateGroup.groupName = adviceContent.delegateGroup group by delegateGroup.groupName order by delegateGroup.groupName desc ";这是我写的Hql,字符变量t_DelegateGroup和t_AdviceContent为类名,现在这个语句能够 执行获得list, 但是(select count(*) from " +t_DelegateGroup+" )获得不到值,只能得到前两项 ,还不出错, 郁闷,
/** 但是(select count(*) from " +t_DelegateGroup+" )获得不到值 **/谁告诉你的,得不到值,你的sql这样写效率不高的,你不信,没办法... 不过你要得到count(*),这有何难? int recordCount=((Integer)session.createQuery("select count(*) from t_DelegateGroup").list().iterator().next()).intValue();
String hSql = "select delegateGroup.groupName,count(delegateGroup),(select count(*) from " +t_DelegateGroup+" ) from " + t_DelegateGroup + " as delegateGroup , "+t_AdviceContent+" as adviceContent where delegateGroup.groupName = adviceContent.delegateGroup group by delegateGroup.groupName order by delegateGroup.groupName desc ";我机器上没环境,不能验证这句sql是否正确,但是如你所说 你的这个hSql可以得到list,那么 String[] result = {"","",""}; try { Query query = session.createQuery(hSql); Iterator it = query.iterate(); while (it.hasNext()) { Object[] objs = (Object[])it.next(); result[0] = (objs[0]!=null)?objs[0]+"":""; result[1] = (objs[1]!=null)?objs[1]+"":""; result[2] = (objs[1]!=null)?objs[2]+"":""; break; } session.flush(); } catch (Exception e) { e.printStackTrace(); }这里只取了一条记录的三个字段就break出来了,如果记录不止一条,你还要再做处理。 result数组里面不就是放的你要的三个字段么? 可能还要根据需要转下型。
首先非常感谢冷月无声热心帮忙,int recordCount=((Integer)session.createQuery("select count(*) from t_DelegateGroup").list().iterator().next()).intValue();您的这段代码让我有点苦笑不得。我要是连这都不知道就不用混了。
这几天看书又,发现了一些好用东西,我认为在做统计时是非常有用的,现在跟大家分享一下。 String hSql = "select new "+delegateGroupPersonTimeRow+"( delegateGroup.groupName,count(delegateGroup),sum(adviceContent.adviceCount)) from " + t_DelegateGroup + " as delegateGroup , "+t_AdviceContent+" as adviceContent where delegateGroup.groupName = adviceContent.delegateGroup and delegateGroup.type=:type1 group by delegateGroup.groupName order by delegateGroup.groupName desc "; Query query = super.getHibernateSession().createQuery(hSql); List list = query.list();用Hsql统计出来的是一些数字组成的list,这个list不能像查询出来的dto对象一样直接通过 struts标签bean:write 直接打到叶面上,上面的hsql语句中我用到 new "+delegateGroupPersonTimeRow+"( )语法,delegateGroupPersonTimeRow为数据bean的类名,由于类名比较长用一个字符串变量代替的,这种语法可以将统计出的数字直接放到数据bean中,像查询出的 dto对象一样用标签显示。*delegateGroupPersonTimeRow类必须有 get、set方法
一定要强求一句sql搞定,那用jdbc来解决吧,不要指望hibernate能搞定一切sql,不可能的。
t_DelegateGroup + " as delegateGroup , "+t_AdviceContent+" as adviceContent where delegateGroup.groupName = adviceContent.delegateGroup group by delegateGroup.groupName order by delegateGroup.groupName desc ";这是我写的Hql,字符变量t_DelegateGroup和t_AdviceContent为类名,现在这个语句能够 执行获得list, 但是(select count(*) from " +t_DelegateGroup+" )获得不到值,只能得到前两项 ,还不出错, 郁闷,
直接在HIBERNATE开个SQL接口,来执行这个SQL啊!HQL是很强大,但是跟SQL比还是有距离的!
你说的那是配置文件,不需要该源代码。
/** 但是(select count(*) from " +t_DelegateGroup+" )获得不到值 **/谁告诉你的,得不到值,你的sql这样写效率不高的,你不信,没办法...
不过你要得到count(*),这有何难?
int recordCount=((Integer)session.createQuery("select count(*) from t_DelegateGroup").list().iterator().next()).intValue();
t_DelegateGroup + " as delegateGroup , "+t_AdviceContent+" as adviceContent where delegateGroup.groupName = adviceContent.delegateGroup group by delegateGroup.groupName order by delegateGroup.groupName desc ";我机器上没环境,不能验证这句sql是否正确,但是如你所说
你的这个hSql可以得到list,那么
String[] result = {"","",""}; try {
Query query = session.createQuery(hSql);
Iterator it = query.iterate();
while (it.hasNext()) {
Object[] objs = (Object[])it.next();
result[0] = (objs[0]!=null)?objs[0]+"":"";
result[1] = (objs[1]!=null)?objs[1]+"":"";
result[2] = (objs[1]!=null)?objs[2]+"":"";
break;
} session.flush();
} catch (Exception e) {
e.printStackTrace(); }这里只取了一条记录的三个字段就break出来了,如果记录不止一条,你还要再做处理。
result数组里面不就是放的你要的三个字段么? 可能还要根据需要转下型。
用newxy可以不写代码实现数据的增、删、改等;newxy也提供了面向对象的NQL语言,但没有烦人特殊语法。newxy技术网站:http://www.newxy.net
newxy范例:http://www.newxy.net/zh_cn/samples/index.jsp
Query query = session.createQuery(hSql);
Iterator it = query.iterate();
while (it.hasNext()) {
Object[] objs = (Object[])it.next();
result[0] = (objs[0]!=null)?objs[0]+"":"";
result[1] = (objs[1]!=null)?objs[1]+"":"";
result[2] = (objs[1]!=null)?objs[2]+"":"";
break;
}
我不知道你为什么要将list转换成数组???
String hSql = "select new "+delegateGroupPersonTimeRow+"( delegateGroup.groupName,count(delegateGroup),sum(adviceContent.adviceCount)) from " + t_DelegateGroup + " as delegateGroup , "+t_AdviceContent+" as adviceContent where delegateGroup.groupName = adviceContent.delegateGroup and delegateGroup.type=:type1 group by delegateGroup.groupName order by delegateGroup.groupName desc ";
Query query = super.getHibernateSession().createQuery(hSql);
List list = query.list();用Hsql统计出来的是一些数字组成的list,这个list不能像查询出来的dto对象一样直接通过
struts标签bean:write 直接打到叶面上,上面的hsql语句中我用到 new "+delegateGroupPersonTimeRow+"( )语法,delegateGroupPersonTimeRow为数据bean的类名,由于类名比较长用一个字符串变量代替的,这种语法可以将统计出的数字直接放到数据bean中,像查询出的 dto对象一样用标签显示。*delegateGroupPersonTimeRow类必须有 get、set方法