Restrictions的部分用法
来源:互联网 发布:破解qq悄悄话的软件 编辑:程序博客网 时间:2024/05/16 14:54
方法说明
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关係
criteria.addOrder(Order.asc("age"));排序
criteria.setFirstResult(51);
criteria.setMaxResults(50);分页
org.hibernate.criterion.Projections的avg()、rowCount()、count()、max()、min()、 countDistinct()等方法,再搭配Criteria的setProjection()方法加入条件设定
criteria.setProjection(Projections.avg("age"));求age的平均值
criteria.setProjection(Projections.groupProperty("age"));分组
ProjectionList projectionList = Projections.projectionList();
projectionList.add(Projections.groupProperty("age"));
projectionList.add(Projections.rowCount());
Criteria criteria = session.createCriteria(User.class);
criteria.setProjection(projectionList);
List users = criteria.list(); 同时使用分组和统计功能
Criteria criteria = session.createCriteria(User.class);
criteria.add(Example.create(user));使用已知的实体查询
criteria.add(Restrictions.sqlRestriction("User.name LIKE(?)", "cater%", Hibernate.STRING));sql传参查询
Integer[] ages = {new Integer(20), new Integer(40)};
Type[] types = {Hibernate.INTEGER, Hibernate.INTEGER};
criteria.add(Restrictions.sqlRestriction("User.age BETWEEN (?) AND(?)", ages, types));sql多个条件查询Between查询
使用DetchedCriteria
Criteria与Session绑定,其生命週期跟随着Session结束而结束,使用Criteria时进行查询时,每次都要于执行时期动态建立物件,并加入各种查询条件,随着Session的回收,Criteria也跟着回收。
为了能够重複使用Criteria物件,在Hibernate 3中新增了org.hibernate.criterion.DetchedCriteria,您可以先建立DetchedCriteria实例,并加入各种查询条件,并于需要查询时再与Session绑定,获得一个绑定Session的Criteria物件,例如:
// 先建立DetchedCriteria物件
DetachedCriteria detchedCriteria = DetachedCriteria.forClass(User.class);
// 加入查询条件
detchedCriteria.add(Restrictions.ge("age",new Integer(25)));
Session session = sessionFactory.openSession();
// 绑定Session并返回一个Criteria实例
Criteria criteria = detchedCriteria.getExecutableCriteria(session);
List users = criteria.list();
- Restrictions的部分用法
- Hibernate 的Restrictions用法
- Hibernate 的Restrictions用法
- hibernatmate Restrictions 的用法:
- 常见的Restrictions用法
- Hibernate的Restrictions用法
- hibernate的Restrictions的用法
- hibernate中Restrictions的用法
- hibernate里面的Restrictions用法
- Restrictions用法
- Restrictions用法
- Restrictions用法
- Restrictions用法
- Restrictions用法
- Restrictions用法
- Restrictions用法
- Restrictions用法
- Restrictions用法
- [转]AFNetWorking3
- C# 读取 Oracle 数据库表的例子 SALE_REPORT
- 为什么安卓没有IOS流畅?看完你就明白了
- 用wireshark抓包分析tcp
- input内的文件水平剧中和垂直居中
- Restrictions的部分用法
- Implement Trie (Prefix Tree)
- [Java] 集合类克隆方法与内部排序
- dom、sax、pull解析的优缺点
- 横竖屏事件响应(viewWillLayoutSubviews和通知)两种方式
- nginx优化 突破十万并发
- online_judge_1093
- QT显示中文
- 内核中断,异常,抢占总结篇