首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

怎么结合hibernate进行复杂的查询功能设计

2012-08-31 
如何结合hibernate进行复杂的查询功能设计package test.bean/**?*作者:孙星?**/public class TotalCharge

如何结合hibernate进行复杂的查询功能设计

package test.bean;

/**
?*作者:孙星
?**/
public class TotalCharge {
? private String name;
? private Double fee;
? private java.util.Date chargeTimeBegin;
? private java.util.Date chargeTimeEnd;
? public TotalCharge() {
? }

? public String getName() {
??? return name;
? }

? public TotalCharge(String name, Double fee, java.util.Date chargeTimeBegin,
???????????????????? java.util.Date chargeTimeEnd) {
??? this.name = name;
??? this.fee = fee;
??? this.chargeTimeBegin = chargeTimeBegin;
??? this.chargeTimeEnd = chargeTimeEnd;
? }

? public void setName(String name) {
??? this.name = name;
? }

? public Double getFee() {
??? return fee;
? }

? public void setFee(Double fee) {
??? this.fee = fee;
? }

? public java.util.Date getChargeTimeBegin() {
??? return chargeTimeBegin;
? }

? public void setChargeTimeBegin(java.util.Date chargeTimeBegin) {
??? this.chargeTimeBegin = chargeTimeBegin;
? }

? public java.util.Date getChargeTimeEnd() {
??? return chargeTimeEnd;
? }

? public void setChargeTimeEnd(java.util.Date chargeTimeEnd) {
??? this.chargeTimeEnd = chargeTimeEnd;
? }
}


TotalChargeDaoImpl.java 代码:
package test.dao.impl;

import java.util.*;
import test.bean.*;
import test.dao.*;
import net.sf.hibernate.*;

/**
?*作者:孙星
?**/
public class TotalChargeDaoImple extends TotalChargeDao{
????
????//下面方法集成自TotalChargeDao
????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;
????}
}

热点排行