Hibernate的几种查询方式-HQL,QBC,QBE,离线查询,复合查询,分页查询
转自:http://blog.csdn.net/iijse/article/details/6161143
HQL查询方式
这一种我最常用,也是最喜欢用的,因为它写起来灵活直观,而且与所熟悉的SQL的语法差不太多。条件查询、分 页查询、连接查询、嵌套查询,写起来与SQL语法基本一致,唯一不同的就是把表名换成了类或者对象。其它的,包括一些查询函数 (count(),sum()等)、查询条件的设定等,全都跟SQL语法一样。
示例:
1: Session session = SessionFactory.getCurrentSession();
2: User user = null;
3: Transaction ts = session.beginTransaction();
4: try {
5: Query query = session.createQuery("from User as u where name=‘ijse‘");
6: user= (User) query.list().get(0);
7: session.commit();
8: } catch (HibernateException ex) {
9: ts.rollBack();
10: ex.printStackTrace();
11: }
12: System.out.println(user.getName());
QBC(Query By Criteria) 查询方式
这种方式比较面向对象方式,重点是有三个描述条件的对象:Restrictions,Order,Projections。使用QBC查询,一般需要以下三个步骤:
- 使用Session实例 的createCriteria()方法创建Criteria对象
- 使用工具类Restrictions的方法为Criteria对象设置查询条件,Order工具类的方法设置排序方式,Projections工具类的方法进行统计和分组。
- 使用Criteria对象的list()方法进行查询并返回结果
Restrictions类的常用方法:
Order类的常用方法:
方法名称 |
描述 |
Order.asc | 升序 |
Order.desc | 降序 |
Projections类的常用方法
示例:
1: Session session = SessionFactory.getCurrentSession();
2: User user = null;
3: Transaction ts = session.beginTransaction();
4: try {
5: Criteria criteria = session.createCriteria(User.class);
6: criteria.add(Restrictions.eq("name","ijse"));
7: user= (User) criteria.list().get(0);
8: session.commit();
9: } catch (HibernateException ex) {
10: ts.rollBack();
11: ex.printStackTrace();
12: }
13: System.out.println(user.getName());
QBE(Query By Example)查询方式
将一个对象的非空属性作为查询条件进行查询。
示例:
1: Session session = SessionFactory.getCurrentSession();
2: User user = new User();
3: user.setName("ijse");
4: Transaction ts = session.beginTransaction();
5: try {
6: Criteria criteria = session.createCriteria(User.class);
7: criteria.add(Example.create(user));
8: user= (User) criteria.list().get(0);
9: session.commit();
10: } catch (HibernateException ex) {
11: ts.rollBack();
12: ex.printStackTrace();
13: }
14: System.out.println(user.getName());
离线查询
离线查询就是建立一个DetachedCriteria对象,将查询的条件等指定好,然后在session.beginTransaction()后将这个对象传入。通常这个对象可以在表示层建立,然后传入业务层进行查询。
示例:
1: DetachedCriteria detachedCriteria = DetachedCriteria.forClass(User.class);
2: detachedCriteria.add(Restrictions.eq("name","ijse");
3:
4: Session session = SessionFactory.getCurrentSession();
5: User user = new User();
6: Transaction ts = session.beginTransaction();
7: try {
8: Criteria criteria = detachedCriteria.getExecutableCriteria(session);
9: user= (User) criteria.list().get(0);
10: session.commit();
11: } catch (HibernateException ex) {
12: ts.rollBack();
13: ex.printStackTrace();
14: }
15: System.out.println(user.getName());
复合查询
复合查询就是在原有查询的基础上再进行查询,可以调用Criteria对象的createCriteria()方法在这个Criteria对象的基础上再进行查询。
示例:
1: Session session = SessionFactory.getCurrentSession();
2: User user = new
3: User();
4: Transaction ts = session.beginTransaction();
5: try
6: {
7: Criteria criteria1 =
8: session.createCriteria(Room.class);
9: Criteria
10: criteria2 =criterial1.createCriteria("User");
11:
12: criteria2.add(Restrictions.eq("name",new String("ijse"));
13:
14: user= (User) criteria.list().get(0);
15:
16: session.commit();
17: } catch (HibernateException ex) {
18:
19: ts.rollBack();
20:
21: ex.printStackTrace();
22: }
23: System.out.println(user.getName());
分页查询
分页查询主要是要指定两个参数:从第几条数据开始,取多少条数据。可以通过调用Query或者Criteria对象的setFirstResult()和setMaxResults()方法分别进行设定。
示例:
1: Session session = SessionFactory.getCurrentSession();
2: List userList = null;
3: Transaction ts = session.beginTransaction();
4: try {
5: Criteria criteria = session.createCriteria(User.class);
6: criteria.setFirstResult(0);//从第一个数据开始
7: criteria.setMaxResults(10);//取10条记录
8: userList = (List) criterial.list();
9: session.commit();
10: } catch (HibernateException ex) {
11: ts.rollBack();
12: ex.printStackTrace();
13: }
实践(shz)
protected String perform() throws Exception {
try {
Date today = new Date();
Date yesterday = DateUtil.addDays(today, -1);
summerStartDate = DateUtil.getBeginSummerDate(yesterday);
summerEndDate = DateUtil.getEndSummerDate(today);
winterStartDate = DateUtil.getBeginWinterDate(yesterday);
winterEndDate = DateUtil.getEndWinterDate(today);
if (ShzStringUtil.isNullOrEmpty(perform)) { // 默认进入该页面时调用
if (searchCriteria == null) {
searchCriteria = new ForexAgentReportCriteria();
}
searchCriteria.setStartDate(winterStartDate);
searchCriteria.setEndDate(winterEndDate);
}
if (StringUtils.isNotEmpty(perform) && ShzConstants.PERFORM_SEARCH.equals(perform)) { // 下为查询的时候调用
final DetachedCriteria detachedCriteria = DetachedCriteria.forClass(PositionTrans.class);
/*********** --------------------查询条件 start---------------- **************/
// executeTime
CriteriaBuilder.buildRange(
detachedCriteria,
PositionTrans.class,
"executeTime",
searchCriteria.getStartDate(),
searchCriteria.getEndDate());
// tradeNo
if (StringUtils.isNotEmpty(searchCriteria.getTradeNo())) {
detachedCriteria.add(Restrictions.eq("tradeNo", searchCriteria.getTradeNo()));
}
// code
if (!GwStringUtil.isNullOrEmpty(searchCriteria.getCode())) {
detachedCriteria.add(Restrictions.eq("code", searchCriteria.getCode()));
}
// platform
searchCriteria.setSortByFields("agentNo", "executeTime");
searchCriteria.setSortOrders(GwConstants.ASC, GwConstants.DESC);
/*********** --------------------查询条件 end---------------- **************/
searchService.find(searchCriteria, detachedCriteria, super.getLocale(), true);
}
} catch (Exception e) {
logger.error("", e);
throw e;
}
return SUCCESS;
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。