Hibernate各类链接、报表查询等

1、检索单个对象
      Query和Criteria都提供了返回单个对象的方法uniqueResult()。先调用setMaxResult(1)方法,把最大检索数目设为1,在调用uniqueResult()方法。
      Hx hx = (Hx)session.createQuery("from Hx").setMaxResults(1).uniqueResult();
      Hx hx = (Hx)session.createCriteria(Hx.class).addOrder(Order.asc("name")).setMaxResults(1).uniqueResult();
      与对象属性绑定
      Hx hx = new Hx();
      hx.setAge("33");
      List list = session.createQuery("from Hx as c where c.age=:age").setProperties(hx).list();
      SQL内链接
      内链接就是传统的链接操做,用join链接关联表,on做为链接条件,where指定其余限定条件的查询,如:
      select hx.name,hx.age,hxhome.home from hx join hxhome on hx.id=hxhome.hxid
      SQL左外链接
      在结果表中包含第一个表中知足的全部纪录,若是是在链接条件上匹配纪录,则第二个表返回相应的值,不然第二个表返回空值。如:
      select hx.name,hx.age,hxhome.home from hx  left join hxhome on hx.id=hxhome.hxid
      SQL右外链接
      在结果表中包含第二个表中知足的全部纪录,若是是在链接条件上匹配纪录,则第一个表返回相应的值,不然第一个表返回空值。如:
      select hx.name,hx.age,hxhome.home from hx  right outer join hxhome on hx.id=hxhome.hxid
数组

2、Hibernate中各类链接
      1.迫切左外链接
      如下两种检索方式是等价的,它们都能同时迫切左外链接类B和类C:
      //HQL迫切左外链接检索方式
      from A a left join fetch a.b b left join fetch a.c c where b is not null and c is not null
      //QBC迫切左外链接检索方式
      List result=session.createCriteria(A.class).setFetchMode("this.b",FetchMode.EAGER).setFetchMode("this.c",FetchMode.EAGER).add(Expression.isNotNull("this.b")).add(Expression.isNotNull("this.c")).list();
      迫切左外链接HQL:
      Session session = HibernateSessionFactory.getSession();
      List list = session.createQuery("from Hxhome c left join fetch c.hx h") .list();
      for(int i=0;i<list.size();i++)
      {
            Hxhome hh = (Hxhome)list.get(i);
            System.out.println("homeid="+hh.getHxid());
            System.out.println("home="+hh.getHome());
            System.out.println("hxname="+hh.getHx().getName());
      }
      迫切左外链接QBC:
      List list = session.createCriteria(Hxhome.class).setFetchMode("hx", FetchMode.EAGER).add(Expression.like("home","h%")).list();
      for(int i=0;i<list.size();i++)
      {
            Hxhome hh = (Hxhome)list.get(i);
            System.out.println("homeid="+hh.getHxid());
            System.out.println("home="+hh.getHome());
            System.out.println("hxname="+hh.getHx().getName());
      }
      QBC中的FetchMode
      FetchMode.DEFAULT:表示采用映射文件中配置的检索策略
      FetchMode.EAGER:覆盖映射文件中配置的检索策略,在程序中显示指定迫切左外链接检索策略
      FetchMode.LAZY:覆盖映射文件中配置的检索策略,在程序中显示指定延迟检索策略
左外链接
      HQL:
      List list = session.createQuery("from Hxhome c left join  c.hx ").list();
      for(int i=0;i<list.size();i++)
      {
            Object[] zmx = (Object[])list.get(i);
            Hx hx = (Hx)zmx[1];
            Hxhome hh = (Hxhome)zmx[0];
            System.out.println("hx="+hx.getName()+"---------hh="+hh.getHome());
      }
      左外链接
      QBC不支持左外链接
      内链接
      HQL:
      List list = session.createQuery("from Hxhome c  join  c.hx ").list();
      for(int i=0;i<list.size();i++)
      {
            Object[] zmx = (Object[])list.get(i);
            Hx hx = (Hx)zmx[1];
            Hxhome hh = (Hxhome)zmx[0];
            System.out.println("hx="+hx.getName()+"---------hh="+hh.getHome());
      }

      QBC:
      List list =session.createCriteria(Hxhome.class).createAlias("hx", "h").add(Expression.like("home","h%")).list();         
      for(Iterator it=list.iterator();it.hasNext();)
      {
            Map map = (Map)it.next();
            Hxhome hh = (Hxhome)map.get("this");
            Hx hx = (Hx)map.get("h");
            ------------------------
      }
      右外链接
      HQL:
      List list = session.createQuery("from Hxhome c right join c.hx h").list();         
      for(Iterator it=list.iterator();it.hasNext();)
      {
            Object[] zmx = (Object[])it.next();
            Hxhome hh = (Hxhome)zmx[0];
            Hx hx = (Hx)zmx[1];
            System.out.print(hh.getHome());
            System.out.println(hx.getName());
      }
      右外链接
      QBC:
      不支持右外链接
      HQL支持各类的链接查询
      一、默认状况
      from Hx c where c.name like ‘h%’
      二、迫切左外链接
      from Hx c left join fetch c.hxhome where c.name like ‘h%’
      三、左链接
      from Hx c left join c.hxhome where c.name like ‘h%’
      四、迫切内链接
      from Hx c join fetch c.hxhome where c.name like ‘h%’
      五、内链接
      from Hx c join c.hxhome where c.name like ‘h%’
      六、右外链接
      from Hx c right join c.hxhome where c.name like ‘h%’ session

