---
title: "Hql语句注意事项总结（转载）"
author: "beckrabbit"
date: "2007-10-18T11:37:08Z"
tags: []
slug: "hql"
draft: false
lastmod: "2007-10-18T11:37:08Z"
id: 133152
url: "https://beckrabbit.iteye.com/blog/133152"
---

### Hql语句注意事项总结（转载）

1.在Hql中使用group by的问题

(1)使用统计函数作为输出结果

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)select sum(total),sum(totalup) from AD where aid=? and fid=? and vdatetime&gt;=? and vdatetime&lt;=? group by aid

(2)使用统计函数作为查询约束条件

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)select uid from MM group by uid having count(\*)&gt;? and min(m\_date)&lt;?

> (3)使用group by 时有关约束条件的问题
> 
> 如果约束条件是单个字段,则必须用where,并且where要放在group by之前,如:
> 
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)select cityname from MI where vdatetime&gt;=? and vdatetime&lt;? group by cityname
> 
> 如果约束条件是统计函数,则要使用having,并且having要放在group by之后,如:(2)
> 
> (4)其实,在某些场合,group by可以用distinct代替,如以下两条语句功能相同:
> 
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)select cityname from MI where vdatetime&gt;=? and vdatetime&lt;? group by cityname  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)select distinct cityname from MI where  vdatetime&gt;=? and vdatetime&lt;?

2.在count()中使用别名问题

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)select count(\*) from MipMailconfig as MipMailconfig where MipMailconfig.uid &lt;&gt; ? and MipMailconfig.email = ?-- 正确  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)select count(MipMailconfig.\*) from MipMailconfig as MipMailconfig where MipMailconfig.uid &lt;&gt; ? and MipMailconfig.email = ?-- 错误

3.关于Integer和Long的问题

在Hibernate3.2中，所有的计数都要用Long，用Integer的话就会有问题.

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)        List list=this.find("select count(\*) from MMC as MMC where MMC.uid &lt;&gt; ? and MMC.email = ?",new String\[]...{uid,email});  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)        if(list!=null&&!list.isEmpty())...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            long count=Long.parseLong(list.get(0).toString());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            if(count&gt;0)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                return true;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            else  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                return false;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)        }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

4.关于参数是数组的问题

在实际的工作中，经常遇到这种情况：  
(1)要删除一批数据，传过来一个数组，  
(2)或者要查询一批数据，查询条件也是传过来一个数组，  
这种情况下如何进行处理？  
(1)查询数据，查询条件为一个数组，可以有如下两个方法：  
&lt;1&gt;直接拼Hql语句

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)        String\[] ids = ...{"1","2","3"};  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)        String str="";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)        for(int i=0;i&lt;ids.length;i++)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            str+="'"+ids\[i]+"'";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            if(i!=(ids.length-1))  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                str+=",";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)            }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)        List list;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)        list = appinfoManager.find("from Appinfo where id in ("+str+")");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)        if(list!=null)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)            for(int i=0;i&lt;list.size();i++)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                Appinfo app = (Appinfo)list.get(i);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                System.out.println(app.getAppname());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)            }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)            }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)        System.out.println("共有"+list.size()+"条记录");

&lt;2&gt;利用回调函数处理

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)    public List getApList() ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        return (List)this.getHibernateTemplate().execute(new HibernateCallback() ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        public Object doInHibernate(Session session) throws HibernateException, SQLException ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)            String\[] ids = ...{"1","2","3"};  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            String  hql=  "from Appinfo where id  in  (:ids)";   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            Query query = session.createQuery(hql);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            List list = query.setParameterList("ids", ids).list();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            return list;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)        }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)        });  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)    }

(2)删除数据，参数为数组，利用回调函数

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)    public void delInArray() ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        this.getHibernateTemplate().execute(new HibernateCallback() ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        public Object doInHibernate(Session session) throws HibernateException, SQLException ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)            String\[] ids = ...{"1","2","3"};  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            String  hql=  "delete Appinfo where id  in  (:ids)";   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            Query query = session.createQuery(hql);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            query.setParameterList("ids", ids).executeUpdate();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            return null;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)        }      
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)        });      
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)    }