Hibernate的Criteria用法
用法很简单,首先传Class实例创建Criteria,Class实例对应你想要查询的那个实体:
Criteria c = session.createCriteria(Person.class);
然后调用Criteria的add方法加入条件(Restrictions)。常用的条件有:
方法
說明
Restrictions.eq
等於
Restrictions.allEq
使用Map,使用key/value進行多個等於的比對
Restrictions.gt
大於 >
Restrictions.ge
大於等於 >=
Restrictions.lt
小於 <
Restrictions.le
小於等於 <=
Restrictions.between
對應SQL的BETWEEN子句
Restrictions.like
對應SQL的LIKE子句
Restrictions.in
對應SQL的in子句
Restrictions.and
and關係
Restrictions.or
or關係
Restrictions.sqlRestriction
SQL限定查詢
例如,我想返回Person实体里面年龄(age)大于20的记录,那么就为Criteria加入Restrictions.gt条件:
List list = c.add(Restrictions.gt(“age”,new Integer(20))).list();
Restrictions.and和Restrictions.or很有用,表示and查询还是or查询,例如我要查询Person实体里面年龄(age)大于20,或者性别 (sex)为man
的记录,那么就为Criteria加入:
List list = c.add(Restrictions.or(
Restrictions.gt(“age”,new Integer(20)),
Restrictions.eq(“sex”,”man”)
)).list();
如果需要关联查询的话,可以使用Criteria的createCriteria方法创建一个Criteria,例如部门(Department)和人员(Person)是一对多的关系,我要查询Person实体里面年龄(age)大于20,性别 (sex)为man,
备注包含应届生,
且所在部门(department)是开发部的记录:
List list = c.add(Restrictions.gt(“age”,new Integer(20)))
.add(Restrictions.eq(“sex”,”man”))
.add(Restrictions.like(“Memo”,”%应届生%”))
.createCriteria(“department”)
.add(Restrictions.eq(“departmentname”,”开发部”))
.list();
其他条件的用法也很简单,就不列举了。
另外,Hibernate3出了一个org.hibernate.criterion.DetachedCriteria,即离线Criteria,它允许你可以先创建DetachedCriteria,然后传到Session里面才真正返回一个Criteria,用法如下:
// 先建立DetchedCriteria物件
DetachedCriteria detchedCriteria = DetachedCriteria.forClass(Person.class);
// 加入查詢條件
detchedCriteria.add(Restrictions.ge("age",new Integer(25)));
Session session = sessionFactory.openSession();
// 綁定Session並返回一個Criteria實例
Criteria criteria = detchedCriteria.getExecutableCriteria(session);
List list = criteria.list();
最后,需要注意,Criteria还提供了setFirstResult和setMaxResults方法进行分页查询。
Hibernate的Criteria用法的更多相关文章
- Hibernate之Criteria的完整用法
Criteria的完整用法 QBE (Query By Example) Criteria cri = session.createCriteria(Student.class); cri.add(E ...
- 转:Hibernate中Criteria和DetachedCriteria的完整用法
原文地址:http://blog.sina.com.cn/s/blog_667528fd0100rkrf.html 设计上可以灵活的根据 Criteria 的特点来方便地进行查询条件的组装.现在对 H ...
- Hibernate中Criteria的完整用法2
Criteria的完整用法 QBE (Query By Example) Criteria cri = session.createCriteria(Student.class); cri.add(E ...
- Atitit.Hibernate中Criteria 使用总结and 关联查询 and 按照子对象查询 o9o
Atitit.Hibernate中Criteria 使用总结and 关联查询 and 按照子对象查询 o9o 1. Criteria,,Criterion ,, 1 <2. 主要的对象黑头配置磊 ...
- 1-4 criteria用法大全
Criteria的完整用法 QBE (Query By Example) Criteria cri = session.createCriteria(Student.class); cri.add(E ...
- Atitit.Hibernate于Criteria 使用汇总and 关系查询 and 按照子对象查询 o9o
Atitit.Hibernate于Criteria 使用总结and 关联查询 and 依照子对象查询 o9o 1. Criteria,,Criterion ,, 1 <2. 基本的对象黑头配置磊 ...
- Hibernate 中Criteria Query查询详解【转】
当查询数据时,人们往往需要设置查询条件.在SQL或HQL语句中,查询条件常常放在where子句中.此外,Hibernate还支持Criteria查询(Criteria Query),这种查询方式把查询 ...
- 5 -- Hibernate的基本用法 -- 要点
Hibernate的基本用法 ⊙ ORM的基本知识 ⊙ ORM和Hibernate的关系 ⊙ Hibernate的基本映射思想 ⊙ Hibernate入门知识 ⊙ 使用Eclipse开发Hiberna ...
- 分享知识-快乐自己:Hibernate 中Criteria Query查询详解
1):Hibernate 中Criteria Query查询详解 当查询数据时,人们往往需要设置查询条件.在SQL或HQL语句中,查询条件常常放在where子句中. 此外,Hibernate还支持Cr ...
随机推荐
- 日志管理-将Log4net的配置配置到的独立文件中
转自:http://www.cnblogs.com/zfanlong1314/p/3662679.html使用log4net已经很久了.但从来没有详情了解log4的参数,及具体使用方法.看了周公的博客 ...
- JS delete 用法(删除对象属性及变量)
1,对象属性删除 function fun(){ this.name = 'mm'; } var obj = new fun(); console.log(obj.name);//mm delete ...
- table中td,th不能设置margin
首先,我们需要知道的是:我们可以对表格table设置margin,而不能设置padding;对单元格td设置padding,而不能设置margin.所以说,我们不能对单元格td设置margin属性来调 ...
- MVC4商城项目一:框架设计
代码已托管在 https://code.csdn.net/denghao156/ktnmb_mvc4 先上图,设计模式参考:ddmvc4.codeplex.com 一.unintofwork 设计 ...
- Ubuntu安装与初始配置
转载请注明作者:梦里风林 更多文章查看我的个人站: ahangchen.site 适用于Ubuntu版本 14.04/16.04LTS 64位 先上图 双系统安装 划分空闲磁盘,U盘安装ubuntu ...
- 切图教程,PS和AI切图方法分享
- 四种常见的 POST 提交数据方式--good
HTTP/1.1 协议规定的 HTTP 请求方法有 OPTIONS.GET.HEAD.POST.PUT.DELETE.TRACE.CONNECT 这几种.其中 POST 一般用来向服务端提交数据,本文 ...
- Makefile里调用Shell注意点
http://www.linuxidc.com/Linux/2012-04/59093.htm 大家经常编写和使用Makefile, Makefile里面也经常用到shell, 但对其中一些需要注意的 ...
- hdu 4496 D-City(并查集)
Problem Description Luxer is a really bad guy. He destroys everything he met. One day Luxer went to ...
- OC基础12:数字、字符串和集合1
"OC基础"这个分类的文章是我在自学Stephen G.Kochan的<Objective-C程序设计第6版>过程中的笔记. 1.有时要将一些数字数据类型的值当做对象来 ...