Hibernate 检索-Criteria

简介:
1. 复合属性的查询
 
   public  void testCriteria(){ 
    Criteria criteria = session.createCriteria(User. class); 
    User user =  new User(); 
    user.setAge(10); 
    NamePk namePk =  new NamePk(); 
    namePk.setFirstName( "zhao"); 
    namePk.setLastName( "guoyu"); 
    user.setNamePk(namePk); 
    user.setEmail( "zhao@163.com"); 
    criteria.add(Example.create(user)); 
    criteria.list(); 
  } 
 
生成的SQL
select this_.firstname as firstname0_0_,
       this_.lastname  as lastname0_0_,
       this_.age       as age0_0_,
       this_.address   as address0_0_,
       this_.tel       as tel0_0_,
       this_.zipcode   as zipcode0_0_,
       this_.email     as email0_0_
  from t_user this_
 where (this_.age = ? and this_.email = ?)
2.

 
   public  void testCriteria2(){ 
    Criteria criteria = session.createCriteria(User. class); 
    criteria.add(Expression.le( "age", 20)); 
    criteria.list(); 
  }
 
生成的SQL
select this_.firstname as firstname0_0_,
       this_.lastname  as lastname0_0_,
       this_.age       as age0_0_,
       this_.address   as address0_0_,
       this_.tel       as tel0_0_,
       this_.zipcode   as zipcode0_0_,
       this_.email     as email0_0_
  from t_user this_
 where this_.age <= ?
 
3.
   public  void testCriteria3(){ 
    Criteria criteria = session.createCriteria(User. class); 
   //  Criteria    addrCriteria = criteria.createCriteria("address"); 
   //  addrCriteria.add(Expression.eq("tel", "189733300")); 
     
    Address address =  new Address(); 
    address.setTel( "112"); 
    criteria.add(Restrictions.eq( "address",address)); 
     
    criteria.list(); 
  } 
 
生成的SQL:
select this_.firstname as firstname0_0_,
       this_.lastname  as lastname0_0_,
       this_.age       as age0_0_,
       this_.address   as address0_0_,
       this_.tel       as tel0_0_,
       this_.zipcode   as zipcode0_0_,
       this_.email     as email0_0_
  from t_user this_
 where (this_.address = ? and this_.tel = ? and this_.zipcode = ?)
4.
public  void testCriteeria3(){ 
    Criteria criteria = session.createCriteria(User. class); 
    criteria.setFirstResult(100); 
    criteria.setMaxResults(3); 
     
    criteria.list(); 
  }
 
生成的sql:
 
select *
  from (select row_.*, rownum rownum_
          from (select this_.firstname as firstname0_0_,
                       this_.lastname  as lastname0_0_,
                       this_.age       as age0_0_,
                       this_.address   as address0_0_,
                       this_.tel       as tel0_0_,
                       this_.zipcode   as zipcode0_0_,
                       this_.email     as email0_0_
                  from t_user this_) row_
         where rownum <= ?)
 where rownum_ > ?
5.
public  void testCriteria4(){ 
    Criteria criteria = session.createCriteria(User. class); 
    criteria.setProjection(Projections.groupProperty( "age")); 
    criteria.list(); 
  }
 
生成的sql:
 
select this_.age as y0_ from t_user this_ group by this_.age
6.
public  void testCriteria5(){ 
    Criteria criteria = session.createCriteria(User. class); 
    criteria.setProjection(Projections.avg( "age")); 
    criteria.list(); 
  }
select avg(this_.age) as y0_ from t_user this_



本文转自 randy_shandong 51CTO博客,原文链接:http://blog.51cto.com/dba10g/246706,如需转载请自行联系原作者
相关文章
|
6月前
|
Java 数据库连接
Hibernate中使用Criteria查询及注解——(Dept.java)
Hibernate中使用Criteria查询及注解——(Dept.java)
|
6月前
|
Java 数据库连接
Hibernate中使用Criteria查询及注解——(Emp.hbm.xml)
Hibernate中使用Criteria查询及注解——(Emp.hbm.xml)
|
6月前
|
Java 数据库连接
Hibernate中使用Criteria查询及注解——( EmpCondition)
Hibernate中使用Criteria查询及注解——( EmpCondition)
|
6月前
|
Java 数据库连接
Hibernate中使用Criteria查询及注解——(DeptTest.java)
Hibernate中使用Criteria查询及注解——(DeptTest.java)
|
6月前
|
Java 数据库连接
Hibernate中使用Criteria查询及注解——(Emp.java)
Hibernate中使用Criteria查询及注解——(Emp.java)
|
6月前
|
SQL Java 数据库连接
Hibernate - QBC和本地SQL对象检索详解
Hibernate - QBC和本地SQL对象检索详解
63 0
|
6月前
|
SQL Java 数据库连接
Hibernate - HQL对象检索详解
Hibernate - HQL对象检索详解
55 0
|
6月前
|
SQL 缓存 Java
Hibernate - 检索策略入门与详解
Hibernate - 检索策略入门与详解
57 0
|
Java 数据库连接
Hibernate中使用Criteria查询及注解——( EmpCondition)
Hibernate中使用Criteria查询及注解——( EmpCondition)
|
Java 数据库连接
Hibernate中使用Criteria查询及注解——(DeptTest.java)
Hibernate中使用Criteria查询及注解——(DeptTest.java)