首页 > 代码库 > hql查询语句 内存中的情况,fetch迫切查询关键字

hql查询语句 内存中的情况,fetch迫切查询关键字

 

  1 package cn.itcast.hibernate.sh.dao;
  2 
  3 import java.util.ArrayList;
  4 import java.util.HashSet;
  5 import java.util.List;
  6 import java.util.Set;
  7 
  8 import org.hibernate.Query;
  9 import org.hibernate.Session;
 10 import org.junit.Test;
 11 
 12 import cn.itcast.hiberate.sh.domain.Classes;
 13 import cn.itcast.hiberate.sh.domain.Student;
 14 import cn.itcast.hibernate.sh.utils.HiberanteUtils;
 15 
 16 /**
 17  * 单表
 18  * 
 19  * ????多和多对??
 20  * 
 21  * 多表的结??
 22  * @author Think
 23  *
 24  */
 25 public class HQLDao extends HiberanteUtils{
 26     public List<Classes> queryAllClasses(){
 27         Session session = sessionFactory.openSession();
 28         List<Classes> cList = session.createQuery("from Classes").list();
 29         
 30         
 31         
 32         session.close();
 33         for(Classes c:cList)
 34             System.out.println(c.getCid());
 35         return cList;
 36     }
 37     
 38     public List queryClasses_Properties(){
 39         Session session = sessionFactory.openSession();
 40         List cList = session.createQuery("select cid,cname from Classes").list();
 41         session.close();
 42         return cList;
 43     }
 44     
 45     public List<Classes> queryClasses_Constructor(){
 46         Session session = sessionFactory.openSession();
 47         List<Classes> cList = session.createQuery("select new cn.itcast.hiberate.sh.domain.Classes(cname,description) from Classes").list();
 48         session.close();
 49         return cList;
 50     }
 51     
 52     public Classes queryClasses_Condition(){
 53         Session session = sessionFactory.openSession();
 54         Query query = session.createQuery("select new cn.itcast.hiberate.sh.domain.Classes(cname,description) from Classes where cid=:cid");
 55         query.setLong("cid", 1L);
 56         Classes classes = (Classes)query.uniqueResult();
 57         System.out.println(classes.getCname());
 58         session.close();
 59         return classes;
 60     }
 61     
 62     public Classes queryClasses_Condition_2(){
 63         Session session = sessionFactory.openSession();
 64         Query query = session.createQuery("select new cn.itcast.hiberate.sh.domain.Classes(cname,description) from Classes where cid=?");
 65         query.setLong(0, 1L);
 66         Classes classes = (Classes)query.uniqueResult();
 67         System.out.println(classes.getCname());
 68         session.close();
 69         return classes;
 70     }
 71     
 72     /**
 73      * order by,group by,sun,min,max,avg,having等都适用
 74      * @return
 75      */
 76     
 77     /**
 78      * 子查??
 79      */
 80     public void queryClasses_SubSelect(){
 81         Session session = sessionFactory.openSession();
 82         List<Classes> cList = session.createQuery("from Classes where cid in(select cid from Classes where cid in(1,2,3))").list();
 83         session.close();
 84     }
 85     
 86     public static HQLDao getInstance(){
 87         return new HQLDao();
 88     }
 89     
 90     /*********************************************************************************************/
 91        /**
 92         * ??????
 93         *    等??连接          查询出来的机构很?? 
 94         *    内连?? 
 95         *    左外连接  
 96         *    迫切左外连接
 97         */
 98     public List<Classes> queryClasses_Student_EQ(){
 99         Session session = sessionFactory.openSession();
100         List<Classes> cList = session.createQuery("from Classes c,Student s where c.cid=s.classes.cid").list();
101         session.close();
102         return cList;
103     }
104     
105     /**
106      * 内连??
107      * @return
108      */
109     public List<Classes> queryClasses_Student_INNER(){
110         Session session = sessionFactory.openSession();
111         List<Classes> cList = session.createQuery("from Classes c inner join c.students").list();
112         session.close();
113         return cList;
114     }
115     
116     /**
117      * 迫切内连??
118      * @return
119      */
120     public List<Classes> queryClasses_Student_INNER_FETCH(){
121         Session session = sessionFactory.openSession();
122         List<Classes> cList = session.createQuery("from Classes c inner join fetch c.students").list();
123         session.close();
124         return cList;
125     }
126     
127     /**
128      * 左外连接
129      */
130     public List<Classes> queryClasses_Student_LeftJoin(){
131         Session session = sessionFactory.openSession();
132         List<Classes> cList = session.createQuery("from Classes c left outer join c.students").list();
133         session.close();
134         return cList;
135     }
136     
137     /**
138      * 迫切左外连接
139      */
140     public List<Classes> queryClasses_Student_LeftJoin_fetch(){
141         Session session = sessionFactory.openSession();
142         String hql = "from Classes c left outer join fetch c.students";
143         hql = "from Student s left outer join fetch s.classes c";
144         List<Classes> cList = session.createQuery(hql).list();
145         session.close();
146         return cList;
147     }
148     
149     /**
150      * 带select的查??
151      */
152     public List<Classes> queryClasses_Student_Select(){
153         Session session = sessionFactory.openSession();
154         String hql = "select new cn.itcast.hiberate.sh.domain.ClassesView(c.cname,s.sname) " +
155                      "from Student s left outer join s.classes c";
156         List<Classes> cList = session.createQuery(hql).list();
157         session.close();
158         return cList;
159     }
160     
161     /**
162      * 多对??
163      */
164     public void testQueryCourse_Student(){
165         Session session = sessionFactory.openSession();
166         List<Student> studentList = session.createQuery("from Student s inner join fetch s.courses c").list();
167         session.close();
168     }
169     
170     
171     /**
172      * ????多结合多对多
173      */
174     public List<Student> queryClasses_Student_Course(){
175         Session session = sessionFactory.openSession();
176         String hql = "from Classes cs inner join fetch cs.students s inner join fetch s.courses c";
177         hql = "from Student s inner join fetch s.classes cs inner join fetch s.courses c";
178         //hql = "from Classes cs left outer join fetch cs.students s left outer join fetch s.courses c";
179         List<Student> cList = session.createQuery(hql).list();
180 //        Set<Classes> cset = new HashSet<Classes>(cList);
181 //        cList = new ArrayList<Classes>(cset);
182         System.out.println(cList.size());
183 //        for(Classes classes:cList){
184 //            System.out.println(classes.getCid());
185 //            Set<Student> students = classes.getStudents();
186 //            for(Student student:students){
187 //                System.out.println(student.getSname());
188 //                Set<Course> courses = student.getCourses();
189 //                for(Course course:courses){
190 //                    System.out.println(course.getCname());
191 //                }
192 //            }
193 //        }
194         session.close();
195         return cList;
196     }
197 }