日期:2014-05-16 浏览次数:20488 次
hql的查询方法有很多种,昨天接触到了一种基于pojo类的查询方法总结于此:
平时我们可以用简单的"FROM [POJO OBJ]"语法来拼装简单的hql查询。如果我们要指定字段的话可以用new一个pojo对象的语法,如下所示:
以下文章用WeiboAccountBean来代指POJO对象:
Select new WeiboAccountBean(bean.accountid,bean.expire,bean.token,bean.companyid,bean.userid,bean.accountemail,bean.accountname,bean.accountpassword,bean.status,bean.openUrl,bean.isenable,bean.source) from WeiboAccountBean bean where 1=1
该hql可以用session.createQuery方法产生的Query对象来执行list方法,如下所示:
Query query = this.getHibernateTemplate().getSessionFactory().getCurrentSession().createQuery(hql); query.list();
该POJO类必须有如下的构造方法与之配合:
    public    WeiboAccountBean(Integer   accountid,String   expire,String  token,Integer  companyid,String   userid,String  accountemail,String  accountname,String  accountpassword,Integer   status,String openUrl,String isenable,String source){
    	  this.accountid=accountid;
    	  this.expire=expire;
    	  this.token=token;
    	  this.companyid=companyid;
    	  this.userid=userid;
    	  this.accountemail=accountemail;
    	  this.accountname=accountname;
    	  this.accountpassword=accountpassword;
    	  this.status=status;
    	  this.openUrl = openUrl;
    	  this.isenable = isenable;
    	  this.source = source;
      }