以下代码对Criteria查询做了一些补充。代码内部有注释。
- package com.shiryu.otm;
- import java.util.Iterator;
- import java.util.List;
- import org.hibernate.Criteria;
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- import org.hibernate.Transaction;
- import org.hibernate.cfg.Configuration;
- import org.hibernate.criterion.DetachedCriteria;
- import org.hibernate.criterion.Expression;
- import org.hibernate.criterion.Order;
- import org.hibernate.criterion.Projections;
- import org.hibernate.criterion.Subqueries;
- public class Test2 {
- public static void main(String args[]) {
- SessionFactory factory = new Configuration().configure().buildSessionFactory();
- Session session = factory.openSession();
- Transaction tr = session.beginTransaction();
- // Criteria查询语句的补充
- // 一、复合查询
- // 在这个一对多的例子中。我们使用复合查询,查询所有用户及其地址。
- // Criteria criteria = session.createCriteria(User.class);
- // List list = criteria.list();
- //
- // for (int i = 0; i < list.size(); i++) {
- // User user = (User) list.get(i);
- // System.out.println("\t user:"+i+" name is:" + user.getName());
- //
- // Set addrSet = user.getAddresses();
- // Iterator it = addrSet.iterator();
- // while(it.hasNext()){
- // Address addr = (Address)it.next();
- // System.out.println("\t\t user:"+i+" address is:"+addr.getAddress());
- // }
- // }
- // 通过Criteria查询条件,我们可以查询位于上海的用户
- // Criteria criteria = session.createCriteria(User.class);
- //
- // Criteria addCriteria =
- // criteria.createCriteria("addresses");//这里addresses和User类对应
- // addCriteria.add(Expression.like("address",
- // "%shanghai%"));//这里构造新的Criteria查询过滤条件
- // List list = criteria.list();
- //
- // for (int i = 0; i < list.size(); i++) {
- // User user = (User) list.get(i);
- // System.out.println("\t user:" + i + " name is:" + user.getName());
- //
- // Set addrSet = user.getAddresses();
- // Iterator it = addrSet.iterator();
- // while (it.hasNext()) {
- // Address addr = (Address) it.next();
- // System.out.println("\t\t user:" + i + " address is:"
- // + addr.getAddress());
- // }
- // }
- /*
- * 二、DetachedCriteria的使用 hibernate2 中critria
- * 生命周期位于宿主Session生命周期之内,session创建criteria实例,Session被销毁,critria实例也随之失效
- * 这很大程度上限制了criteria的重用hibernate3 中提供了DetachedCriteria
- * 它可以脱离Session实例独立存在,我们可以把通用的criteria查询条件进行抽离,更好的实现代码重用
- */
- DetachedCriteria deCriteria = DetachedCriteria.forClass(User.class);
- deCriteria.add(Expression.eq("name", "zhaiyu"));
- deCriteria.add(Expression.eq("age", new Integer(21)));
- Criteria criteria = deCriteria.getExecutableCriteria(session);// 在其需要的时候与session进行绑定
- Iterator it = criteria.list().iterator();
- while (it.hasNext()) {
- User user = (User) it.next();
- System.out.println(user.getName());
- }
- /*
- * DetachedCriteria 也可以用于子查询表达 下例返回了年龄超过了平均值的用户记录
- */
- DetachedCriteria avgAge = DetachedCriteria.forClass(User.class);
- avgAge.setProjection(Projections.avg("age"));
- Criteria criteria2 = session.createCriteria(User.class);
- criteria2.add(Subqueries.propertyGt("age", avgAge));
- List list = criteria2.list();
- // 我们可以将DetachedCriteria纳入查询表达式 :select ... from User where age > (select
- // avg(age) from User)
- // (select avg(age) from User) 由DetachedCriteria avgAge描述
- /* 三、Criteria查询语句的高级特性 */
- // 限定返回记录范围
- Criteria criteria3 = session.createCriteria(User.class);
- // 限定从第100条开始的20条记录
- criteria3.setFirstResult(100);
- criteria3.setMaxResults(20);
- // 对记录进行排序
- Criteria criteria4 = session.createCriteria(User.class);
- criteria4.add(Expression.eq("id", new Integer(2)));
- criteria4.addOrder(Order.asc("name"));
- criteria4.addOrder(Order.desc("id"));
- /* Criteria分组与统计 */
- Criteria criteria5 = session.createCriteria(User.class);
- criteria5.setProjection(Projections.groupProperty("age"));
- Iterator it3 = criteria5.list().iterator();
- while (it.hasNext()) {
- System.out.println(it.next());
- }
- }
- }
【编辑推荐】