hibernate中的HQL详解(实例)

来源:互联网 发布:淘宝店铺出售0 编辑:程序博客网 时间:2024/06/01 15:53
本例模仿部门与员工之间来进行HQL的各种查询,一共有两张表,一张部门表,一张员工表,下面是具体查询代码:

1.Junit测试

[java] view plain copy
  1. package cn.itcast.a_query;  
  2.   
  3. import org.hibernate.Query;  
  4. import org.hibernate.SessionFactory;  
  5. import org.hibernate.cfg.Configuration;  
  6. import org.hibernate.classic.Session;  
  7. import org.junit.Test;  
  8.   
  9. public class App_hql {  
  10.     private static SessionFactory sf;  
  11.     static{  
  12.         sf=new Configuration()  
  13.         .configure()  
  14.         .addClass(Dept.class)  
  15.         .addClass(Employee.class)  
  16.         .buildSessionFactory();  
  17.     }  
  18.       
  19.     @Test  
  20.     public void all() throws Exception{  
  21.         Session session=sf.openSession();  
  22.         session.beginTransaction();  
  23.         //1.主键查询及区别  
  24.     //  Dept dept=(Dept) session.get(Dept.class, 3);  
  25.         //1.对象导航查询  
  26.     /*  Dept dept=(Dept) session.get(Dept.class, 3); 
  27.         System.out.println(dept.getDeptName()); 
  28.         System.out.println(dept.getEmps());*/  
  29.         //HQL查询  
  30.         //注意:使用HQL查询的时候auto-import="true",要设置true,  
  31.         //  如果是false,写hql的时候,要指定类的全名  
  32.         /*Query q=session.createQuery("from Dept"); 
  33.         System.out.println(q.list());*/  
  34.         //a.查询全部列  
  35.     //  Query q = session.createQuery("from Dept");  //OK  
  36. //      Query q = session.createQuery("select * from Dept");  //NOK, 错误,不支持*  
  37. //      Query q = session.createQuery("select d from Dept d");  // OK  
  38. //      System.out.println(q.list());  
  39.           
  40.         // b. 查询指定的列  【返回对象数据Object[] 】  
  41. //      Query q = session.createQuery("select d.deptId,d.deptName from Dept d");    
  42. //      System.out.println(q.list());  
  43.           
  44.         // c. 查询指定的列, 自动封装为对象  【必须要提供带参数构造器】  
  45. //      Query q = session.createQuery("select new Dept(d.deptId,d.deptName) from Dept d");    
  46. //      System.out.println(q.list());  
  47.           
  48.         // d. 条件查询: 一个条件/多个条件and or/between and/模糊查询  
  49.         // 条件查询: 占位符  
  50.         //  Query q=session.createQuery("from Dept d where deptName=?");  
  51.         //  q.setString(0,"人事部");  
  52.         //  q.setParameter(0,"人事部");  
  53.         //  System.out.println(q.list());  
  54.           
  55.         // 条件查询: 命名参数  
  56.     /*  Query q=session.createQuery("from Dept d where deptId=:myid or deptName=:name"); 
  57.         q.setParameter("myid", 3); 
  58.         q.setParameter("name", "人事部"); 
  59.         System.out.println(q.list()); 
  60.         */  
  61.         // 范围  
  62. //      Query q = session.createQuery("from Dept d where deptId between ? and ?");  
  63. //      q.setParameter(0, 1);  
  64. //      q.setParameter(1, 20);  
  65. //      System.out.println(q.list());  
  66.           
  67.           
  68.         // 模糊  
  69. //      Query q = session.createQuery("from Dept d where deptName like ?");  
  70. //      q.setString(0, "%部%");  
  71. //      System.out.println(q.list());  
  72.           
  73.   
  74.         // e. 聚合函数统计  
  75. //      Query q = session.createQuery("select count(*) from Dept");  
  76. //      Long num = (Long) q.uniqueResult();  
  77. //      System.out.println(num);  
  78.   
  79.         // f. 分组查询  
  80.         //-- 统计t_employee表中,每个部门的人数  
  81.         //数据库写法:SELECT dept_id,COUNT(*) FROM t_employee GROUP BY dept_id;  
  82.         // HQL写法  
  83. //      Query q = session.createQuery("select e.dept, count(*) from Employee e group by e.dept");  
  84. //      System.out.println(q.list());  
  85.           
  86.           
  87.         session.getTransaction().commit();  
  88.         session.close();  
  89.     }  
  90.     //连接查询  
  91.     @Test  
  92.     public void join() throws Exception{  
  93.         Session session=sf.openSession();  
  94.         session.beginTransaction();  
  95.   
  96.         //1) 内连接   【映射已经配置好了关系,关联的时候,直接写对象的属性即可】  
  97.     /*  Query q = session.createQuery("from Dept d inner join d.emps"); 
  98.         System.out.println(q.list());*/  
  99.         //2) 左外连接  
  100. //      Query q = session.createQuery("from Dept d left join d.emps");  
  101.         //3) 右外连接  
  102.     /*  Query q = session.createQuery("from Employee e right join e.dept"); 
  103.         q.list();*/   
  104.         session.getTransaction().commit();  
  105.         session.close();  
  106.     }  
  107.       
  108.     // g. 连接查询 - 迫切连接  
  109.     @Test  
  110.     public void fetch() throws Exception{  
  111.   
  112.         Session session = sf.openSession();  
  113.         session.beginTransaction();  
  114.         //1) 迫切内连接    【使用fetch, 会把右表的数据,填充到左表对象中!】  
  115.     /*  Query q=session.createQuery("from Dept d inner join fetch d.emps"); 
  116.         q.list();*/  
  117.           
  118.   
  119.         //2) 迫切左外连接  
  120.     /*  Query q = session.createQuery("from Dept d left join fetch d.emps"); 
  121.         q.list();*/  
  122.           
  123.         session.getTransaction().commit();  
  124.         session.close();  
  125.     }  
  126.       
  127.     // HQL查询优化  
  128.     @Test  
  129.     public void hql_other(){  
  130.         Session session = sf.openSession();  
  131.         session.beginTransaction();  
  132.         // HQL写死  
  133. //      Query q = session.createQuery("from Dept d where deptId < 10 ");  
  134.           
  135.         // HQL 放到映射文件中  
  136.         Query q = session.getNamedQuery("getAllDept");  
  137.         q.setParameter(010);  
  138.         System.out.println(q.list());  
  139.           
  140.         session.getTransaction().commit();  
  141.         session.close();  
  142.     }  
  143.   
  144. }  

