hql

来源:互联网 发布:js正则表达式语法 编辑:程序博客网 时间:2024/06/05 17:01

本文转载自http://www.blogjava.net/focusJ/archive/2011/04/22/367245.html

HQL实例:

1. 查询表中的所有记录:from Category

2. 带有where子句的条件查询:from Category c where c.name > 'c5'

3. 结果根据某一字段排序:from Category c order by c.name desc(desc表示降序排列,asc表示升序排列)

4. 去除重复记录获得单一记录:select distinct c from Category c order by c.name desc

5. 带有参数的查询:from Category c where c.id > :min and c.id < :max。hql语句中’:min’ 表示的是参数,可以像jdbc中一样,为参数赋值。在hql中可以这样,这里也运用了链式编程:

session.createQuery("from Category c where c.id > :min and c.id < :max")

.setInteger("min", 2)

.setInteger("max", 8);

6. 带参数hql查询的另外一种查询:from Category c where c.id > ? and c.id < ?

7. hibernate分页查询 

Query q = session.createQuery("from Category c order by c.name desc");

q.setMaxResults(3);

q.setFirstResult(0);

其中setMaxResult()是设置每页的最大显示量,setFirstResult()是设置其实元素从哪里开始,这里0代表最后一条元素。

8. 多表连接查询:select t.title, c.name from Topic t join t.category c

9. HQL函数:

a) Count():select count(*) from Msg m

b) Max()-min()-avg():select max(m.id), min(m.id), avg(m.id), sum(m.id) from Msg m

c) Between:from Msg m where m.id between 3 and 5

d) In:from Msg m where m.id in (3, 4, 5)

10. Is null;is not null:from Msg m where m.cont is not null

11. Is empty:from Topic t where t.msgs is empty

12. Like:from Topic t where t.title like '%5'。'%'匹配所有字符,'_'匹配单个字符。

13. 

一些功能函数,但是不重要了解即可:select lower(t.title)," +

 "upper(t.title)," +

 "trim(t.title)," +

 "concat(t.title, '***')," +

 "length(t.title)" +

      " from Topic t ")

Trim()是去掉首尾空格,返回字符串的副本,concat()将字符串欲查询出的字符串连接。

14. Abs()-sqrt()-mod():select abs(t.id)," +  "sqrt(t.id)," + "mod(t.id,2)" + " from Topic t 

15. 获取当前的时间:select current_date, current_time, current_timestamp, t.id from Topic t

16. Having子句:select t.title, count(*) from Topic t group by t.title having count(*) <= 1

17. Existfrom Topic t where not exists (select m.id from Msg m where m.topic.id=t.id)

需要注意的一点:in同样可以实现exist的功能,但是exist的执行效率较高。

18. Update的用法:update Topic t set t.title = upper(t.title)

19. hql删除的三种方式:

Hibernate的删除方式:

/*方式一*/
String hql = "select p from Province as p where p.id=?";
Query query = session.createQuery(hql);
query.setString(0, id);
Province p = (Province)query.list().get(0);
session.delete(p);
/*方式二*/
String hql = "delete Province where id=?";
Query query = session.createQuery(hql);
query.setString(0, id);
int x = query.executeUpdate();
if(x>0){
  flag = true;
}
/*方式三*/
Province p = (Province)session.get(Province.class, id);
session.delete(p);

方式一相对比较笨重。

方式二中的Hql语句不要加as + 别名!

方式三是Hibernate自带的方法。

20. 查询表中的某些字段:

方法一:给这个类新建一个构造方法,传进去你想要的参数,然后hql语句可以这样写:

select new Class(c.name, c.date, c.sex) from Class

方法二:用JDBCsql语句:

Session.createSQLQuery(sql);

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 移动预约号码取消怎么办 身份证换地址驾驶证怎么办 刚来成都怎么办居住证 我在外地怎么办身份证 身份证丢在外地怎么办 换领新身份证时旧证丢了怎么办 二代身份证重号怎么办 北京行驶证到期怎么办 北京驾驶证即将过期怎么办 去澳门没有网络怎么办 三个周期未年检怎么办 深圳驾照丢了怎么办 武汉驾照年审过期怎么办 武汉驾照过期了怎么办 科二过不了怎么办 南京身份证到期换新怎么办 过期身份证丢了怎么办 南京驾照过期了怎么办 换驾照身体证明怎么办 学车办理暂住证怎么办 a牌驾照扣分怎么办 b牌驾照扣分怎么办 b驾照扣分了怎么办 考驾照要暂住证怎么办 换驾驶证有色盲怎么办 外籍人员办理就业证怎么办 驾驶证该审过期怎么办 小车证扣满12分怎么办 b证扣满12分怎么办 车过户后保险怎么办 换新轮胎旧轮胎怎么办 驾照一审超一年怎么办 驾驶证年检过期了怎么办 交管12123怎么办进京证 驾照登录密码忘记怎么办 预约密码忘记了怎么办 学驾照密码忘记怎么办 考驾照不识字怎么办 驾校密码忘了怎么办 b证酒驾扣12分怎么办 成都驾照过期了怎么办