hibernate3学习笔记(七)|Criteria Queries

 仍然接着前面的例子来讲:http://blog.csdn.net/kunshan_shenbin/archive/2008/09/02/2864883.aspx

示例代码如下:

  1. package com.hb3.pack_01;
  2. import java.util.Iterator;
  3. import java.util.List;
  4. import org.hibernate.Criteria;
  5. import org.hibernate.Hibernate;
  6. import org.hibernate.Session;
  7. import org.hibernate.SessionFactory;
  8. import org.hibernate.cfg.Configuration;
  9. import org.hibernate.criterion.Example;
  10. import org.hibernate.criterion.Order;
  11. import org.hibernate.criterion.ProjectionList;
  12. import org.hibernate.criterion.Projections;
  13. import org.hibernate.criterion.Property;
  14. import org.hibernate.criterion.Restrictions;
  15. import org.hibernate.type.Type;
  16. import com.hb3.pack_01.model.User;
  17. public class BusinessService {
  18.     public static void main(String[] args) {
  19.         
  20.         Configuration config = new Configuration().configure();
  21.         SessionFactory sessionFactory = config.buildSessionFactory();      
  22.         Session session = sessionFactory.openSession();
  23.         
  24.         Criteria criteria = session.createCriteria(User.class);
  25.         criteria.addOrder(Order.asc("age"));
  26.         List users = criteria.list();
  27.         printUserInfo(users);
  28.         
  29.         criteria = session.createCriteria(User.class);
  30.         criteria.add(Restrictions.gt("age"new Integer(22)));
  31.         criteria.add(Restrictions.lt("age"new Integer(27)));
  32.         users = criteria.list();
  33.         printUserInfo(users);
  34.         
  35.         criteria = session.createCriteria(User.class);
  36.         criteria.add(Restrictions.or( 
  37.                         Restrictions.eq("age"new Integer(23)), 
  38.                         Restrictions.isNull("age"
  39.                     )); 
  40.         users = criteria.list();
  41.         printUserInfo(users);
  42.         
  43.         criteria = session.createCriteria(User.class);
  44.         criteria.add(Restrictions.sqlRestriction("{alias}.name LIKE (?)""%ya%", Hibernate.STRING));
  45.         users = criteria.list();
  46.         printUserInfo(users);
  47.         
  48.         criteria = session.createCriteria(User.class);
  49.         Integer[] ages = {new Integer(26), new Integer(28)};
  50.         Type[] types = {Hibernate.INTEGER, Hibernate.INTEGER};
  51.         criteria.add(Restrictions.sqlRestriction("{alias}.age BETWEEN (?) AND (?)", ages, types));
  52.         users = criteria.list();
  53.         printUserInfo(users);
  54.         
  55.         criteria = session.createCriteria(User.class);
  56.         criteria.setFirstResult(3); 
  57.         criteria.setMaxResults(2);
  58.         users = criteria.list();
  59.         printUserInfo(users);
  60.         
  61.         criteria = session.createCriteria(User.class);
  62.         criteria.setProjection(Projections.rowCount());
  63.         users = criteria.list();
  64.         Iterator iterator = users.iterator();
  65.         while (iterator.hasNext()) {
  66.             System.out.println(iterator.next());
  67.         }
  68.         
  69.         criteria = session.createCriteria(User.class);
  70.         criteria.setProjection(Projections.avg("age"));
  71.         users = criteria.list();
  72.         iterator = users.iterator();
  73.         while (iterator.hasNext()) {
  74.             System.out.println(iterator.next());
  75.         }
  76.         
  77.         criteria = session.createCriteria(User.class);
  78.         criteria.setProjection(Projections.groupProperty("age"));
  79.         users = criteria.list();
  80.         iterator = users.iterator();
  81.         while (iterator.hasNext()) {
  82.             System.out.println(iterator.next());
  83.         }
  84.         
  85.         ProjectionList projectionList = Projections.projectionList();
  86.         projectionList.add(Projections.groupProperty("age"));
  87.         projectionList.add(Projections.groupProperty("name"));
  88.         projectionList.add(Projections.rowCount());
  89.         criteria = session.createCriteria(User.class);
  90.         criteria.setProjection(projectionList);
  91.         users = criteria.list();
  92.         iterator =  users.iterator();
  93.         while(iterator.hasNext()) {
  94.             Object[] o = (Object[]) iterator.next();
  95.             System.out.println(o[0] + "/t" + o[1] + "/t" + o[2]);
  96.         }
  97.         
  98.         criteria = session.createCriteria(User.class);
  99.         criteria.add(Property.forName("name").like("%ya%"));
  100.         criteria.addOrder(Property.forName("age").desc());
  101.         users = criteria.list();
  102.         printUserInfo(users);
  103.         
  104.         User user = new User();
  105.         user.setAge(new Integer(26));
  106.         criteria = session.createCriteria(User.class);
  107.         criteria.add(Example.create(user));
  108.         users = criteria.list();
  109.         printUserInfo(users);
  110.         
  111.         session.close();
  112.         sessionFactory.close();
  113.     }
  114.     
  115.     public static void printUserInfo(List users){
  116.         
  117.         Iterator iterator = users.iterator();
  118.         System.out.println("id /t name/age");
  119.         while (iterator.hasNext()) {
  120.             User user = (User) iterator.next();
  121.             System.out.println(user.getId() + " /t " + user.getName() + "/" + user.getAge());
  122.         }
  123.     }
  124. }

运行结果如下:

15:41:36,312  WARN ConfigurationFactory:127 - No configuration found. Configuring ehcache from ehcache-failsafe.xml  found in the classpath: jar:file:/D:/Java/MyEclipse%206.0/workspace/hb3demo/ehcache-1.2.3.jar!/ehcache-failsafe.xml
15:41:36,750  WARN EhCacheProvider:93 - Could not find configuration [org.hibernate.cache.UpdateTimestampsCache]; using defaults.
15:41:36,781  WARN EhCacheProvider:93 - Could not find configuration [org.hibernate.cache.StandardQueryCache]; using defaults.
Hibernate: select this_.id as id0_0_, this_.name as name0_0_, this_.age as age0_0_ from user this_ order by this_.age asc
id   name/age
11   yangye/null
12   yangye/null
9   yangye/23
10   yangye/23
2   chenyan/26
1   shenbin/28
Hibernate: select this_.id as id0_0_, this_.name as name0_0_, this_.age as age0_0_ from user this_ where this_.age>? and this_.ageid   name/age
2   chenyan/26
9   yangye/23
10   yangye/23
Hibernate: select this_.id as id0_0_, this_.name as name0_0_, this_.age as age0_0_ from user this_ where (this_.age=? or this_.age is null)
id   name/age
9   yangye/23
10   yangye/23
11   yangye/null
12   yangye/null
Hibernate: select this_.id as id0_0_, this_.name as name0_0_, this_.age as age0_0_ from user this_ where this_.name LIKE (?)
id   name/age
2   chenyan/26
9   yangye/23
10   yangye/23
11   yangye/null
12   yangye/null
Hibernate: select this_.id as id0_0_, this_.name as name0_0_, this_.age as age0_0_ from user this_ where this_.age BETWEEN (?) AND (?)
id   name/age
1   shenbin/28
2   chenyan/26
Hibernate: select this_.id as id0_0_, this_.name as name0_0_, this_.age as age0_0_ from user this_ limit ?, ?
id   name/age
10   yangye/23
11   yangye/null
Hibernate: select count(*) as y0_ from user this_
6
Hibernate: select avg(this_.age) as y0_ from user this_
25.0
Hibernate: select this_.age as y0_ from user this_ group by this_.age
null
23
26
28
Hibernate: select this_.age as y0_, this_.name as y1_, count(*) as y2_ from user this_ group by this_.age, this_.name
null yangye 2
23 yangye 2
26 chenyan 1
28 shenbin 1
Hibernate: select this_.id as id0_0_, this_.name as name0_0_, this_.age as age0_0_ from user this_ where this_.name like ? order by this_.age desc
id   name/age
2   chenyan/26
9   yangye/23
10   yangye/23
11   yangye/null
12   yangye/null
Hibernate: select this_.id as id0_0_, this_.name as name0_0_, this_.age as age0_0_ from user this_ where (this_.age=?)
id   name/age
2   chenyan/26

 

注意:

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限定查询

 

此外,Criteria还可以进行复合查询。即在原有的查询基础上再进行查询,例如在Room对User的一对多关系中,在查询出所有的Room資料之后,希望再查询users中"age"为30的user資料:

  1. Criteria roomCriteria = session.createCriteria(Room.class);
  2. Criteria userCriteria = roomCriteria.createCriteria("users");
  3. userCriteria.add(Restrictions.eq("age"new Integer(30)));
  4. List rooms = roomCriteria.list(); // 只列出users属性中有user之"age"为30的Room
  5. Iterator iterator = rooms.iterator();

这个涉及到对象的关联映射,在后续章节中还会着重讲述。

你可能感兴趣的:(Java相关)