HQL经典查询语句

使用Set集合来去除重复元素;或是使用distinct元素


      Iterator iterator = session.createQuery("select distinct c.name from customer ").list().iterator();


      while(iterator.hasnext()){


            String name = (String) it.next() ;


      }


///使用聚集函数


count(); 记录的条数


min();    求最小值


max();    求最大值


avg();    求平均值


sum();    求和


1'查询customer中的所有记录条数


    integer i = (Integer) session.createQuery("select count(*) from customer").uniqueResult();


2'查询customer中的所有客户的平均年龄


    integer i = (Integer) session.createQuery("select avg(c.age) from customer c ").uniqueResult();


3'查询customer中的客户年龄的最大值、最小值


    object [] i = (Integer) session.createQuery("select max(c.age),min(c.age) from customer c ").uniqueResult();


    Integer Maxage = (Integer) i [0];


    Integer Minage = (Integer) i [1];


4'统计customer中的客户的名称数目,忽略重复的姓名


    Integer cout = (Integer) session.createQuery("select count(distinct c.name) from customer c").uniqueResult();


使用分组查询


1'按姓名分组,统计customer中的相同姓名的记录数目


    Iterator iterator = (Integer) session.createQuery("select c.name ,count(c) from customer c group by c.name").list.iterator();


    while(iterator.hasnext()){


          object[] p = (objcet[])iterator.next();


          String name = p[0];


          Integer cout = p[1];


    }


2'按客户分组,统计每个客户的订单数量


    Iterator iterator = session.crateQuery("select c.id ,c.name , count(o) from customer c join c.order o group by c.id ").list().iterator;


    while(iterator.hasnext()){


          object[] p = (objcet[])iterator.next();


          Integer id = p[0]


          String name = p[1];


          Integer cout = p[2];


    }


3'统计每个客户的订单总价


    Iterator iterator = session.crateQuery("select c.id ,c.name,sum(o.price) from customer c join c.order o group by c.id").list.iterator();


    while(iterator.hasnext()){


          object[] p = (objcet[])iterator.next();


          Integer id = p[0]


          String name = p[1];


          Double cout = p[2];


    }


//报表的优化


使用HQL时如果只查询对象的一部分属性不会返回持久化对象


from customer c join c.order o group by c.age;//返回持久化对象,占用缓存。


select c.id , c.name , c.age ,o.id ,o.price from customer c join c.order o group by c.age //返回关系性数据,不占用session的缓存,可以为JVM回收


/HQL子查询的集合函数属性


size() 返回集合中元素的数目


minIndex() 建立索引的集合获得最小的索引


maxIndex() 建立索引的集合获得最大的索引


minElement() 对于包含基本元素的集合获得集合最小的元素


maxElement() 对于包含基本元素的集合获得集合最大的元素


element() 获得集合中的所有元素


///Hibernate的缓存管理


管理一级缓存,不建议使用下列方法来管理一级缓存


evict(Object o)从缓存中清除参数指定的持久化对象


clear()清空缓存中所有的持久化对象


///批量更新和批量删除


批量更新customer表中的年龄大于零的所有记录的AGE字段:


     tx = session.beginTransaction();


     Iterator customer = session.find("from customer c where c.age > 0").iterator();


     while(iterator.hasnext()){


         customer customer = (customer) customer.next();


         customer.setAge(customer.getAge()+1);


     }


     tx.commit();


     session.close();//上述代码对数据库操作效果不好可以使用下列的JDBCAPI直连




     tx = session.beginTranscation();


     Connection conn = session.connection();


     Preparedstatement ps = conn.preparedstatment("update customer set age = age + 1 where age > 0 ");


     ps.executeupdate();


     tx.commit(); 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值