3、报表查询
      一、投影查询
      二、使用汇集函数
      三、分组查询 函数

      投影查询
      投影查询:指查询结果仅包含部分实体或实体的部分属性,经过select关键字来实现。select关键字用于选择对象的部分属性,例如:
      Session session = HibernateSessionFactory.getSession();
      Transaction tx = session.beginTransaction();
      Iterator it = session.createQuery("select c.name,c.age,h.home from Hx c join c.homes h where c.id>0 ").list().iterator();
      while(it.hasNext())
      {
            Object[] row = (Object[])it.next();
            String age = (String)row[1];
            String home = (String)row[2];
            System.out.println("|age="+age+"|home="+home);
      }
      tx.commit();
      session.close();
      投影查询
      过滤结果中的重复数据—使用set
      Iterator it = session.createQuery("select c.name from Hx c").iterate();
      Set set = new HashSet();
      while(it.hasNext())
      {
            String it1 = set.iterator();
            while(it1.hasNext())
            {
                  String name1 = (String)it1.next();
                  System.out.println("name1="+name1);
            }
}}}      } fetch

      投影查询也可使用distinct关键字来过滤重复记录
      Session session = HibernateSessionFactory.getSession();
      Transaction tx = session.beginTransaction();
      Iterator it = session.createQuery("select distinct c.name from Hx c").iterate();
      while(it.hasNext())
      {
            String c.ID,c.NAME,o.ORDER_NUMBER
            from CUSTOMERS c inner join ORDERS o
            on c.ID=o.CUSTOMER_ID where o.ORDER_NUMBER like 'T%';
      以上查询语句的查询结果以下:
      +----+------+--------------+
      | ID | NAME | ORDER_NUMBER |
      +----+------+--------------+
      |  1 | Tom  | Tom_Order001 |
      |  1 | Tom  | Tom_Order002 |
      |  1 | Tom  | Tom_Order003 |
      +----+------+--------------+
      Query的list()方法返回的集合中包含三个对象数组类型的元素,每一个对象数组表明以上查询结果的一条记录。 this

      使用汇集函数
      在HQL中能够调用
      Count:统计函数
      Min:求最小值函数
      Max:求最大值函数
      Sum:求和函数
      Avg:求平均数函数 spa

      Count:统计函数     
      Session session = HibernateSessionFactory.getSession();
      Transaction tx = session.beginTransaction();
      Integer count = (Integer)session.createQuery("select count(*) from Hx").uniqueResult();
      System.out.print(count);
      tx.commit();
      session.close();
      Avg:求平均数函数
      Session session = HibernateSessionFactory.getSession();
      Transaction tx = session.beginTransaction();
      Float count = (Float)session.createQuery("select avg(c.id) from Hx c").uniqueResult();
      System.out.print(count);
      tx.commit();
      session.close();
      Sum:求和函数
      Session session = HibernateSessionFactory.getSession();
      Transaction tx = session.beginTransaction();
      Integer count = (Integer)session.createQuery("select sum(c.id) from Hx c").uniqueResult();
      System.out.print(count);
      tx.commit();
      session.close();
      Min:求最小值函数 Max:求最大值函数
      Session session = HibernateSessionFactory.getSession();
      Transaction tx = session.beginTransaction();
      Object[] count = (Object[])session.createQuery("select min(c.age),max(c.age) from Hx c").uniqueResult();
      String min = (String)count[0];
      String max = (String)count[1];
      System.out.print("min="+min+"|max="+max);
      tx.commit();
      session.close(); 对象

      分组查询
      Session session = HibernateSessionFactory.getSession();
      Transaction tx = session.beginTransaction();
      Iterator it = session.createQuery("select c.name,count(c) from Hx c group by c.name").iterate();
      while(it.hasNext())
      {
            Object[] oc = (Object[])it.next();
            String count = (Integer)oc[1];
            System.out.println(name+":"+count);
      }
      tx.commit();
      session.close();  get

相关文章
相关标签/搜索