首页 > 代码库 > Hibernate中createCriteria(QBC)查询的用法

Hibernate中createCriteria(QBC)查询的用法

现在假设有一个Student类,内有id,name,age属性 
String hql = "from Student s"
按照以前的做法,我们通常是 
Query query = session.createQuery(hql); 
或者要按照条件检索的话. 
String hql = "from Student s where s.name like ‘王%‘" 
Query query = session.createQuery(hql); 
不用HQL而使用QBC的话,那么代码为: 
Criteria criteria =session.createCriteria(Student.class); 
Criterion criterion = Expression.like("name","王%"); 
这样还看不出来.那我们把检索条件加上年龄为22. 
HQL: 
String hql = "from Student s where s.name like ‘王%‘ and s.age= 22 "
Query query = session.createQuery(hql); 
List list = query.list(); 
QBC: 
Criteria criteria =session.createCriteria(Student.class); 
Criterion criterion1 = Expression.like("name","王%"); 
Criterion criterion2 = Expression.eq("age",newInteger(22)); 
criteria.add(criterion1).add(criterion2); 
List list = criteria.list(); 


看上去烦琐很多.但是做过项目的人都知道,当一个模块业务逻辑发生改变的时候,往往要重写sql,最烦也是最讨厌的就是拿着别人的hql或者sql,两眼瞪的溜园找到底要修改什么地方呢? 
如果使用QBC大大的增加了代码的可读性,以及可维护性. 
需要注意的是null值 
比如我们要查找姓名为null的Student对象时应该这么写 
Criteria criteria =session.createCriteria(Student.class); 
Criterion criterion = Expression.isNull("name"); 
criteria.add(criterion).list(); 
以及使用between...and的时候 
Criteria criteria =session.createCriteria(Student.class); 
Criterion criterion1 = Expression.ge("age",new Integer(20));//下限 
Criterion criterion2 = Expression.le("age",new Integer(25));//上限 
//这里也可以把上述两个条件添加到第三个条件里 
Criterion criterion3 =Expression.and(criterion1,criterion2); 
criteria.add(criterion3).list(); 
相当于from Student s where s.age between 20 and 25 
等同于from Student s where s.age >= 20 and s.age <=25 


下面是就HQL和QBC常用的查询条件做的比较 
表达式含义                                     HQL                                     QBC 
大于等于                                         >=                                        Expression.ge() 
大于                                                 >                                         Expression.gt() 
小于等于                                         <=                                        Expression.le() 
小于                                                 <                                         Expression.lt() 
等于                                                 =                                         Expression.eq() 
不等于                                             <>或者!=                            Expression.ne()    

为空                                                 is null                             Expression.isNull() 
不为空                                             is notnull                        Expression.isNotNull() 
在指定范围内                                 betweenand                        Expression.between() 
不在指定范围                                 not betweenand                Expression.not(Expression.between()) 
属于某个集合                                 in                                        Expression.in() 
不属于某个集合                             notin                                 Expression.not(Expression.in()) 
与                                                    and                                     Expression.and() 
或                                                    or                                        Expression.or() 
非                                                    not                                     Expression.not() 
模糊查询                                        like                                    Expression.like

Hibernate中createCriteria(QBC)查询的用法