hql语句集

 

常用的HQL语句

Java代码

  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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值