hql查询语句 内存中的情况,fetch迫切查询关键字
Classes.java
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package cn.itcast.hiberate.sh.domain; 2 3 import java.io.Serializable; 4 import java.util.Set; 5 6 public class Classes implements Serializable{ 7 private Long cid; 8 private String cname; 9 private String description; 10 11 public Classes(){} 12 13 public Classes(String cname,String description){ 14 this.cname = cname; 15 this.description = description; 16 } 17 18 public Long getCid() { 19 return cid; 20 } 21 22 public void setCid(Long cid) { 23 this.cid = cid; 24 } 25 26 public String getCname() { 27 return cname; 28 } 29 30 public void setCname(String cname) { 31 this.cname = cname; 32 } 33 34 public String getDescription() { 35 return description; 36 } 37 38 public void setDescription(String description) { 39 this.description = description; 40 } 41 42 public Set<Student> getStudents() { 43 return students; 44 } 45 46 public void setStudents(Set<Student> students) { 47 this.students = students; 48 } 49 50 private Set students; 51 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <?xml version="1.0" encoding="utf-8"?> 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 4 <hibernate-mapping> 5 <class name="cn.itcast.hiberate.sh.domain.Classes"> 6 <id name="cid" length="5" type="java.lang.Long"> 7 <generator class="increment"></generator> 8 </id> 9 <property name="cname" length="20" type="java.lang.String"></property> 10 11 <property name="description" length="100" type="java.lang.String"></property> 12 <!-- 13 set元素对应类中的set集合 14 通过set元素使classes表与student表建立关联 15 key是通过外键的形式让两张表建立关联 16 one-to-many是通过类的形式让两个类建立关联 17 18 cascade 级联 19 save-update 20 1、当 保存班级的时候,对学生进行怎么样的操作 21 如果学生对象在数据库中没有对应的值,这个时候会执行save操作 22 如果学生对象在数据库中有对应的值,这个时候会执行update操作 23 delete 24 all 25 inverse 维护关系 26 true 不维护关系 27 false 维护关系 28 default false 29 --> 30 <set name="students" cascade="save-update" inverse="true" lazy="false" fetch="join"> 31 <!-- 32 key是用来描述外键 33 --> 34 <key> 35 <column name="cid"></column> 36 </key> 37 <one-to-many class="cn.itcast.hiberate.sh.domain.Student"/> 38 </set> 39 </class> 40 </hibernate-mapping>
Course.java
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package cn.itcast.hiberate.sh.domain; 2 3 import java.io.Serializable; 4 import java.util.Set; 5 6 public class Course implements Serializable{ 7 private Long cid; 8 private String cname; 9 private String description; 10 11 public Long getCid() { 12 return cid; 13 } 14 15 public void setCid(Long cid) { 16 this.cid = cid; 17 } 18 19 public String getCname() { 20 return cname; 21 } 22 23 public void setCname(String cname) { 24 this.cname = cname; 25 } 26 27 public String getDescription() { 28 return description; 29 } 30 31 public void setDescription(String description) { 32 this.description = description; 33 } 34 35 public Set<Student> getStudents() { 36 return students; 37 } 38 39 public void setStudents(Set<Student> students) { 40 this.students = students; 41 } 42 43 private Set<Student> students; 44 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <?xml version="1.0" encoding="utf-8"?> 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 4 <!-- 5 建立表和类的对应关系 6 --> 7 <hibernate-mapping> 8 <class name="cn.itcast.hiberate.sh.domain.Course"> 9 <id name="cid" type="java.lang.Long" length="5"> 10 <generator class="increment"></generator> 11 </id> 12 <property name="cname" type="java.lang.String" length="20"></property> 13 <property name="description" type="java.lang.String" length="50"></property> 14 <!-- 15 table 16 student与course在数据库的层次上通过该表实现关联 17 --> 18 <set name="students" table="student_course"> 19 <key> 20 <!-- 21 在哪个映射文件中,就添加哪个 22 --> 23 <column name="cid"></column> 24 </key> 25 <!-- 26 column表示外键 27 --> 28 <many-to-many class="cn.itcast.hiberate.sh.domain.Student" column="sid"></many-to-many> 29 </set> 30 </class> 31 </hibernate-mapping>
Student.java
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package cn.itcast.hiberate.sh.domain; 2 3 import java.io.Serializable; 4 import java.util.HashSet; 5 import java.util.Set; 6 7 public class Student implements Serializable{ 8 private Long sid; 9 private String sname; 10 11 private Classes classes; 12 13 private Set<Course> courses = new HashSet<Course>(0); 14 15 public Set<Course> getCourses() { 16 return courses; 17 } 18 public void setCourses(Set<Course> courses) { 19 this.courses = courses; 20 } 21 public Classes getClasses() { 22 return classes; 23 } 24 public void setClasses(Classes classes) { 25 this.classes = classes; 26 } 27 public Long getSid() { 28 return sid; 29 } 30 public void setSid(Long sid) { 31 this.sid = sid; 32 } 33 public String getSname() { 34 return sname; 35 } 36 public void setSname(String sname) { 37 this.sname = sname; 38 } 39 public String getDescription() { 40 return description; 41 } 42 public void setDescription(String description) { 43 this.description = description; 44 } 45 private String description; 46 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <?xml version="1.0" encoding="utf-8"?> 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 4 <hibernate-mapping> 5 <class name="cn.itcast.hiberate.sh.domain.Classes"> 6 <id name="cid" length="5" type="java.lang.Long"> 7 <generator class="increment"></generator> 8 </id> 9 <property name="cname" length="20" type="java.lang.String"></property> 10 11 <property name="description" length="100" type="java.lang.String"></property> 12 <!-- 13 set元素对应类中的set集合 14 通过set元素使classes表与student表建立关联 15 key是通过外键的形式让两张表建立关联 16 one-to-many是通过类的形式让两个类建立关联 17 18 cascade 级联 19 save-update 20 1、当 保存班级的时候,对学生进行怎么样的操作 21 如果学生对象在数据库中没有对应的值,这个时候会执行save操作 22 如果学生对象在数据库中有对应的值,这个时候会执行update操作 23 delete 24 all 25 inverse 维护关系 26 true 不维护关系 27 false 维护关系 28 default false 29 --> 30 <set name="students" cascade="save-update" inverse="true" lazy="false" fetch="join"> 31 <!-- 32 key是用来描述外键 33 --> 34 <key> 35 <column name="cid"></column> 36 </key> 37 <one-to-many class="cn.itcast.hiberate.sh.domain.Student"/> 38 </set> 39 </class> 40 </hibernate-mapping>
Hibernate.cfg.xml
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <?xml version='1.0' encoding='utf-8'?> 2 <!DOCTYPE hibernate-configuration PUBLIC 3 "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 4 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 5 <hibernate-configuration> 6 <!-- 7 一个session-factory只能连接一个数据库 8 --> 9 <session-factory> 10 <!-- 11 数据库的用户名 12 --> 13 <property name="connection.username">root</property> 14 15 <property name="connection.driver_class"> 16 com.mysql.jdbc.Driver 17 </property> 18 19 <!-- 20 密码 21 --> 22 <property name="connection.password">friends</property> 23 <!-- 24 url 25 --> 26 <property name="connection.url"> 27 jdbc:mysql://localhost:3306/mydatabase 28 </property> 29 <!-- 30 作用:根据持久化类和映射文件生成表 31 validate 32 create-drop 33 create 34 update 35 --> 36 <property name="hbm2ddl.auto">update</property> 37 38 <property name="hibernate.dialect"> 39 org.hibernate.dialect.MySQLInnoDBDialect 40 </property> 41 <!-- 42 显示hibernate内部生成的sql语句 43 --> 44 <property name="show_sql">true</property> 45 <property name="format_sql">true</property> 46 <mapping resource="cn/itcast/hiberate/sh/domain/Classes.hbm.xml" /> 47 <mapping resource="cn/itcast/hiberate/sh/domain/Student.hbm.xml" /> 48 <mapping resource="cn/itcast/hiberate/sh/domain/Course.hbm.xml" /> 49 50 </session-factory> 51 </hibernate-configuration>
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.Course; 14 import cn.itcast.hiberate.sh.domain.Student; 15 import cn.itcast.hibernate.sh.utils.HiberanteUtils; 16 17 /** 18 * 单表 19 * 20 * 一对多和多对多 21 * 22 * 多表的结合 23 * @author Think 24 * 25 */ 26 public class HQLDao extends HiberanteUtils{ 27 public List<Classes> queryAllClasses(){ 28 Session session = sessionFactory.openSession(); 29 List<Classes> cList = session.createQuery("from Classes").list(); 30 session.close(); 31 return cList; 32 } 33 34 public List queryClasses_Properties(){ 35 Session session = sessionFactory.openSession(); 36 List cList = session.createQuery("select cid,cname from Classes").list(); 37 session.close(); 38 return cList; 39 } 40 41 public List<Classes> queryClasses_Constructor(){ 42 Session session = sessionFactory.openSession(); 43 List<Classes> cList = session.createQuery("select new cn.itcast.hiberate.sh.domain.Classes(cname,description) from Classes").list(); 44 session.close(); 45 return cList; 46 } 47 48 public Classes queryClasses_Condition(){ 49 Session session = sessionFactory.openSession(); 50 Query query = session.createQuery("select new cn.itcast.hiberate.sh.domain.Classes(cname,description) from Classes where cid=:cid"); 51 query.setLong("cid", 1L); 52 Classes classes = (Classes)query.uniqueResult(); 53 System.out.println(classes.getCname()); 54 session.close(); 55 return classes; 56 } 57 58 public Classes queryClasses_Condition_2(){ 59 Session session = sessionFactory.openSession(); 60 Query query = session.createQuery("select new cn.itcast.hiberate.sh.domain.Classes(cname,description) from Classes where cid=?"); 61 query.setLong(0, 1L); 62 Classes classes = (Classes)query.uniqueResult(); 63 System.out.println(classes.getCname()); 64 session.close(); 65 return classes; 66 } 67 68 /** 69 * order by,group by,sun,min,max,avg,having等都适用 70 * @return 71 */ 72 73 /** 74 * 子查询 75 */ 76 public void queryClasses_SubSelect(){ 77 Session session = sessionFactory.openSession(); 78 List<Classes> cList = session.createQuery("from Classes where cid in(select cid from Classes where cid in(1,2,3))").list(); 79 session.close(); 80 } 81 82 public static HQLDao getInstance(){ 83 return new HQLDao(); 84 } 85 86 /*********************************************************************************************/ 87 /** 88 * 一对多 89 * 等值连接 查询出来的机构很差 90 * 内连接 91 * 左外连接 92 * 迫切左外连接 93 */ 94 public List<Classes> queryClasses_Student_EQ(){ 95 Session session = sessionFactory.openSession(); 96 List<Classes> cList = session.createQuery("from Classes c,Student s where c.cid=s.classes.cid").list(); 97 session.close(); 98 return cList; 99 } 100 101 /** 102 * 内连接 103 * @return 104 */ 105 public List<Classes> queryClasses_Student_INNER(){ 106 Session session = sessionFactory.openSession(); 107 List<Classes> cList = session.createQuery("from Classes c inner join c.students").list(); 108 session.close(); 109 return cList; 110 } 111 112 /** 113 * 迫切内连接 114 * @return 115 */ 116 public List<Classes> queryClasses_Student_INNER_FETCH(){ 117 Session session = sessionFactory.openSession(); 118 List<Classes> cList = session.createQuery("from Classes c inner join fetch c.students").list(); 119 session.close(); 120 return cList; 121 } 122 123 /** 124 * 左外连接 125 */ 126 public List<Classes> queryClasses_Student_LeftJoin(){ 127 Session session = sessionFactory.openSession(); 128 List<Classes> cList = session.createQuery("from Classes c left outer join c.students").list(); 129 session.close(); 130 return cList; 131 } 132 133 /** 134 * 迫切左外连接 135 */ 136 public List<Classes> queryClasses_Student_LeftJoin_fetch(){ 137 Session session = sessionFactory.openSession(); 138 String hql = "from Classes c left outer join fetch c.students"; 139 hql = "from Student s left outer join fetch s.classes c"; 140 List<Classes> cList = session.createQuery(hql).list(); 141 session.close(); 142 return cList; 143 } 144 145 /** 146 * 带select的查询 147 */ 148 public List<Classes> queryClasses_Student_Select(){ 149 Session session = sessionFactory.openSession(); 150 String hql = "select new cn.itcast.hiberate.sh.domain.ClassesView(c.cname,s.sname) " + 151 "from Student s left outer join s.classes c"; 152 List<Classes> cList = session.createQuery(hql).list(); 153 session.close(); 154 return cList; 155 } 156 157 /** 158 * 多对多 159 */ 160 public void testQueryCourse_Student(){ 161 Session session = sessionFactory.openSession(); 162 List<Student> studentList = session.createQuery("from Student s inner join fetch s.courses c").list(); 163 session.close(); 164 } 165 166 167 /** 168 * 一对多结合多对多 169 */ 170 public List<Student> queryClasses_Student_Course(){ 171 Session session = sessionFactory.openSession(); 172 String hql = "from Classes cs inner join fetch cs.students s inner join fetch s.courses c"; 173 hql = "from Student s inner join fetch s.classes cs inner join fetch s.courses c"; 174 //hql = "from Classes cs left outer join fetch cs.students s left outer join fetch s.courses c"; 175 List<Student> cList = session.createQuery(hql).list(); 176 // Set<Classes> cset = new HashSet<Classes>(cList); 177 // cList = new ArrayList<Classes>(cset); 178 System.out.println(cList.size()); 179 // for(Classes classes:cList){ 180 // System.out.println(classes.getCid()); 181 // Set<Student> students = classes.getStudents(); 182 // for(Student student:students){ 183 // System.out.println(student.getSname()); 184 // Set<Course> courses = student.getCourses(); 185 // for(Course course:courses){ 186 // System.out.println(course.getCname()); 187 // } 188 // } 189 // } 190 session.close(); 191 return cList; 192 } 193 }