牛骨文教育服务平台(让学习变的简单)
博文笔记

多表联接查询select count()

创建时间:2015-11-09 投稿人: 浏览次数:1296

需求:在动态添加查询条件的情况下拼接hql语句,并计算出满足这些查询条件的记录数

之前代码:

1.在Action中拼接hql语句

String conditionsHql="from VerificationRecord vr 

                           left outer join fetch vr.operation  myopera 

                           left outer join fetch  vr.device  mydevi 

                           left outer join fetch  vr.model  mymodel 

                           left outer join fetch  vr.result myresu 

                           left outer join fetch vr.voice myvoic

                           where myopera.name=" "+arrayValue.get(i)+" "

                           and myresu.name=" "+arrayValue.get(i)+" "

                           and vr.id=""+arrayValue.get(i)+""

                           and vr.istTime > to_date(""+arrayValue.get(i)+"","yyyy-MM-dd HH24:mi:ss")

                           and mymode.id=""+arrayValue.get(i)+""

                           and vr.score>="+scoreBet[0]+" and vr.score<="+scoreBet[1]";

2. 在DAO里查询hql语句,返回集合的size(),从而实现计算出满足这些查询条件的记录数

Query queryObject =getCurrentSession().createQuery(conditionsHql);
return queryObject.list();


-------------------------------------------------------------------------------------------------------------------------------

之后发现这样的想法是错的,且在数据达到几十万条的时候运行还出现了错误。这样写hql语句相当于把所有的记录查出来了,而我其实只要它的条数就可以了,在大数量时显然速度会很慢,这样想的话用select count(*)这样的语句仅查出来有多少条记录数即可,继而进行第二次修改:

1.在Action中拼接hql语句,由于聚合函数count()不能喝fetch关键字联用,我们就去掉fetch,加上count() 

String conditionsHql="select count(vr.id)  from VerificationRecord vr 

                           left outer join vr.operation  myopera 

                           left outer join vr.device  mydevi 

                           left outer join vr.model mymodel 

                           left outer join vr.result myresu 

                           left outer join vr.voice myvoic

                           where myopera.name=" "+arrayValue.get(i)+" "

                           and myresu.name=" "+arrayValue.get(i)+" "

                           and vr.id=""+arrayValue.get(i)+""

                           and vr.istTime > to_date(""+arrayValue.get(i)+"","yyyy-MM-dd HH24:mi:ss")

                           and mymode.id=""+arrayValue.get(i)+""

                           and vr.score>="+scoreBet[0]+" and vr.score<="+scoreBet[1]";


去掉fetch关键字会让查询出来的一条记录不能成为一个VerificationRecord 实体,而是一条记录有operation  ,device  ,model...多个类组成,这样也会报错 


最后在同学的帮助下,知道VerificationRecord 的懒加载lazy如果没有关,则可以不用写很多个left outer join来做联接,最后一次修改:

1.在Action中拼接hql语句

String conditionsHql="select count(vr.id) from VerificationRecord vr

                      where vr.operation.name=""+arrayValue.get(i)+""

                      and vr.result.name=""+arrayValue.get(i)+""

                      and vr.id=""+arrayValue.get(i)+""

                      and vr.istTime > to_date(""+arrayValue.get(i)+"","yyyy-MM-dd HH24:mi:ss")

                      and vr.model.id=""+arrayValue.get(i)+""

                      and vr.score>="+scoreBet[0]+" and vr.score<="+scoreBet[1]

2. 在DAO里查询hql语句

Query queryObject = getCurrentSession().createQuery(conditionsHql);
return ((Long) queryObject.iterate().next()).intValue();



这样的查询是正确的!



声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。