程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> hibernate3學習筆記(七) Criteria Queries

hibernate3學習筆記(七) Criteria Queries

編輯:關於JAVA

仍然接著前面的例子來講:http://www.bianceng.cn/Programming/Java/201101/23637.htm

示例代碼如下:

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

運行結果如下:

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_.age<?
id 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();

這個涉及到對象的關聯映射,在後續章節中還會著重講述。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved