1.实例
接口org.hibernate.Criteria针对特殊持久层类进行查询,Sesion是Criteria的工厂:
1 Criteria crit = sess.createCriteria(Cat.class); 2 crit.setMaxResults(50); 3 List cats = crit.list();
2.精简结果集
1 List cats = sess.createCriteria(Cat.class) 2 .add( Restrictions.like("name", "Fritz%") ) 3 .add( Restrictions.between("weight", minWeight, maxWeight) ) 4 .list();
1 List cats = sess.createCriteria(Cat.class) 2 .add( Restrictions.like("name", "Fritz%") ) 3 .add( Restrictions.or( 4 Restrictions.eq( "age", new Integer(0) ), 5 Restrictions.isNull("age") 6 ) ) 7 .list();
1 List cats = sess.createCriteria(Cat.class) 2 .add( Restrictions.in( "name", new String[] { "Fritz", "Izi", "Pk" } ) ) 3 .add( Restrictions.disjunction() 4 .add( Restrictions.isNull("age") ) 5 .add( Restrictions.eq("age", new Integer(0) ) ) 6 .add( Restrictions.eq("age", new Integer(1) ) ) 7 .add( Restrictions.eq("age", new Integer(2) ) ) 8 ) ) 9 .list();
1 List cats = sess.createCriteria(Cat.class) 2 .add( Restrictions.sqlRestriction("lower({alias}.name) like lower(?)", "Fritz%", Hibernate.STRING) ) 3 .list();
1 Property age = Property.forName("age"); 2 List cats = sess.createCriteria(Cat.class) 3 .add( Restrictions.disjunction() 4 .add( age.isNull() ) 5 .add( age.eq( new Integer(0) ) ) 6 .add( age.eq( new Integer(1) ) ) 7 .add( age.eq( new Integer(2) ) ) 8 ) ) 9 .add( Property.forName("name").in( new String[] { "Fritz", "Izi", "Pk" } ) ) 10 .list();
3.结果集排序
1 List cats = sess.createCriteria(Cat.class) 2 .add( Restrictions.like("name", "F%") 3 .addOrder( Order.asc("name") ) 4 .addOrder( Order.desc("age") ) 5 .setMaxResults(50) 6 .list();
1 List cats = sess.createCriteria(Cat.class) 2 .add( Property.forName("name").like("F%") ) 3 .addOrder( Property.forName("name").asc() ) 4 .addOrder( Property.forName("age").desc() ) 5 .setMaxResults(50) 6 .list();
4.关联
1 List cats = sess.createCriteria(Cat.class) 2 .add( Restrictions.like("name", "F%") ) 3 .createCriteria("kittens") 4 .add( Restrictions.like("name", "F%") ) 5 .list();
1 List cats = sess.createCriteria(Cat.class) 2 .createAlias("kittens", "kt") 3 .createAlias("mate", "mt") 4 .add( Restrictions.eqProperty("kt.name", "mt.name") ) 5 .list();
1 List cats = sess.createCriteria(Cat.class) 2 .createCriteria("kittens", "kt") 3 .add( Restrictions.eq("name", "F%") ) 4 .setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP) 5 .list(); 6 Iterator iter = cats.iterator(); 7 while ( iter.hasNext() ) { 8 Map map = (Map) iter.next(); 9 Cat cat = (Cat) map.get(Criteria.ROOT_ALIAS); 10 Cat kitten = (Cat) map.get("kt"); 11 }
1 List cats = session.createCriteria( Cat.class ) 2 .createAlias("mate", "mt", Criteria.LEFT_JOIN, Restrictions.like("mt.name", "good%") ) 3 .addOrder(Order.asc("mt.age")) 4 .list(); 5
5.动态关联检索
List cats = sess.createCriteria(Cat.class) .add( Restrictions.like("name", "Fritz%") ) .setFetchMode("mate", FetchMode.EAGER) .setFetchMode("kittens", FetchMode.EAGER) .list();
6.组件
Cat有fullName属性,fullName属性又有lastName属性:
List cats = session.createCriteria(Cat.class) .add(Restrictions.eq("fullName.lastName", "Cattington")) .list();