标签:hibernate
班级的映射关系表 <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="cn.itcast.domain.Classess"> <id name="cid" length="5" type="java.lang.Long"> <generator class="increment"></generator> </id> <property name="cname" length="20" type="java.lang.String"></property> <property name="description" length="100" type="java.lang.String"></property> <!-- set元素对应类中的set集合 通过set元素使classes表与student表建立关联 key是通过外键的形式让两张表建立关联 one-to-many是通过类的形式让两个类建立关联 cascade 级联 save-update 1、当 保存班级的时候,对学生进行怎么样的操作 如果学生对象在数据库中没有对应的值,这个时候会执行save操作 如果学生对象在数据库中有对应的值,这个时候会执行update操作 delete all inverse 维护关系 true 不维护关系 false 维护关系 default false --> <set name="students" cascade="all" inverse="true"> <!-- key是用来描述外键--> <key> <column name="cid"></column> </key> <one-to-many class="cn.itcast.domain.Student"/> </set> </class> </hibernate-mapping> 学生映射关系表 <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="cn.itcast.domain.Student"> <id name="sid" length="5"> <generator class="increment"></generator> </id> <property name="sname" length="20"></property> <property name="description" length="100"></property> </class> </hibernate-mapping> 1.保存班级和学生 @Test public void testSaveClassess(){ Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); Classess Classess = new Classess(); Classess.setCname("传智上海云一期"); Classess.setDescription("很牛"); session.save(Classess); transaction.commit(); session.close(); } @Test public void testSaveStudent(){ Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); Student student = new Student(); student.setSname("班长"); student.setDescription("老牛:很牛"); session.save(student); transaction.commit(); session.close(); } @Test public void testSaveClassess_Student(){ Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); Classess Classess = new Classess(); Classess.setCname("xxx:"); Classess.setDescription("很牛X"); Student student = new Student(); student.setSname("班长"); student.setDescription("老牛:很牛X"); session.save(student); session.save(Classess); transaction.commit(); session.close(); } 2.cascade-1-保存班级保存学生 1、在classes.hbm.xml文件,针对student进行了级联操作save-update 2、在客户端的代码中,通过班级建立班级与学生之间的关系 3、因为student是一个临时状态的对象 4、在保存班级的时候同时保存学生 /** * 在保存班级的时候,级联保存学生 */ @Test public void testSaveClassess_Cascade_Student_Save(){ Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); Classess Classess = new Classess(); Classess.setCname("xxxxxxxxx2:"); Classess.setDescription("很牛XX"); Student student = new Student(); student.setSname("班长"); student.setDescription("老牛:很牛XX"); Set<Student> students = new HashSet<Student>(); students.add(student); //建立Classess与student之间的关联 Classess.setStudents(students); session.save(Classess); transaction.commit(); session.close(); } @Test public void testSaveClassess_Cascade_Student_Update(){ Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); Classess Classess = new Classess(); Classess.setCname("xxxxxxxx4:"); Classess.setDescription("很牛XXX"); Student student = (Student)session.get(Student.class, 1L); student.setSname("班秘"); Set<Student> students = new HashSet<Student>(); students.add(student); Classess.setStudents(students); session.save(Classess); transaction.commit(); session.close(); } @Test public void testUpdateClassess_Cascade_Student_Save(){ Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); Classess Classess = (Classess)session.get(Classess.class, 5L); Student student = new Student(); student.setSname("班花"); student.setDescription("稀有人物"); Classess.getStudents().add(student); transaction.commit(); session.close(); } 3.cascade-2-保存班级更新学生 @Test public void testUpdateClassess_Cascade_Student_Update(){ Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); Classess Classess = (Classess)session.get(Classess.class, 5L); Set<Student> students = Classess.getStudents();//为cid为5的班级的所有的学生 //for(Student student:students){ //student.setDescription("压力山大"); //} transaction.commit(); session.close(); } 4.更新班级保存或者更新学生-隐式操作异常 在客户端试图通过保存班级保存学生,但是由于在配置文件中针对students没有cascade属性,没有级联,所以导致 classes中的student成为临时状态的对象了,hibernate不允许这种情况出现 把session.save/update一个对象的操作为显示操作,级联对象的操作为隐式操作 /** * 一个错误的演示,映射文件中没有写级联属性 */ @Test public void testSaveClassess_Cascade_Student_Save_Error(){ Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); Classess Classess = new Classess(); Classess.setCname("xxxxxxxx5:"); Classess.setDescription("很牛XXXXXX"); Student student = new Student(); student.setSname("班长XXXXXX"); student.setDescription("老牛:很牛XXXXXX"); Set<Student> students = new HashSet<Student>(); students.add(student); //建立Classess与student之间的关联 Classess.setStudents(students); session.save(Classess); transaction.commit(); session.close(); } 5-关系操作 inverse Classes.hbm.xml <set name="students" inverse="true/false/default"> inverse所在的映射文件classes对classes与student之间的关系是否进行维护 维护关系体现在两点: 1、在映射文件中inverse的值必须是false/default 2、必须在客户端代码上建立两者之间的关系 /** * 已经存在一个班级,新建一个学生,建立学生与班级之间的关系 * 通过更新班级级联保存学生 cascade * 建立班级和学生之间的关系 inverse */ @Test public void testSaveStudent_R_1(){ Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); Student student = new Student(); student.setSname("技术班长"); student.setDescription("大神"); Classess Classess = (Classess)session.get(Classess.class, 1L); Classess.getStudents().add(student); transaction.commit(); session.close(); } /** * Hibernate: select Classess0_.cid as cid0_0_, Classess0_.cname as cname0_0_, Classess0_.description as descript3_0_0_ from Classess Classess0_ where Classess0_.cid=? Hibernate: select max(sid) from Student 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=? Hibernate: insert into Student (sname, description, sid) values (?, ?, ?) 更新关系的操作 Hibernate: update Student set cid=? where sid=? */ @Test public void testSaveStudent_R_2(){ Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); Student student = new Student(); student.setSname("技术班长"); student.setDescription("大神"); Classess Classess = (Classess)session.get(Classess.class, 1L); session.save(student); Classess.getStudents().add(student); transaction.commit(); session.close(); } /** * 已经存在一个学生,新建一个班级,把学生加入到该班级 */ @Test public void testSaveClassess_R(){ Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); Classess Classess = new Classess(); Classess.setCname("老毕基础加强班"); Classess.setDescription("必看,杀手锏"); Student student = (Student)session.get(Student.class, 2L); Set<Student> students = new HashSet<Student>(); students.add(student); Classess.setStudents(students); session.save(Classess); transaction.commit(); session.close(); } /** * 把一个学生从一个班级转移到另一个班级 * Hibernate: select Classess0_.cid as cid0_0_, Classess0_.cname as cname0_0_, Classess0_.description as descript3_0_0_ from Classess Classess0_ where Classess0_.cid=? Hibernate: select Classess0_.cid as cid0_0_, Classess0_.cname as cname0_0_, Classess0_.description as descript3_0_0_ from Classess Classess0_ where Classess0_.cid=? Hibernate: select student0_.sid as sid1_0_, student0_.sname as sname1_0_, student0_.description as descript3_1_0_ from Student student0_ where student0_.sid=? 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=? 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=? Hibernate: update Student set cid=null where cid=? and sid=? Hibernate: update Student set cid=? where sid=? */ @Test public void testTransformClass(){ Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); //Classess Classess5 = (Classess)session.get(Classess.class, 5L); Classess Classess6 = (Classess)session.get(Classess.class, 6L); Student student = (Student)session.get(Student.class, 1L); //Classess5.getStudents().remove(student); Classess6.getStudents().add(student); transaction.commit(); session.close(); } /** * 解除一个班级和一些学生之间的关系 */ @Test public void testR_Some(){ Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); Classess Classess = (Classess)session.get(Classess.class, 1L); Set<Student> students = Classess.getStudents(); //for(Student student:students){ //if(student.getSid().longValue()==6||student.getSid().longValue()==7){ //students.remove(student); //} //} //set-->list //List<Student> sList = new ArrayList<Student>(students); //for(int i=0;i<sList.size();i++){ // if(sList.get(i).getSid().longValue()==6||sList.get(i).getSid().longValue()==7){ // sList.remove(sList.get(i)); // i--; //} //} students = new HashSet<Student>(sList); Classess.setStudents(students); /** * 增强for循环只能修改一次 * 1、用普通的for循环 * 2、新建一个set集合,把原来的set集合要保留的数据存放到新的set集合中 */ transaction.commit(); session.close(); } /* * Classess.setStudents(null);直接把班级针对student的集合设置为null */ @Test public void testRealseAll(){ Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); Classess Classess = (Classess)session.get(Classess.class, 1L); //Set<Student> students = Classess.getStudents(); //students.clear(); Classess.setStudents(null); transaction.commit(); session.close(); } /** * 已经存在一个班级,已经存在一个学生,建立该班级与该学生之间的关系 * 11、已经存在一个班级,已经存在多个学生,建立多个学生与班级之间的关系 */ //cascade=”delete”或”all” @Test public void testDeleteStudent(){ Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); Student student = (Student)session.get(Student.class, 8L); session.delete(student); transaction.commit(); session.close(); }
标签:hibernate
原文地址:http://pengya123.blog.51cto.com/8467424/1811624