hql語句集

 

常用的HQL語句<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Java代碼 <?xml:namespace prefix = v ns = "urn:schemas-microsoft-com:vml" />

  1. <SPAN style="FONT-SIZE: small">
  2. 1.hql更新   
  3.    String hql = "update PhUser set realName=?";   
  4.    int row=this.getSession().createQuery(hql).setString(0, "小李想").executeUpdate();   
  5.    PhUser 類名    
  6. 2.hql刪除   
  7.    String hql = "delete PhUser a where a.userId=2";   
  8.    int row=this.getSession().createQuery(hql).executeUpdate();   
  9.    還有個這種的格式:   
  10.    final String hql = "delete PhRoleFunction as a where a.roleId = "  
  11.      + roleId;   
  12.    this.getHibernateTemplate().execute(new HibernateCallback() {   
  13.     public Object doInHibernate(Session session)   
  14.       throws HibernateException, SQLException {   
  15.      return session.createQuery(hql).executeUpdate();   
  16.     }   
  17.    });更新也可以寫成這樣的格式   
  18. 3.hql單表查詢   
  19.    String hql = "from PhUser a where a.userId=" + userId;   
  20.    List list = this.getHibernateTemplate().find(hql);   
  21. 4.hql多表查詢   
  22.    (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";   
  23.    List list = this.getHibernateTemplate().find(hql);   
  24.    多個表的字段放到map,map的鍵值就是as後面的別名,如果沒有as就是字段名   
  25.    (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";   
  26.     return this.getHibernateTemplate().find(hql);   
  27.    另外就是寫一個類,對應你要查詢的字段,這裏的類名是new com.phantom.appeal.action.bean.DealPaper,裏面對應查詢的字段名   
  28. 5.得到記錄數   
  29.    String hql = "select count(*) from PhUser";   
  30.    List list = this.getHibernateTemplate().find(hql);   
  31.    return ((Long) list.get(0)).intValue();   
  32. </SPAN>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章