1. ) )
2. .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.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) .add( Restrictions.in( "name", new String[] { "Fritz", "Izi", "Pk" } ) ) .add( Restrictions.disjunction() .add( Restrictions.isNull("age") ) .add( Restrictions.eq("age", new Integer(0) ) ) .add( Restrictions.eq("age", new Integer(1) ) ) .add( Restrictions.eq("age", new Integer(2) ) ) ) ) .list();
List cats = sess.createCriteria(Cat.class) .add( Restrictions.in( "name",new String[] { "Fritz", "Izi", "Pk" } ) ) .add( Restrictions.disjunction() .add(Restrictions.isNull("age") ) .add( Restrictions.eq("age", new Integer(0) ) ) .add( Restrictions.eq("age",new Integer(1) ) ) .add(Restrictions.eq("age", new Integer(2) ) ) ) ) .list();
Hibernate提供了相当多的内置criterion类型(Restrictions 子类), 但是尤其有用的是可以允许 你直接使用SQL。
1. List cats =sess.createCriteria(Cat. class )
2. .add( Restrictions.sql( "lower({alias}.name) like lower(?)" ,"Fritz%" ,
3. Hibernate.STRING))
4. .list();
1. List cats =sess.createCriteria(Cat. class )
2. .add( Restrictions.sql("lower({alias}.name) like lower(?)" ,"Fritz%" ,
3. Hibernate.STRING))
4. .list();
1. List cats = sess.createCriteria(Cat.class) .add( Restrictions.sql("lower({alias}.name) like lower(?)", "Fritz%", Hibernate.STRING) ) .list();
List cats = sess.createCriteria(Cat.class) .add(Restrictions.sql("lower({alias}.name) like lower(?)","Fritz%", Hibernate.STRING) ) .list();
{alias}占位符应当被替换为被查询实体的列别名。 Property实例是获得一个条件的另外一种途径。你可以通过调用Property.forName() 创建一个 Property。
1. Property age =Property.forName( "age" );
2. st 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();
1. Property age =Property.forName( "age" );
2. st 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();
1. Property age = Property.forName("age"); List cats = sess.createCriteria(Cat.class) .add( Restrictions.disjunction() .add( age.isNull() ) .add( age.eq( new Integer(0) ) ) .add( age.eq( new Integer(1) ) ) .add( age.eq( new Integer(2) ) ) ) ) .add( Property.forName("name").in( new String[] { "Fritz", "Izi", "Pk" } ) ) .list();
Property age = Property.forName("age"); List cats =sess.createCriteria(Cat.class) .add(Restrictions.disjunction() .add(age.isNull() ) .add( age.eq( newInteger(0) ) ) .add( age.eq( newInteger(1) ) ) .add( age.eq( newInteger(2) ) ) ) ) .add(Property.forName("name").in( new String[] { "Fritz","Izi", "Pk" } ) ) .list();
3. 结果集排序
你可以使用org.hibernate.criterion.Order来为查询结果排序。
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( 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) .add( Restrictions.like("name", "F%") .addOrder( Order.asc("name") ) .addOrder( Order.desc("age") ) .setMaxResults(50) .list();
List cats = sess.createCriteria(Cat.class) .add( Restrictions.like("name","F%") .addOrder(Order.asc("name") ) .addOrder( Order.desc("age") ) .setMaxResults(50) .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();
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();
1. List cats = sess.createCriteria(Cat.class) .add( Property.forName("name").like("F%") ) .addOrder( Property.forName("name").asc() ) .addOrder( Property.forName("age").desc() ) .setMaxResults(50) .list();
List cats = sess.createCriteria(Cat.class) .add(Property.forName("name").like("F%") ) .addOrder(Property.forName("name").asc() ) .addOrder( Property.forName("age").desc() ) .setMaxResults(50) .list();
4. 关联
你可以使用createCriteria()非常容易的在互相关联的实体间建立约束。
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. .add( Restrictions.like("name" , "F%" )
3. .createCriteria("kittens" )
4. .add( Restrictions.like("name" , "F%" )
5. .list();
1. List cats = sess.createCriteria(Cat.class) .add( Restrictions.like("name", "F%") .createCriteria("kittens") .add( Restrictions.like("name", "F%") .list();
List cats = sess.createCriteria(Cat.class) .add( Restrictions.like("name","F%") .createCriteria("kittens") .add( Restrictions.like("name", "F%") .list();
注意第二个 createCriteria()返回一个新的 Criteria实例,该实例引用kittens 集合中的元素。 接下来,替换形态在某些情况下也是很有用的。
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. .createAlias("kittens" , "kt" )
3. .createAlias("mate" , "mt" )
4. .add( Restrictions.eqProperty("kt.name" , "mt.name" ))
5. .list();
1. List cats = sess.createCriteria(Cat.class) .createAlias("kittens", "kt") .createAlias("mate", "mt") .add( Restrictions.eqProperty("kt.name", "mt.name") ) .list();
List cats = sess.createCriteria(Cat.class) .createAlias("kittens","kt") .createAlias("mate", "mt") .add(Restrictions.eqProperty("kt.name", "mt.name") ) .list();
(createAlias()并不创建一个新的 Criteria实例。) Cat实例所保存的之前两次查询所返回的kittens集合是 没有被条件预过滤的。如果你希望只获得符合条件的kittens, 你必须使用returnMaps()。
1. List cats =sess.createCriteria(Cat. class )
2. .createCriteria( "kittens" , "kt" )
3. .add( Restrictions.eq( "name" , "F%" ) )
4. .returnMaps()
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 =sess.createCriteria(Cat. class )
2. .createCriteria("kittens" , "kt" )
3. .add( Restrictions.eq("name" , "F%" ) )
4. .returnMaps()
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 = sess.createCriteria(Cat.class) .createCriteria("kittens", "kt") .add( Restrictions.eq("name", "F%") ) .returnMaps() .list(); Iterator iter = cats.iterator(); while ( iter.hasNext() ) { Map map = (Map) iter.next(); Cat cat = (Cat) map.get(Criteria.ROOT_ALIAS); Cat kitten = (Cat) map.get("kt"); }
List cats = sess.createCriteria(Cat.class) .createCriteria("kittens","kt") .add(Restrictions.eq("name", "F%") ) .returnMaps() .list(); Iterator iter = cats.iterator();while ( iter.hasNext() ) { Map map =(Map) iter.next(); Cat cat = (Cat)map.get(Criteria.ROOT_ALIAS); Catkitten = (Cat) map.get("kt"); }
5. 动态关联抓取 你可以使用setFetchMode()在运行时定义动态关联抓取的语义。
1. List cats =sess.createCriteria(Cat. class )
2. .add( Restrictions.like( "name" , "Fritz%" ) )
3. .setFetchMode( "mate" , FetchMode.EAGER)
4. .setFetchMode( "kittens" , FetchMode.EAGER)
5. .list();
1. List cats =sess.createCriteria(Cat. class )
2. .add( Restrictions.like("name" , "Fritz%" ) )
3. .setFetchMode("mate" , FetchMode.EAGER)
4. .setFetchMode("kittens" , FetchMode.EAGER)
5. .list();
1. List cats = sess.createCriteria(Cat.class) .add( Restrictions.like("name", "Fritz%") ) .setFetchMode("mate", FetchMode.EAGER) .setFetchMode("kittens", FetchMode.EAGER) .list();
List cats = sess.createCriteria(Cat.class) .add( Restrictions.like("name","Fritz%") ) .setFetchMode("mate", FetchMode.EAGER) .setFetchMode("kittens",FetchMode.EAGER) .list();
这个查询可以通过外连接抓取mate和kittens。
6. 查询示例 org.hibernate.criterion.Example类允许你通过一个给定实例 构建一个条件查询。
1. Cat cat = newCat();
2. cat.setSex( ‘F‘ );
3. cat.setColor(Color.BLACK);
4. List results =session.createCriteria(Cat. class )
5. .add( Example.create(cat) )
6. .list();
1. Cat cat = new Cat();
2. cat.setSex(‘F‘);
3. cat.setColor(Color.BLACK);
4. List results =session.createCriteria(Cat.class )
5. .add( Example.create(cat) )
6. .list();
1. Cat cat = new Cat(); cat.setSex(‘F‘); cat.setColor(Color.BLACK); List results = session.createCriteria(Cat.class) .add( Example.create(cat) ) .list();
Cat cat = new Cat(); cat.setSex(‘F‘); cat.setColor(Color.BLACK); Listresults = session.createCriteria(Cat.class) .add( Example.create(cat) ) .list();
版本属性、标识符和关联被忽略。默认情况下值为null的属性将被排除。可以自行调整Example使之更实用。
1. Example example =Example.create(cat)
2. .excludeZeroes() //exclude zero valued properties
3. .excludeProperty( "color" ) //exclude the property named"color"
4. .ignoreCase() //perform case insensitive string comparisons
5. .enableLike(); //use like for string comparisons
6. List results =session.createCriteria(Cat. class )
7. .add(example)
8. .list();
1. Example example =Example.create(cat)
2. .excludeZeroes() //excludezero valued properties
3. .excludeProperty("color" ) //exclude the property named"color"
4. .ignoreCase() //performcase insensitive string comparisons
5. .enableLike(); //uselike for string comparisons
6. List results =session.createCriteria(Cat.class )
7. .add(example)
8. .list();
1. Example example = Example.create(cat) .excludeZeroes() //exclude zero valued properties .excludeProperty("color") //exclude the property named "color" .ignoreCase() //perform case insensitive string comparisons .enableLike(); //use like for string comparisons List results = session.createCriteria(Cat.class) .add(example) .list();
Example example = Example.create(cat) .excludeZeroes() //exclude zero valued properties .excludeProperty("color") //exclude the property named "color" .ignoreCase() //perform case insensitive stringcomparisons .enableLike(); //use like for string comparisonsList results = session.createCriteria(Cat.class) .add(example) .list();
甚至可以使用examples在关联对象上放置条件。
1. List results =session.createCriteria(Cat. class )
2. .add( Example.create(cat) )
3. .createCriteria( "mate" )
4. .add( Example.create( cat.getMate() ) )
5. .list();
1. List results =session.createCriteria(Cat. class )
2. .add( Example.create(cat) )
3. .createCriteria("mate" )
4. .add( Example.create( cat.getMate() ) )
5. .list();
1. List results = session.createCriteria(Cat.class) .add( Example.create(cat) ) .createCriteria("mate") .add( Example.create( cat.getMate() ) ) .list();
List results = session.createCriteria(Cat.class) .add( Example.create(cat) ) .createCriteria("mate") .add( Example.create( cat.getMate() )) .list();
7. 投影(Projections)、聚合(aggregation)和分组(grouping) org.hibernate.criterion.Projections是 Projection 的实例工厂。我们通过调用 setProjection()应用投影到一个查询。
1. List results =session.createCriteria(Cat. class )
2. .setProjection( Projections.rowCount() )
3. .add( Restrictions.eq( "color" , Color.BLACK) )
4. .list();
1. List results =session.createCriteria(Cat. class )
2. .setProjection( Projections.rowCount() )
3. .add( Restrictions.eq("color" , Color.BLACK) )
4. .list();
1. List results =session.createCriteria(Cat. class )
2. .setProjection( Projections.projectionList()
3. .add( Projections.rowCount() )
4. .add( Projections.avg( "weight" ) )
5. .add( Projections.max( "weight" ) )
6. .add( Projections.groupProperty( "color" ) )
7. )
8. .list();
1. List results =session.createCriteria(Cat. class )
2. .setProjection( Projections.projectionList()
3. .add( Projections.rowCount() )
4. .add( Projections.avg("weight" ) )
5. .add( Projections.max("weight" ) )
6. .add( Projections.groupProperty("color" ) )
7. )
8. .list();
1. List results = session.createCriteria(Cat.class) .setProjection( Projections.projectionList() .add( Projections.rowCount() ) .add( Projections.avg("weight") ) .add( Projections.max("weight") ) .add( Projections.groupProperty("color") ) ) .list();
List results = session.createCriteria(Cat.class) .setProjection(Projections.projectionList() .add( Projections.rowCount() ) .add( Projections.avg("weight") ) .add(Projections.max("weight") ) .add( Projections.groupProperty("color") ) ) .list();
在一个条件查询中没有必要显式的使用 "groupby" 。某些投影类型就是被定义为 分组投影,他 们也出现在SQL的group by子句中。 可以选择把一个别名指派给一个投影,这样可以使投影值被约束或排序所引用。下面是两种不同的 实现方式:
1. List results =session.createCriteria(Cat. class )
2. .setProjection( Projections.alias( Projections.groupProperty( "color"), "colr" ) )
3. .addOrder( Order.asc( "colr" ) )
4. .list();
1. List results =session.createCriteria(Cat. class )
2. .setProjection( Projections.alias( Projections.groupProperty("color"), "colr" ) )
3. .addOrder( Order.asc("colr" ) )
4. .list();
1. List results = session.createCriteria(Cat.class) .setProjection( Projections.alias( Projections.groupProperty("color"), "colr" ) ) .addOrder( Order.asc("colr") ) .list();
List results = session.createCriteria(Cat.class) .setProjection( Projections.alias(Projections.groupProperty("color"), "colr" ) ) .addOrder( Order.asc("colr")) .list();
1. List results =session.createCriteria(Cat. class )
2. .setProjection( Projections.groupProperty( "color" ).as("colr" ) )
3. .addOrder( Order.asc( "colr" ) )
4. .list();
1. List results =session.createCriteria(Cat. class )
2. .setProjection( Projections.groupProperty("color" ).as("colr" ) )
3. .addOrder( Order.asc("colr" ) )
4. .list();
1. List results = session.createCriteria(Cat.class) .setProjection( Projections.groupProperty("color").as("colr") ) .addOrder( Order.asc("colr") ) .list();
List results = session.createCriteria(Cat.class) .setProjection(Projections.groupProperty("color").as("colr") ) .addOrder( Order.asc("colr")) .list();
alias()和as()方法简便的将一个投影实例包装到另外一个别名的Projection实例中。简而言之, 当你添加一个投影到一个投影列表中时 你可以为它指定一个别名:
1. List results =session.createCriteria(Cat. class )
2. .setProjection( Projections.projectionList()
3. .add( Projections.rowCount(), "catCountByColor" )
4. .add( Projections.avg( "weight" ), "avgWeight" )
5. .add( Projections.max( "weight" ), "maxWeight" )
6. .add( Projections.groupProperty( "color" ), "color" )
7. )
8. .addOrder( Order.desc( "catCountByColor" ) )
9. .addOrder( Order.desc( "avgWeight" ) )
10. .list();
1. List results =session.createCriteria(Cat. class )
2. .setProjection( Projections.projectionList()
3. .add( Projections.rowCount(), "catCountByColor" )
4. .add( Projections.avg("weight" ), "avgWeight" )
5. .add( Projections.max("weight" ), "maxWeight" )
6. .add( Projections.groupProperty("color" ), "color")
7. )
8. .addOrder( Order.desc("catCountByColor" ) )
9. .addOrder( Order.desc("avgWeight" ) )
10. .list();
1. List results =session.createCriteria(Domestic. class , "cat" )
2. .createAlias( "kittens" , "kit" )
3. .setProjection( Projections.projectionList()
4. .add( Projections.property( "cat.name" ), "catName" )
5. .add( Projections.property( "kit.name" ), "kitName" )
6. )
7. .addOrder( Order.asc( "catName" ) )
8. .addOrder( Order.asc( "kitName" ) )
9. .list();
1. List results =session.createCriteria(Domestic. class , "cat" )
2. .createAlias("kittens" , "kit" )
3. .setProjection( Projections.projectionList()
4. .add( Projections.property("cat.name" ), "catName")
5. .add( Projections.property("kit.name" ), "kitName")
6. )
7. .addOrder( Order.asc("catName" ) )
8. .addOrder( Order.asc("kitName" ) )
9. .list();
1. List results = session.createCriteria(Domestic.class, "cat") .createAlias("kittens", "kit") .setProjection( Projections.projectionList() .add( Projections.property("cat.name"), "catName" ) .add( Projections.property("kit.name"), "kitName" ) ) .addOrder( Order.asc("catName") ) .addOrder( Order.asc("kitName") ) .list();
List results = session.createCriteria(Domestic.class,"cat") .createAlias("kittens", "kit") .setProjection(Projections.projectionList() .add(Projections.property("cat.name"), "catName" ) .add(Projections.property("kit.name"), "kitName" ) ) .addOrder( Order.asc("catName") ) .addOrder( Order.asc("kitName")) .list();
也可以使用Property.forName()来表示投影:
1. List results = session.createCriteria(Cat.class )
2. .setProjection( Property.forName( "name" ) )
3. .add( Property.forName( "color" ).eq(Color.BLACK) )
4. .list();
1. List results =session.createCriteria(Cat. class )
2. .setProjection( Property.forName("name" ) )
3. .add( Property.forName("color" ).eq(Color.BLACK) )
4. .list();
1. List results =session.createCriteria(Cat. class )
2. .setProjection( Projections.projectionList()
3. .add( Projections.rowCount().as( "catCountByColor" ) )
4. .add( Property.forName( "weight" ).avg().as( "avgWeight" ))
5. .add( Property.forName( "weight" ).max().as( "maxWeight" ))
6. .add( Property.forName( "color" ).group().as( "color" )
7. )
8. .addOrder( Order.desc( "catCountByColor" ) )
9. .addOrder( Order.desc( "avgWeight" ) )
10. .list();
1. List results =session.createCriteria(Cat. class )
2. .setProjection( Projections.projectionList()
3. .add( Projections.rowCount().as("catCountByColor" ) )
4. .add( Property.forName("weight" ).avg().as( "avgWeight" ))
5. .add( Property.forName("weight" ).max().as( "maxWeight" ))
6. .add( Property.forName("color" ).group().as( "color")
7. )
8. .addOrder( Order.desc("catCountByColor" ) )
9. .addOrder( Order.desc("avgWeight" ) )
10. .list();
1. List results = session.createCriteria(Cat.class) .setProjection( Projections.projectionList() .add( Projections.rowCount().as("catCountByColor") ) .add( Property.forName("weight").avg().as("avgWeight") ) .add( Property.forName("weight").max().as("maxWeight") ) .add( Property.forName("color").group().as("color" ) ) .addOrder( Order.desc("catCountByColor") ) .addOrder( Order.desc("avgWeight") ) .list();
List results = session.createCriteria(Cat.class) .setProjection(Projections.projectionList() .add( Projections.rowCount().as("catCountByColor") ) .add(Property.forName("weight").avg().as("avgWeight") ) .add(Property.forName("weight").max().as("maxWeight") ) .add( Property.forName("color").group().as("color") ) .addOrder(Order.desc("catCountByColor") ) .addOrder( Order.desc("avgWeight") ) .list();
8. 离线(detached)查询和子查询 DetachedCriteria类使你在一个session范围之外创建一个查询,并且可以使用任意的 Session来 执行它。
1. DetachedCriteriaquery = DetachedCriteria.forClass(Cat. class )
2. .add( Property.forName( "sex" ).eq( ‘F‘ ) );
3. //创建一个Session
4. Session session =.;
5. Transaction txn =session.beginTransaction();
6. List results =query.getExecutableCriteria(session).setMaxResults( 100).list();
7. txn.commit();
8. session.close();
1. DetachedCriteriaquery = DetachedCriteria.forClass(Cat. class )
2. .add( Property.forName("sex" ).eq( ‘F‘ ) );
3. //创建一个Session
4. Session session =.;
5. Transaction txn =session.beginTransaction();
6. List results =query.getExecutableCriteria(session).setMaxResults(100).list();
7. txn.commit();
8. session.close();
1. DetachedCriteria query = DetachedCriteria.forClass(Cat.class) .add( Property.forName("sex").eq(‘F‘) ); //创建一个Session Session session = .; Transaction txn = session.beginTransaction(); List results = query.getExecutableCriteria(session).setMaxResults(100).list(); txn.commit(); session.close();
DetachedCriteria query = DetachedCriteria.forClass(Cat.class) .add(Property.forName("sex").eq(‘F‘) ); //创建一个Session Session session = .;Transaction txn = session.beginTransaction(); List results = query.getExecutableCriteria(session).setMaxResults(100).list();txn.commit(); session.close();
DetachedCriteria也可以用以表示子查询。条件实例包含子查询可以通过 Subqueries或者 Property获得。
1. DetachedCriteriaavgWeight = DetachedCriteria.forClass(Cat. class )
2. .setProjection( Property.forName( "weight" ).avg() );
3. session.createCriteria(Cat.class )
4. .add( Property.forName("weight).gt(avgWeight) )
5. .list();
6. DetachedCriteriaweights = DetachedCriteria.forClass(Cat. class )
7. .setProjection( Property.forName( "weight" ) );
8. session.createCriteria(Cat.class )
9. .add( Subqueries.geAll( "weight" , weights) )
10. .list();
1. DetachedCriteriaavgWeight = DetachedCriteria.forClass(Cat. class )
2. .setProjection( Property.forName("weight" ).avg() );
3. session.createCriteria(Cat.class)
4. .add( Property.forName("weight).gt(avgWeight) )
5. .list();
6. DetachedCriteriaweights = DetachedCriteria.forClass(Cat.class )
7. .setProjection( Property.forName("weight" ) );
8. session.createCriteria(Cat.class)
9. .add( Subqueries.geAll("weight" , weights) )
10. .list();
1. DetachedCriteria avgWeight = DetachedCriteria.forClass(Cat.class) .setProjection( Property.forName("weight").avg() ); session.createCriteria(Cat.class) .add( Property.forName("weight).gt(avgWeight) ) .list(); DetachedCriteria weights = DetachedCriteria.forClass(Cat.class) .setProjection( Property.forName("weight") ); session.createCriteria(Cat.class) .add( Subqueries.geAll("weight", weights) ) .list();
DetachedCriteria avgWeight = DetachedCriteria.forClass(Cat.class) .setProjection(Property.forName("weight").avg() );session.createCriteria(Cat.class) .add( Property.forName("weight).gt(avgWeight) ) .list(); DetachedCriteria weights =DetachedCriteria.forClass(Cat.class) .setProjection( Property.forName("weight") ); session.createCriteria(Cat.class) .add( Subqueries.geAll("weight",weights) ) .list();
相互关联的子查询也是有可能的:
1. DetachedCriteriaavgWeightForSex = DetachedCriteria.forClass(Cat. class , "cat2" )
2. .setProjection( Property.forName( "weight" ).avg() )
3. .add( Property.forName( "cat2.sex" ).eqProperty( "cat.sex") );
4. session.createCriteria(Cat.class , "cat" )
5. .add( Property.forName("weight).gt(avgWeightForSex) )
6. .list();
1. DetachedCriteriaavgWeightForSex = DetachedCriteria.forClass(Cat. class , "cat2")
2. .setProjection( Property.forName("weight" ).avg() )
3. .add( Property.forName("cat2.sex" ).eqProperty( "cat.sex" ));
4. session.createCriteria(Cat.class, "cat" )
5. .add( Property.forName("weight).gt(avgWeightForSex) )
6. .list();
在hibernate中要实现动态查询或者子查询等可以用DetachedCriteria来动态构造sql语句
比如要实现类似这样一个sql语句:
select* from Table1 t1 where t1.field1 in(select field2 from Table2 t2 wheret2.field3=value)
实现方法:
Sessionsession = HibernateSessionFactory.currentSession();
Criteriacriteria = session.createCriteria(Table1.class);
DetachedCriteriadc =DetachedCriteria.forClass(Table2.class).setProjection(Property.forName("field2"));
dc.add(Expression.eq("field3",value));
criteria.add(Property.forName("field1").in(dc));
Listlist = criteria.list()
HibernateSessionFactory.closeSession();
本文出自 “第一天的课” 博客,请务必保留此出处http://5453607.blog.51cto.com/5443607/1613059
Criteria和DetachedCriteria的区别与应用
原文地址:http://5453607.blog.51cto.com/5443607/1613059