博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Hibernate 检索-Criteria
阅读量:6081 次
发布时间:2019-06-20

本文共 2642 字,大约阅读时间需要 8 分钟。

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,如需转载请自行联系原作者
你可能感兴趣的文章
Python 05 自定义函数的创建、调用和函数
查看>>
千方百计获取百度网盘下载链接
查看>>
淘宝网页变为繁体,教你如何改回简体
查看>>
网页选项卡
查看>>
数据库的备份与恢复 mysqldump+binlog方式
查看>>
SAMBA服务器的配置
查看>>
Cisco设备配置文件定期备份
查看>>
初次接触Linux
查看>>
mysql行转列(拆分字符串场景)
查看>>
SDL做的一个简单Button
查看>>
php utf8和utf-8的区别
查看>>
近期遇到电脑问题几则
查看>>
lvs nginx 负载均衡
查看>>
Nginx IP纯洁库功能测试
查看>>
H3C MSR 20-10 / 900的×××拨号组网模板
查看>>
Java学习路线
查看>>
恶心的问题: error while loading shared libraries: libstdc++-libc6.2-2.so.3:
查看>>
体验竞争:何以成为家用市场竞争焦点?
查看>>
HttpURLConnection学习
查看>>
开年巨制!千人千面回放技术让你“看到”Flutter用户侧问题
查看>>