hibernate多表查询

本文探讨了HQL语言中特定语法的应用,特别是在构建新类和处理查询结果方面。通过示例展示了如何利用HQL进行高效的数据筛选及聚合操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在没有使用Select的时候,返回的是一个 Student对象,否则返回的是一个对象数组( Object[])

IList userList=session.Find (" from testMSSql.student as student where student.Name=?","张三", NHibernate.NHibernate.String);

IList userList=session.Find ("select s.Name,t.TeacherName from student  s left outer join s.Teacher as t where t.TeacherName='ee' ");

 
在HQL语言中有这样的一个语法:   NEW className OPEN selectedPropertiesList CLOSE 
即:可以将查询出来的属性组建成一个新的类,如:
select  new StudentInfo(s.Name,t.TeacherName) from student  s left outer join s.Teacher as t where t.TeacherName='ee' ,但是我再尝试的过程中发现新的类必须是持久的,也就是说必须实现StudentInfo及其hbm.xml文件,这也就失去意义了。
 
 
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;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值