常用的Hql语句

来源:互联网 发布:奥飞数据 ipo 编辑:程序博客网 时间:2024/05/22 15:48
  1. <SPAN style="FONT-SIZE: small">1.hql更新   
  2.    String hql = "update PhUser set realName=?";   
  3.    int row=this.getSession().createQuery(hql).setString(0"小李想").executeUpdate();   
  4.    PhUser 类名    
  5. 2.hql删除   
  6.    String hql = "delete PhUser a where a.userId=2";   
  7.    int row=this.getSession().createQuery(hql).executeUpdate();   
  8.    还有个这种的格式:   
  9.    final String hql = "delete PhRoleFunction as a where a.roleId = "  
  10.      + roleId;   
  11.    this.getHibernateTemplate().execute(new HibernateCallback() {   
  12.     public Object doInHibernate(Session session)   
  13.       throws HibernateException, SQLException {   
  14.      return session.createQuery(hql).executeUpdate();   
  15.     }   
  16.    });更新也可以写成这样的格式   
  17. 3.hql单表查询   
  18.    String hql = "from PhUser a where a.userId=" + userId;   
  19.    List list = this.getHibernateTemplate().find(hql);   
  20. 4.hql多表查询   
  21.    (1)String hql = "select new map(a.CUId as CUId,a.unitName as unitName,b.CUFId as CUFId,b.UFName as UFName) from PhCorrelativeUnit a,PhCorrelativeUnitFunction b where a.CUId=b.CUId";   
  22.    List list = this.getHibernateTemplate().find(hql);   
  23.    多个表的字段放到map中,map的键值就是as后面的别名,如果没有as就是字段名   
  24.    (2) String hql = "select new com.phantom.appeal.action.bean.DealPaper(a.id as id,a.billId as billId,a.state as     state,a.creator as creator,a.createtime as createtime ,b.eventContent as eventContent ,c.realName as     realName,b.billCode as billCode,b.citName as citName ) from PhDealBill a,PhAcceptBill b,PhUser c where    a.departmentId="+ billid+ " and a.state=0 and a.billId=b.billId and a.creator =c.userId order by a.billId";   
  25.     return this.getHibernateTemplate().find(hql);   
  26.    另外就是写一个类,对应你要查询的字段,这里的类名是new com.phantom.appeal.action.bean.DealPaper,里面对应查询的字段名   
  27. 5.得到记录数   
  28.    String hql = "select count(*) from PhUser";   
  29.    List list = this.getHibernateTemplate().find(hql);   
  30.    return ((Long) list.get(0)).intValue();   
  31. </SPAN>  
原创粉丝点击