hibernate3学习笔记(七) Criteria Queries - 编程入门网
作者 佚名技术
来源 NET编程
浏览
发布时间 2012-06-20
hibernate3学习笔记(七) Criteria Queries时间:2011-02-02 csdn博客 沈斌仍然接着前面的例子来讲: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(it |
凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢! |
你可能对下面的文章感兴趣
关于hibernate3学习笔记(七) Criteria Queries - 编程入门网的所有评论