六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 48|回复: 0

常用的HQL语句

[复制链接]

升级  50%

5

主题

5

主题

5

主题

童生

Rank: 1

积分
25
 楼主| 发表于 2013-1-27 05:06:25 | 显示全部楼层 |阅读模式
1.hql更新   String hql = "update PhUser set realName=?";   int row=this.getSession().createQuery(hql).setString(0, "小李想").executeUpdate();   PhUser 类名 2.hql删除   String hql = "delete PhUser a where a.userId=2";   int row=this.getSession().createQuery(hql).executeUpdate();   还有个这种的格式:   final String hql = "delete PhRoleFunction as a where a.roleId = "     + roleId;   this.getHibernateTemplate().execute(new HibernateCallback() {    public Object doInHibernate(Session session)      throws HibernateException, SQLException {     return session.createQuery(hql).executeUpdate();    }   });更新也可以写成这样的格式3.hql单表查询   String hql = "from PhUser a where a.userId=" + userId;   List list = this.getHibernateTemplate().find(hql);4.hql多表查询   (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";   List list = this.getHibernateTemplate().find(hql);   多个表的字段放到map中,map的键值就是as后面的别名,如果没有as就是字段名   (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";    return this.getHibernateTemplate().find(hql);   另外就是写一个类,对应你要查询的字段,这里的类名是new com.phantom.appeal.action.bean.DealPaper,里面对应查询的字段名5.得到记录数   String hql = "select count(*) from PhUser";   List list = this.getHibernateTemplate().find(hql);   return ((Long) list.get(0)).intValue(); 
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表