Classes.java
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 SetgetStudents() {43 return students;44 }45 46 public void setStudents(Set students) {47 this.students = students;48 }49 50 private Set students;51 }
1 2 45 6 407 98 10 11 12 30 31 34 3935 3736 38
Course.java
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 SetgetStudents() {36 return students;37 }38 39 public void setStudents(Set students) {40 this.students = students;41 }42 43 private Set students;44 }
1 2 4 78 9 3110 1211 13 14 18 19 3020 23 25 2824 29
Student.java
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 Setcourses = new HashSet (0);14 15 public Set getCourses() {16 return courses;17 }18 public void setCourses(Set 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 }
1 2 45 6 407 98 10 11 12 30 31 34 3935 3736 38
Hibernate.cfg.xml
1 2 56 9 10 13 51root 14 1516 com.mysql.jdbc.Driver17 18 19 22friends 23 2627 jdbc:mysql://localhost:3306/mydatabase28 29 36update 37 3839 org.hibernate.dialect.MySQLInnoDBDialect40 41 44true 45true 4647 48 49 50
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 ListqueryAllClasses(){ 28 Session session = sessionFactory.openSession(); 29 List 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 queryClasses_Constructor(){ 42 Session session = sessionFactory.openSession(); 43 List 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 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 queryClasses_Student_EQ(){ 95 Session session = sessionFactory.openSession(); 96 List 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 * @return104 */105 public List queryClasses_Student_INNER(){106 Session session = sessionFactory.openSession();107 List cList = session.createQuery("from Classes c inner join c.students").list();108 session.close();109 return cList;110 }111 112 /**113 * 迫切内连接114 * @return115 */116 public List queryClasses_Student_INNER_FETCH(){117 Session session = sessionFactory.openSession();118 List 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 queryClasses_Student_LeftJoin(){127 Session session = sessionFactory.openSession();128 List 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 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 cList = session.createQuery(hql).list();141 session.close();142 return cList;143 }144 145 /**146 * 带select的查询147 */148 public List 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 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 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 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 cList = session.createQuery(hql).list();176 // Set cset = new HashSet (cList);177 // cList = new ArrayList (cset);178 System.out.println(cList.size());179 // for(Classes classes:cList){180 // System.out.println(classes.getCid());181 // Set students = classes.getStudents();182 // for(Student student:students){183 // System.out.println(student.getSname());184 // Set 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 }