2.Dept.hbm.xml
[html] view plain copy
  1. <?xml version="1.0"?>  
  2. <!DOCTYPE hibernate-mapping PUBLIC   
  3.     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
  4.     "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">  
  5.   
  6. <hibernate-mapping package="cn.itcast.a_query" auto-import="true">  
  7.       
  8.     <class name="Dept" table="t_dept" >  
  9.         <id name="deptId">  
  10.             <generator class="native"></generator>  
  11.         </id>   
  12.         <property name="deptName" length="20"></property>  
  13.           
  14.         <!--   
  15.             集合属性,默认使用懒加载   
  16.             lazy  
  17.                 true 懒加载  
  18.                 extra 懒加载(智能)  
  19.                 false 关闭懒加载  
  20.           
  21.         -->  
  22.          <set name="emps" lazy="extra">  
  23.              <key column="dept_id"></key>  
  24.              <one-to-many class="Employee"/>  
  25.          </set>  
  26.          <!-- 存放sql语句 -->  
  27.     <query name="getAllDept">  
  28.         <![CDATA[ 
  29.             from Dept d where deptId < ? 
  30.         ]]>  
  31.           
  32.     </query>  
  33.       
  34.            
  35.     </class>  
  36.       
  37.   
  38. </hibernate-mapping>  

3.Dept
[java] view plain copy
  1. package cn.itcast.a_query;  
  2.   
  3. import java.util.HashSet;  
  4. import java.util.Set;  
  5.   
  6. public class Dept {  
  7.     private int deptId;  
  8.     private String deptName;  
  9.       
  10.       
  11.       
  12.     public Dept() {  
  13.         super();  
  14.     }  
  15.     public Dept(int deptId, String deptName, Set<Employee> emps) {  
  16.         super();  
  17.         this.deptId = deptId;  
  18.         this.deptName = deptName;  
  19.         this.emps = emps;  
  20.     }  
  21.     public Dept(int deptId, String deptName) {  
  22.         super();  
  23.         this.deptId = deptId;  
  24.         this.deptName = deptName;  
  25.     }  
  26.     // 【一对多】 部门对应的多个员工  
  27.     private Set<Employee> emps = new HashSet<Employee>();  
  28.       
  29.     public int getDeptId() {  
  30.         return deptId;  
  31.     }  
  32.     public void setDeptId(int deptId) {  
  33.         this.deptId = deptId;  
  34.     }  
  35.     public String getDeptName() {  
  36.         return deptName;  
  37.     }  
  38.     public void setDeptName(String deptName) {  
  39.         this.deptName = deptName;  
  40.     }  
  41.     public Set<Employee> getEmps() {  
  42.         return emps;  
  43.     }  
  44.     public void setEmps(Set<Employee> emps) {  
  45.         this.emps = emps;  
  46.     }  
  47.       
  48.       
  49.   
  50. }  

    其他的配置文件比较简单,这里没有列出来,主要是收藏hibernate的HQL查询语句,以及以后怎样使用HQL来查询