当查询数据时,人们往往需要设置查询条件。在SQL或HQL语句中,查询条件常常放在where子句中。此外,Hibernate还支持Criteria查询(Criteria Query),这种查询方式把查询条件封装为一个Criteria对象。在实际应用中,使用Session的createCriteria()方法构建一个org.hibernate.Criteria实例,然后把具体的查询条件通过Criteria的add()方法加入到Criteria实例中。这样,程序员可以不使用SQL甚至HQL的情况下进行数据查询,如例程9-1所示。
例程9-1 Criteria应用实例
代码
Criteria cr = session.createCriteria(Student.class); //生成一个Criteria对象
cr.add(Restrictions.eq("name", "Bill"));//等价于where name=’Bill’
List list = cr.list();
Student stu = (Student)list.get(0);
System.out.println(stu.getName());
1.常用的查询限制方法
在例程9-1中,Restrictions.eq()方法表示equal,即等于的情况。Restrictions类提供了查询限制机制。它提供了许多方法,以实现查询限制。这些方法及其他一些criteria常用查询限制方法列于表9-1中。
表9-1 Criteria Query常用的查询限制方法
Method Summary | |
static Criterion | allEq(Map propertyNameValues) |
static Conjunction | and(Criterion... predicates) |
static LogicalExpression | and(Criterion lhs, Criterion rhs) |
static Criterion | between(String propertyName, Object lo, Object hi) |
static Conjunction | conjunction() |
static Disjunction | disjunction() |
static SimpleExpression | eq(String propertyName, Object value) |
static PropertyExpression | eqProperty(String propertyName, String otherPropertyName) |
static SimpleExpression | ge(String propertyName, Object value) |
static PropertyExpression | geProperty(String propertyName, String otherPropertyName) |
static SimpleExpression | gt(String propertyName, Object value) |
static PropertyExpression | gtProperty(String propertyName, String otherPropertyName) |
static Criterion | idEq(Object value) |
static Criterion | ilike(String propertyName, Object value) |
static Criterion | ilike(String propertyName, String value, MatchMode matchMode) |
static Criterion | in(String propertyName, Collection values) |
static Criterion | in(String propertyName, Object[] values) |
static Criterion | isEmpty(String propertyName) |
static Criterion | isNotEmpty(String propertyName) |
static Criterion | isNotNull(String propertyName) |
static Criterion | isNull(String propertyName) |
static SimpleExpression | le(String propertyName, Object value) |
static PropertyExpression | leProperty(String propertyName, String otherPropertyName) |
static SimpleExpression | like(String propertyName, Object value) |
static SimpleExpression | like(String propertyName, String value, MatchMode matchMode) |
static SimpleExpression | lt(String propertyName, Object value) |
static PropertyExpression | ltProperty(String propertyName, String otherPropertyName) |
static NaturalIdentifier | naturalId() |
static SimpleExpression | ne(String propertyName, Object value) |
static PropertyExpression | neProperty(String propertyName, String otherPropertyName) |
static Criterion | not(Criterion expression) |
static Disjunction | or(Criterion... predicates) |
static LogicalExpression | or(Criterion lhs, Criterion rhs) |
static Criterion | sizeEq(String propertyName, int size) |
static Criterion | sizeGe(String propertyName, int size) |
static Criterion | sizeGt(String propertyName, int size) |
static Criterion | sizeLe(String propertyName, int size) |
static Criterion | sizeLt(String propertyName, int size) |
static Criterion | sizeNe(String propertyName, int size) |
static Criterion | sqlRestriction(String sql) |
static Criterion | sqlRestriction(String sql, Object[] values, Type[] types) |
static Criterion | sqlRestriction(String sql, Object value, Type type) |
例1:查询学生名字以t开头的所有Student对象。
Criteria cr = session.createCriteria(Student.class); |
或者使用另一种方式:
Criteria cr = session.createCriteria(Student.class); |
例2:查询学生姓名在Bill, Jack和Tom之间的所有Student对象。
String[] names = {“Bill”, “Jack”, “Tom”} |
例3:查询学生的年龄age等于22或age为空(null)的所有Student对象。
Criteria cr = session.createCriteria(Student.class); |
例4:查询学生姓名以字母F开头的所有Student对象,并按姓名升序排序。
Criteria cr = session.createCriteria(Student.class); |
调用Order.asc的方法应是Criteria的addOrder()方法。
使用add()方法加入条件时,预设是使用and来组合条件,如果要用or的方式来组合条件,则可以使用Restrictions.or()方法,例如结合age等于(eq)20或(or)age为空(isNull)的条件:
Criteria criteria = session.createCriteria(User.class);
criteria.add(Restrictions.or(
Restrictions.eq("age", new Integer(20)),
Restrictions.isNull("age")
));
List users = criteria.list();
观察所产生的SQL语句,将使用where与or子句完成SQL的条件查询:
Hibernate: select this_.id as id0_0_,this_.name as name0_0_, this_.age as age0_0_ from T_USER this_ where(this_.age=? or this_.age is null)
使用Criteria进行查询时,不仅仅能组合出SQL中where子句的功能,还可以组合出如排序、统计、分组等的查询功能。这就是Criteria进阶查询。
排序
您可以使用Criteria进行查询,并使用org.hibernate.criterion.Order对结果进行排序,例如使用Oder.asc(),指定根据”age”由小到大排序(反之则使用desc()):
Criteria criteria = session.createCriteria(User.class);
criteria.addOrder(Order.asc("age"));
List users = criteria.list();
注意在加入Order条件时,使用的是addOrder()方法,而不是add()方法,在产生SQL语句时,会使用order by与asc(desc)来进行排序指定:
Hibernate: select this_.id as id0_0_,this_.name as name0_0_, this_.age as age0_0_ from T_USER this_ order bythis_.age asc
限定查询笔数
Criteria的setMaxResults()方法可以限定查询回来的笔数,如果配合setFirstResult()设定传回查询结果第一笔资料的位置,就可以实现简单的分页,例如传回第51笔之后的50笔资料(如果有的话):
Criteria criteria = session.createCriteria(User.class);
criteria.setFirstResult(51);
criteria.setMaxResults(50);
List users = criteria.list();
根据您所指定得资料库,Hibernate将自动产生与资料库相依的限定笔数查询子句,例如在MySQL中,将使用limit产生以下的SQL语句:
Hibernate: select this_.id as id0_0_,this_.name as name0_0_, this_.age as age0_0_ from T_USER this_ limit ?, ?
统计动作
您可以对查询结果进行统计动作,使用org.hibernate.criterion.Projections的avg()、rowCount()、count()、max()、min ()、 countDistinct()等方法,再搭配Criteria的setProjection()方法加入条件设定,例如对查询结果的"age"作平均:
Criteria criteria = session.createCriteria(User.class);
criteria.setProjection(Projections.avg("age"));
List users = criteria.list();
上面的程式将由Hibernate自动产生SQL的avg函数进行平均计算:
Hibernate: select avg(this_.age) as y0_from T_USER this_
分组
还可以配合Projections的groupProperty()来对结果进行分组,例如以"age"进行分组,也就是如果资料中"age"如果有 20、20、25、30,则以下会显示20、25、30:
Criteria criteria = session.createCriteria(User.class);
criteria.setProjection(Projections.groupProperty("age"));
List users = criteria.list();
上面的程式将由Hibernate自动产生SQL的group by子句进行分组计算:
Hibernate: select this_.age as y0_ fromT_USER this_ group by this_.age
如果想同时结合统计与分组功能,则可以使用org.hibernate.criterion.ProjectionList,例如下面的程式会计算每个年龄各有多少个人:
ProjectionList projectionList = Projections.projectionList();
projectionList.add(Projections.groupProperty("age"));
projectionList.add(Projections.rowCount());
Criteria criteria = session.createCriteria(User.class);
criteria.setProjection(projectionList);
List users = criteria.list();
观察所产生的SQL语句,将使用group by先进行分组,再针对每个分组进行count函数的计数,
Hibernate: select this_.age as y0_,count(*) as y1_ from T_USER this_ group by this_.age
根据已知物件进行查询
设定查询条件并非一定要使用Restrictions,如果属性条件很多,使用Restrictions也不方便,如果有一个已知的物件,则可以根据这个物件作为查询的依据,看看是否有属性与之类似的物件,例如:
User user = new User();
user.setAge(new Integer(30));
Criteria criteria = session.createCriteria(User.class);
criteria.add(Example.create(user));
List users = criteria.list();
Criteria进阶查询中,您可以透过org.hibernate.criterion.Example的create()方法来建立Example实例,Example实作了 Criteria介面,因此可以使用add()方法加入至Criteria条件设定之中,Hibernate将自动过滤掉空属性,根据已知物件上已设定的属性,判定是否产生于where子句之中:
Hibernate: select this_.id as id0_0_,this_.name as name0_0_, this_.age as age0_0_ from T_USER this_ where(this_.age=?)
设定SQL范本
如果您了解如何撰写SQL语句,想要设定一些Hibernate产生SQL时的范本,您也可以使用Restrictions的sqlRestriction()方法,提供SQL语法范本作限定查询,例如查询name以cater开头的资料:
Criteria criteria = session.createCriteria(User.class);
criteria.add(Restrictions.sqlRestriction(
"{alias}.name LIKE (?)", "cater%", Hibernate.STRING));
List users = criteria.list();
其中alias将被替换为与User类别相关的名称,而? 将被替换为cater%,也就是第二个参数所提供的值,sqlRestriction()方法第一个参数所设定的是where子句的部份,所以在SQL撰写时,不必再写where,观察所产生的SQL语句,将使用您所设定的SQL范本作为基础,来完成SQL的条件查询:
Hibernate: select this_.id as id0_0_,this_.name as name0_0_, this_.age as age0_0_ from T_USER this_ where this_.nameLIKE (?)
如果有多个查询条件,例如between子句的查询,则可以如下:
Criteria criteria = session.createCriteria(User.class);
Integer[] ages = {new Integer(20), new Integer(40)};
Type[] types = {Hibernate.INTEGER, Hibernate.INTEGER};
criteria.add(Restrictions.sqlRestriction(
"{alias}.age BETWEEN (?) AND (?)", ages, types));
List users = criteria.list();
观察所产生的SQL语句如下:
Hibernate: select this_.id as id0_0_, this_.nameas name0_0_, this_.age as age0_0_ from T_USER this_ where this_.age BETWEEN (?)AND (?)
2.连接限制
在Criteria 查询中使用FetchMode来实现连接限制。在HQL语句中,可以通过fetch关键字来表示预先抓取(Eager fetching),如下所示:
from Group g |
可以使用Criteria的API完成同样的功能,如下所示:
Criteria cr = session.createCriteria(Group.class); |
以上两种方式编写的代码,都使用相同的SQL语句完成它们的功能,如下所示:
select g.*, s.* from Group g |
Query :
常用方法:
Method Summary | |
int | executeUpdate() |
String[] | getNamedParameters() |
getQueryString() | |
String[] | getReturnAliases() |
Type[] | getReturnTypes() |
boolean | isReadOnly() |
iterate() | |
list() | |
scroll() | |
scroll(ScrollMode scrollMode) | |
setBigDecimal(int position, BigDecimal number) | |
setBigDecimal(String name, BigDecimal number) | |
setBigInteger(int position, BigInteger number) | |
setBigInteger(String name, BigInteger number) | |
setBinary(int position, byte[] val) | |
setBoolean(int position, boolean val) | |
setBoolean(String name, boolean val) | |
setByte(int position, byte val) | |
setCacheable(boolean cacheable) | |
setCacheMode(CacheMode cacheMode) | |
setCacheRegion(String cacheRegion) | |
setCalendar(int position, Calendar calendar) | |
setCalendar(String name, Calendar calendar) | |
setCalendarDate(int position, Calendar calendar) | |
setCalendarDate(String name, Calendar calendar) | |
setCharacter(int position, char val) | |
setCharacter(String name, char val) | |
setComment(String comment) | |
setDate(String name, Date date) | |
setDouble(int position, double val) | |
setEntity(int position, Object val) | |
setEntity(String name, Object val) | |
setFetchSize(int fetchSize) | |
setFirstResult(int firstResult) | |
setFloat(int position, float val) | |
setFlushMode(FlushMode flushMode) | |
setInteger(int position, int val) | |
setInteger(String name, int val) | |
setLockMode(String alias, LockMode lockMode) | |
setLockOptions(LockOptions lockOptions) | |
setLong(int position, long val) | |
setMaxResults(int maxResults) | |
setParameter(int position, Object val) | |
setParameter(int position, Object val, Type type) | |
setParameter(String name, Object val) | |
setParameter(String name, Object val, Type type) | |
setParameterList(String name, Collection vals) | |
setParameterList(String name, Collection vals, Type type) | |
setParameterList(String name, Object[] vals) | |
setParameterList(String name, Object[] vals, Type type) | |
setParameters(Object[] values, Type[] types) | |
setProperties(Map bean) | |
setProperties(Object bean) | |
setReadOnly(boolean readOnly) | |
setResultTransformer(ResultTransformer transformer) | |
setSerializable(int position, Serializable val) | |
setSerializable(String name, Serializable val) | |
setShort(int position, short val) | |
setTime(String name, Date date) | |
setTimeout(int timeout) | |
setTimestamp(int position, Date date) | |
setTimestamp(String name, Date date) | |
uniqueResult() |
Query-----Hibernate查询(Query Language)
参考:http://blog.sina.com.cn/s/blog_aaa756740101fncl.html (讲的很好)