需要以下文件:
Customer.java PO对象
Charge.java PO对象
TotalCharge.java 用于保存统计结果Bean即是多表查询结果的所需字段
Customer.hbm.xml PO映射
Charge.hbm.xml PO映射
TotalChargeDao.java 统计Dao定义
TotalChargeDaoImpl.java 统计Dao定义实现
DaoFactory.java Dao工厂
HibernateSessionFactory.java Session工厂
//下面是一个 TotalChargeDaoImpl.java 里实现多表查询的方法
public List statTotalCharge(Date statTimeBegin, Date statTimeEnd) throws DaoException{
List res = new Vector();//将用于存放保存的结果集合
Session session = null;
ScrollableResults srs = null;
try{
session = HibernateSessionFactory.openSession();//得到一个Hibernate Session
//下面创建一个匿名Query实例并调用它的scroll()方法返回以ScrollableResults形式组织的查询结果
srs = session.createQuery(“select b.name, count(a.fee) mix(a.chargeBeginTime) max(a.chargeEndTime) from charge a, customer b where a.idCustomer = b.idCustomer and a.chargeBeginTime >= ? and a.chargeEndTime < ? gourp by a.idCustomer“).setDate(0, statTimeBegin).setDate(1, statTimeEnd).scroll();
//将查询结果放入List保存
while(srs.next()){
res.add(new TotalCharge(srs.getString(0), srs,getDouble(1), srs.getDate(2), srs.getDate(3)));
}
}catch(HibernateException he){
;//loging err..
if(srs!=null){
try{
srs.close();
}catch(Exception e){
;
}
}
}finally{
try{
session.close();
}catch(Exception e){
;
}
}
return res;
}