HQL连接查询

连接查询(重要)
* 内连接
* 外连接(左连接/右连接)
参见:JoinQueryTest.java

内连接有三种:inner join ,join,“=”

Java代码 复制代码
  1. package com.wlh.hibernate;   
  2.   
  3. import java.util.Iterator;   
  4. import java.util.List;   
  5.   
  6. import junit.framework.TestCase;   
  7.   
  8. import org.hibernate.Session;   
  9.   
  10. public class JoinQueryTest extends TestCase{   
  11.        
  12.     /**  
  13.      * 内联结  
  14.      */  
  15.     public void testQuery1() {   
  16.         Session session = null;   
  17.         try {   
  18.             session = HibernateUtils.getSession();   
  19.             session.beginTransaction();   
  20.              //====关键字join==========//   
  21.             List students=session.createQuery("select s.name ,c.name from Student s join s.classes c").list();   
  22.             for(Iterator iter=students.iterator();iter.hasNext();){   
  23.                 Object [] o=(Object [])iter.next();   
  24.                 System.out.println("id="+o[0]+",o[1]"+o[0]);   
  25.             }   
  26.             session.getTransaction().commit();   
  27.         }catch(Exception e) {   
  28.             e.printStackTrace();   
  29.             session.getTransaction().rollback();   
  30.         }finally {   
  31.             HibernateUtils.closeSession(session);   
  32.         }   
  33.     }      
  34.        
  35.     /**  
  36.      * 内联结  
  37.      */  
  38.     public void testQuery2() {   
  39.         Session session = null;   
  40.         try {   
  41.             session = HibernateUtils.getSession();   
  42.             session.beginTransaction();   
  43.              //=======关键字inner join 默认就是内连接,所以inner可以加上,也可以省略======//   
  44.             List students=session.createQuery("select s.name, c.name from Student s inner join s.classes c").list();   
  45.             for(Iterator iter=students.iterator();iter.hasNext();){   
  46.                 Object [] o=(Object [])iter.next();   
  47.                 System.out.println("id="+o[0]+",o[1]"+o[0]);   
  48.             }   
  49.             session.getTransaction().commit();   
  50.         }catch(Exception e) {   
  51.             e.printStackTrace();   
  52.             session.getTransaction().rollback();   
  53.         }finally {   
  54.             HibernateUtils.closeSession(session);   
  55.         }   
  56.     }      
  57.        
  58.     /**  
  59.      * 内联结  
  60.      */  
  61.     public void testQuery3() {   
  62.         Session session = null;   
  63.         try {   
  64.             session = HibernateUtils.getSession();   
  65.             session.beginTransaction();   
  66.             //===========用"="实现内联===========//   
  67.             List students=session.createQuery("select s.name ,c.name from Student s ,Classes c where s.classes.id=c.id").list();   
  68.             for(Iterator iter=students.iterator();iter.hasNext();){   
  69.                 Object [] o=(Object [])iter.next();   
  70.                 System.out.println("id="+o[0]+",o[1]"+o[0]);   
  71.             }   
  72.             session.getTransaction().commit();   
  73.         }catch(Exception e) {   
  74.             e.printStackTrace();   
  75.             session.getTransaction().rollback();   
  76.         }finally {   
  77.             HibernateUtils.closeSession(session);   
  78.         }   
  79.     }      
  80.        
  81.   
  82. [b][color=red]外联接:能够查出2个表之间不相互关联的记录[/color] [/b]   
  83.             
  84.   
  85. <PRE class=java name="code"></PRE>   
  86. <BR>     
  87. <BR>    /**  
  88. <BR>     * 外联结  
  89. <BR>     */  
  90. <BR>    public void testQuery4() {   
  91. <BR>        Session session = null;   
  92. <BR>        try {   
  93. <BR>            session = HibernateUtils.getSession();   
  94. <BR>            session.beginTransaction();   
  95. <BR>            //查询出所有的学生(包括没有班级的学生)   
  96. <BR>            List students=session.createQuery("select s.name ,c.name from Student s  left join s.classes c").list();   
  97. <BR>            for(Iterator iter=students.iterator();iter.hasNext();){   
  98. <BR>                Object [] o=(Object [])iter.next();   
  99. <BR>                System.out.println("id="+o[0]+",o[1]="+o[1]);   
  100. <BR>            }   
  101. <BR>            session.getTransaction().commit();   
  102. <BR>        }catch(Exception e) {   
  103. <BR>            e.printStackTrace();   
  104. <BR>            session.getTransaction().rollback();   
  105. <BR>        }finally {   
  106. <BR>            HibernateUtils.closeSession(session);   
  107. <BR>        }   
  108. <BR>    }      
  109. <BR>       
  110. <BR>       
  111. <BR>       
  112. <BR>    /**  
  113. <BR>     * 外联结  
  114. <BR>     */  
  115. <BR>    public void testQuery5() {   
  116. <BR>        Session session = null;   
  117. <BR>        try {   
  118. <BR>            session = HibernateUtils.getSession();   
  119. <BR>            session.beginTransaction();   
  120. <BR>            //查询出所有的班级,(包括没有学生的班级)   
  121. <BR>            List students=session.createQuery("select s.name ,c.name from Student s  right join s.classes c").list();   
  122. <BR>            for(Iterator iter=students.iterator();iter.hasNext();){   
  123. <BR>                Object [] o=(Object [])iter.next();   
  124. <BR>                System.out.println("id="+o[0]+",o[1]="+o[1]);   
  125. <BR>            }   
  126. <BR>            session.getTransaction().commit();   
  127. <BR>        }catch(Exception e) {   
  128. <BR>            e.printStackTrace();   
  129. <BR>            session.getTransaction().rollback();   
  130. <BR>        }finally {   
  131. <BR>            HibernateUtils.closeSession(session);   
  132. <BR>        }   
  133. <BR>    }      
  134. <BR>       
  135. <BR>       
  136. <BR>}  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值