hibernate建表一对多 一的一方控制多的方
级联操作,操作class对象的时候 级联操作 student
Classes.java文件
1 package cn.itcast.hiberate.sh.domain; 2 3 import java.util.Set; 4 5 public class Classes { 6 private Long cid; 7 private String cname; 8 private String description; 9 public String getDescription() { 10 return description; 11 } 12 public Classes(String cname, String description) { 13 super(); 14 this.cname = cname; 15 this.description = description; 16 } 17 public Classes(Long cid, String cname, String description, 18 Set<Student> students) { 19 super(); 20 this.cid = cid; 21 this.cname = cname; 22 this.description = description; 23 this.students = students; 24 } 25 public Classes() { 26 // TODO Auto-generated constructor stub 27 } 28 public void setDescription(String description) { 29 this.description = description; 30 } 31 private Set<Student> students; 32 public Long getCid() { 33 return cid; 34 } 35 public void setCid(Long cid) { 36 this.cid = cid; 37 } 38 public String getCname() { 39 return cname; 40 } 41 public void setCname(String cname) { 42 this.cname = cname; 43 } 44 public Set<Student> getStudents() { 45 return students; 46 } 47 public void setStudents(Set<Student> students) { 48 this.students = students; 49 } 50 51 }
Classes.hbm.xml
建立一对多的关系
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"> 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>
Student.java
1 package cn.itcast.hiberate.sh.domain; 2 3 public class Student { 4 private Long sid; 5 private String sname; 6 private String description; 7 public Long getSid() { 8 return sid; 9 } 10 public void setSid(Long sid) { 11 this.sid = sid; 12 } 13 public Student(String sname, String description) { 14 super(); 15 this.sname = sname; 16 this.description = description; 17 } 18 public Student() { 19 // TODO Auto-generated constructor stub 20 } 21 public String getSname() { 22 return sname; 23 } 24 public void setSname(String sname) { 25 this.sname = sname; 26 } 27 public String getDescription() { 28 return description; 29 } 30 public void setDescription(String description) { 31 this.description = description; 32 } 33 34 }
Test.java
1 package cn.itcast.hibernate.sh.test; 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.Session; 9 import org.hibernate.Transaction; 10 import org.hibernate.annotations.Type; 11 import org.junit.Test; 12 13 import cn.itcast.hiberate.sh.domain.Classes; 14 import cn.itcast.hiberate.sh.domain.Student; 15 import cn.itcast.hibernate.sh.utils.HiberanteUtils; 16 17 /** 18 * 1、新建一个班级 19 * 2、新建一个学生 20 * 3、新建一个班级的时候同时新建一个学生 21 * 4、已经存在一个班级,新建一个学生,建立学生与班级之间的关系 22 * 5、已经存在一个学生,新建一个班级,把学生加入到该班级 23 * 6、把一个学生从一个班级转移到另一个班级 24 * 7、解析一个班级和一个学生之间的关系 25 * 8、解除一个班级和一些学生之间的关系 26 * 9、解除该班级和所有的学生之间的关系 27 * 10、已经存在一个班级,已经存在一个学生,建立该班级与该学生之间的关系 28 * 11、已经存在一个班级,已经存在多个学生,建立多个学生与班级之间的关系 29 * 12、删除学生 30 * 13、删除班级 31 * 删除班级的时候同时删除学生 32 * 在删除班级之前,解除班级和学生之间的关系 33 * @author Think 34 * 35 */ 36 public class OneToManySingleTest extends HiberanteUtils{ 37 @Test 38 public void testSaveClasses(){ 39 Session session = sessionFactory.openSession(); 40 Transaction transaction = session.beginTransaction(); 41 Classes classes = new Classes(); 42 classes.setCname("传智上海云一期"); 43 classes.setDescription("很牛"); 44 session.save(classes); 45 transaction.commit(); 46 session.close(); 47 } 48 49 50 51 @Test 52 public void testSaveStudent(){ 53 Session session = sessionFactory.openSession(); 54 Transaction transaction = session.beginTransaction(); 55 Student student = new Student(); 56 student.setSname("班长"); 57 student.setDescription("老牛:很牛"); 58 session.save(student); 59 transaction.commit(); 60 session.close(); 61 } 62 63 @Test 64 public void testSaveClasses_Student(){ 65 Session session = sessionFactory.openSession(); 66 Transaction transaction = session.beginTransaction(); 67 68 Classes classes = new Classes(); 69 classes.setCname("传智上海云二期:"); 70 classes.setDescription("很牛X"); 71 72 Student student = new Student(); 73 student.setSname("班长"); 74 student.setDescription("老牛:很牛X"); 75 76 session.save(student); 77 session.save(classes); 78 transaction.commit(); 79 session.close(); 80 } 81 82 /** 83 * 在保存班级的时候,级联保存学生 84 */ 85 @Test 86 public void testSaveClasses_Cascade_Student_Save(){ 87 Session session = sessionFactory.openSession(); 88 Transaction transaction = session.beginTransaction(); 89 90 Classes classes = new Classes(); 91 classes.setCname("传智上海云三期:"); 92 classes.setDescription("很牛XX"); 93 94 Student student = new Student(); 95 student.setSname("班长"); 96 student.setDescription("老牛:很牛XX"); 97 98 Set<Student> students = new HashSet<Student>(); 99 students.add(student); 100 101 //建立classes与student之间的关联 102 classes.setStudents(students); 103 session.save(classes); 104 transaction.commit(); 105 session.close(); 106 } 107 108 @Test 109 public void testSaveClasses_Cascade_Student_Update(){ 110 Session session = sessionFactory.openSession(); 111 Transaction transaction = session.beginTransaction(); 112 113 Classes classes = new Classes(); 114 classes.setCname("传智上海云四期:"); 115 classes.setDescription("很牛XXX"); 116 117 Student student = (Student)session.get(Student.class, 1L); 118 119 student.setSname("班秘"); 120 121 Set<Student> students = new HashSet<Student>(); 122 students.add(student); 123 124 classes.setStudents(students); 125 126 session.save(classes); 127 transaction.commit(); 128 session.close(); 129 } 130 131 @Test 132 public void testUpdateClasses_Cascade_Student_Save(){ 133 Session session = sessionFactory.openSession(); 134 Transaction transaction = session.beginTransaction(); 135 Classes classes = (Classes)session.get(Classes.class, 5L); 136 Student student = new Student(); 137 student.setSname("班花"); 138 student.setDescription("稀有人物"); 139 classes.getStudents().add(student); 140 transaction.commit(); 141 session.close(); 142 } 143 144 @Test 145 public void testUpdateClasses_Cascade_Student_Update(){ 146 Session session = sessionFactory.openSession(); 147 Transaction transaction = session.beginTransaction(); 148 Classes classes = (Classes)session.get(Classes.class, 5L); 149 Set<Student> students = classes.getStudents();//为cid为5的班级的所有的学生 150 // for(Student student:students){ 151 // student.setDescription("压力山大"); 152 // } 153 transaction.commit(); 154 session.close(); 155 } 156 157 /** 158 * 一个错误的演示 159 */ 160 @Test 161 public void testSaveClasses_Cascade_Student_Save_Error(){ 162 Session session = sessionFactory.openSession(); 163 Transaction transaction = session.beginTransaction(); 164 165 Classes classes = new Classes(); 166 classes.setCname("传智上海云六期:"); 167 classes.setDescription("很牛XXXXXX"); 168 169 Student student = new Student(); 170 student.setSname("班长XXXXXX"); 171 student.setDescription("老牛:很牛XXXXXX"); 172 173 Set<Student> students = new HashSet<Student>(); 174 students.add(student); 175 176 //建立classes与student之间的关联 177 classes.setStudents(students); 178 session.save(classes); 179 transaction.commit(); 180 session.close(); 181 } 182 183 184 /** 185 * 已经存在一个班级,新建一个学生,建立学生与班级之间的关系 186 * 通过更新班级级联保存学生 cascade 187 * 建立班级和学生之间的关系 inverse 188 */ 189 @Test 190 public void testSaveStudent_R_1(){ 191 Session session = sessionFactory.openSession(); 192 Transaction transaction = session.beginTransaction(); 193 Student student = new Student(); 194 student.setSname("技术班长"); 195 student.setDescription("大神"); 196 Classes classes = (Classes)session.get(Classes.class, 1L); 197 198 classes.getStudents().add(student); 199 200 transaction.commit(); 201 session.close(); 202 } 203 204 /** 205 * Hibernate: select classes0_.cid as cid0_0_, classes0_.cname as cname0_0_, classes0_.description as descript3_0_0_ from Classes classes0_ where classes0_.cid=? 206 Hibernate: select max(sid) from Student 207 Hibernate: select students0_.cid as cid0_1_, students0_.sid as sid1_, students0_.sid as sid1_0_, students0_.sname as sname1_0_, students0_.description as descript3_1_0_ from Student students0_ where students0_.cid=? 208 Hibernate: insert into Student (sname, description, sid) values (?, ?, ?) 209 更新关系的操作 210 Hibernate: update Student set cid=? where sid=? 211 */ 212 @Test 213 public void testSaveStudent_R_2(){ 214 Session session = sessionFactory.openSession(); 215 Transaction transaction = session.beginTransaction(); 216 Student student = new Student(); 217 student.setSname("技术班长"); 218 student.setDescription("大神"); 219 Classes classes = (Classes)session.get(Classes.class, 1L); 220 221 session.save(student); 222 223 classes.getStudents().add(student); 224 225 transaction.commit(); 226 session.close(); 227 } 228 229 /** 230 * 已经存在一个学生,新建一个班级,把学生加入到该班级 231 */ 232 @Test 233 public void testSaveClasses_R(){ 234 Session session = sessionFactory.openSession(); 235 Transaction transaction = session.beginTransaction(); 236 237 Classes classes = new Classes(); 238 classes.setCname("老毕基础加强班"); 239 classes.setDescription("必看,杀手锏"); 240 241 Student student = (Student)session.get(Student.class, 2L); 242 Set<Student> students = new HashSet<Student>(); 243 students.add(student); 244 classes.setStudents(students); 245 246 session.save(classes); 247 transaction.commit(); 248 session.close(); 249 } 250 251 /** 252 * 把一个学生从一个班级转移到另一个班级 253 * Hibernate: select classes0_.cid as cid0_0_, classes0_.cname as cname0_0_, classes0_.description as descript3_0_0_ from Classes classes0_ where classes0_.cid=? 254 Hibernate: select classes0_.cid as cid0_0_, classes0_.cname as cname0_0_, classes0_.description as descript3_0_0_ from Classes classes0_ where classes0_.cid=? 255 Hibernate: select student0_.sid as sid1_0_, student0_.sname as sname1_0_, student0_.description as descript3_1_0_ from Student student0_ where student0_.sid=? 256 Hibernate: select students0_.cid as cid0_1_, students0_.sid as sid1_, students0_.sid as sid1_0_, students0_.sname as sname1_0_, students0_.description as descript3_1_0_ from Student students0_ where students0_.cid=? 257 Hibernate: select students0_.cid as cid0_1_, students0_.sid as sid1_, students0_.sid as sid1_0_, students0_.sname as sname1_0_, students0_.description as descript3_1_0_ from Student students0_ where students0_.cid=? 258 Hibernate: update Student set cid=null where cid=? and sid=? 259 Hibernate: update Student set cid=? where sid=? 260 */ 261 @Test 262 public void testTransformClass(){ 263 Session session = sessionFactory.openSession(); 264 Transaction transaction = session.beginTransaction(); 265 //Classes classes5 = (Classes)session.get(Classes.class, 5L); 266 Classes classes6 = (Classes)session.get(Classes.class, 6L); 267 Student student = (Student)session.get(Student.class, 1L); 268 //classes5.getStudents().remove(student); 269 classes6.getStudents().add(student); 270 transaction.commit(); 271 session.close(); 272 } 273 274 /** 275 * 解除一个班级和一些学生之间的关系 276 */ 277 278 @Test 279 public void testR_Some(){ 280 Session session = sessionFactory.openSession(); 281 Transaction transaction = session.beginTransaction(); 282 Classes classes = (Classes)session.get(Classes.class, 1L); 283 Set<Student> students = classes.getStudents(); 284 // for(Student student:students){ 285 // if(student.getSid().longValue()==6||student.getSid().longValue()==7){ 286 // students.remove(student); 287 // } 288 // } 289 //set-->list 290 List<Student> sList = new ArrayList<Student>(students); 291 for(int i=0;i<sList.size();i++){ 292 if(sList.get(i).getSid().longValue()==6||sList.get(i).getSid().longValue()==7){ 293 sList.remove(sList.get(i)); 294 i--; 295 } 296 } 297 298 students = new HashSet<Student>(sList); 299 classes.setStudents(students); 300 /** 301 * 增强for循环只能修改一次 302 * 1、用普通的for循环 303 * 2、新建一个set集合,把原来的set集合要保留的数据存放到新的set集合中 304 */ 305 transaction.commit(); 306 session.close(); 307 } 308 309 /* 310 * classes.setStudents(null);直接把班级针对student的集合设置为null 311 */ 312 @Test 313 public void testRealseAll(){ 314 Session session = sessionFactory.openSession(); 315 Transaction transaction = session.beginTransaction(); 316 Classes classes = (Classes)session.get(Classes.class, 1L); 317 // Set<Student> students = classes.getStudents(); 318 // students.clear(); 319 classes.setStudents(null); 320 transaction.commit(); 321 session.close(); 322 } 323 324 /** 325 * 已经存在一个班级,已经存在一个学生,建立该班级与该学生之间的关系 326 * 11、已经存在一个班级,已经存在多个学生,建立多个学生与班级之间的关系 327 */ 328 329 @Test 330 public void testDeleteStudent(){ 331 Session session = sessionFactory.openSession(); 332 Transaction transaction = session.beginTransaction(); 333 Student student = (Student)session.get(Student.class, 8L); 334 session.delete(student); 335 transaction.commit(); 336 session.close(); 337 } 338 339 340 //这个时候必须保证inverse="false"才能保证能够维持 Classes和Student之间的关系, 如果为inverse="true"并且cascade="save-update"会报错,外键 //约束错误 341 @Test 342 public void testDeleteClasses(){ 343 Session session = sessionFactory.openSession(); 344 Transaction transaction = session.beginTransaction(); 345 Classes classes = (Classes)session.get(Classes.class, 5L); 346 //classes.setStudents(null); //因为inverse="true"所以,这句话对数据库没作用,没有关联关系了 347 session.delete(classes); 348 transaction.commit(); 349 session.close(); 350 } 351
//这个时候用到了 Classes.hbm.xml中的级联属性 cascade="delete"(用all就包括它们两个属性)inverse="true"也是没有关系的
352 @Test
353 public void testDeleteClasses_Cascade(){
354 Session session = sessionFactory.openSession();
355 Transaction transaction = session.beginTransaction();
356 Classes classes = (Classes)session.get(Classes.class, 5L);
357 session.delete(classes);
358 transaction.commit();
359 session.close();
360 }
361
362 }