日期:2011-03-22 16:12:00 来源:本站整理
hibernate3学习笔记(七) Criteria Queries[Java编程]
本文“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.}
以上是“hibernate3学习笔记(七) Criteria Queries[Java编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
本文地址: | 与您的QQ/BBS好友分享! |
评论内容只代表网友观点,与本站立场无关!
评论摘要(共 0 条,得分 0 分,平均 0 分)
查看完整评论