leProjection 和 ProjectionList 的实例化是由内建的 Projections 来完成,如提供的 avg 、 count 、 max 、 min 、 sum 可以让开发者很容易对某个字段进行统计查询。
Property 是对某个字段进行查询条件的设置,如通过Porperty.forName“color”.in new String“black””red””write” 则可以创建一个 Project 实例。
通过 criteria 的 addProject 方法加入到查询条件中去。
使用 Criteria 进行查询,主要要清晰的是 Hibernate 提供了那些类和方法来满足开发中查 询条件的创建和组装,下面介绍几种用法: 1. 创建一个Criteria 实例 org.hibernate.Criteria接口表示特定持久类的一个查询。
Session是 Criteria实例的工厂。
Criteria crit sess.createCriteriaCat.class crit.setMaxResults50 List cats crit.list 2. 限制结果集内容 一个单独的查询条件是org.hibernate.criterion.Criterion 接口的一个实例。
org.hibernate.criterion.Restrictions类 定义了获得某些内置Criterion类型的工厂方法。
List cats sess.createCriteriaCat.class .add Restrictions.likequotnamequot quotFritzquot .add Restrictions.betweenquotweightquot minWeight maxWeight .list 约束可以按逻辑分组。
List cats sess.createCriteriaCat.class .add Restrictions.likequotnamequot quotFritzquot .add Restrictions.or Restrictions.eq quotagequot new Integer0 Restrictions.isNullquotagequot .list List cats sess.createCriteriaCat.class .add Restrictions.in quotnamequot new String quotFritzquot quotIziquot quotPkquot .add Restrictions.disjunction .add Restrictions.isNullquotagequot .add Restrictions.eqquotagequot new Integer0 .add Restrictions.eqquotagequot new Integer1 .add Restrictions.eqquotagequot new Integer2 .list Hibernate提供了相当多的内置criterion类型Restrictions 子类 但是尤其有用的是可以允许 你直接使用SQL。
List cats sess.createCriteriaCat.class .add Restrictions.sqlquotloweralias.name like lowerquot quotFritzquot Hibernate.STRING .list alias占位符应当被替换为被查询实体的列别名。
Property实例是获得一个条件的另外一种途径。
你可以通过调用Property.forName 创建一个 Property。
Property age Property.forNamequotagequot List cats sess.createCriteriaCat.class .add Restrictions.disjunction .add age.isNull .add age.eq new Integer0 .add age.eq new Integer1 .add age.eq new Integer2 .add Property.forNamequotnamequot.in new String quotFritzquot quotIziquot quotPkquot .list 3. 结果集排序 你可以使用org.hibernate.criterion.Order来为查询结果排序。
List cats sess.createCriteriaCat.class .add Restrictions.likequotnamequot quotFquot .addOrder Order.ascquotnamequot .addOrder Order.descquotagequot .setMaxResults50 .list List cats sess.createCriteriaCat.class .add Property.forNamequotnamequot.likequotFquot .addOrder Property.forNamequotnamequot.asc .addOrder Property.forNamequotagequot.desc .setMaxResults50 .list 4. 关联 你可以使用createCriteria非常容易的在互相关联的实体间建立 约束。
List cats sess.createCriteriaCat.class .add Restrictions.likequotnamequot quotFquot .createCriteriaquotkittensquot .add Restrictions.likequotnamequot quotFquot .list 注意第二个 createCriteria返回一个新的 Criteria实例,该实例引用kittens 集合中的元素。
接下来,替换形态在某些情况下也是很有用的。
List cats sess.createCriteriaCat.class .createAliasquotkittensquot quotktquot .createAliasquotmatequot quotmtquot .add Restrictions.eqPropertyquotkt.namequot quotmt.namequot .list createAlias并不创建一个新的 Criteria实例。
Cat实例所保存的之前两次查询所返回的kittens集合是 没有被条件预过滤的。
如果你希望只获得 符合条件的kittens, 你必须使用returnMaps。
List cats sess.createCriteriaCat.class .createCriteriaquotkittensquot quotktquot .add Restrictions.eqquotnamequot quotFquot .returnMaps .list Iterator iter cats.iterator while iter.hasNext Map map Map iter.next.
上一篇:
javaweb开发的通用功能实现源代码下载
下一篇:
多年来只想说一句,我不怪你