在没有使用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文件,这也就失去意义了。
即:可以将查询出来的属性组建成一个新的类,如:
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;
}
}
//下面方法集成自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;
}
}