HIBERNATE一對多配置實(shí)例
1. 環(huán)境:假定班級和學(xué)生是一對多的關(guān)系,班級是一,學(xué)生是多,對應(yīng)表格分別是:zlass ,student 2. 創(chuàng)建Zlass和Student對象 //Zlass對象--------------------------------------------------------- public class Zlass{ private String class_id; private String class_name; private java.util.Set students; public void setId(String id){ this.class_id = id; } public void setClassName(String className){ this.class_name = className; } public void setStudents(java.util.Set students){ this.students = students; } public String getId(){ return class_id; } public String getClassName(){ return class_name; } public java.util.Set getStudents(){ return students; } } //學(xué)生對象 public class Student{ private String student_id; private String name; private Address address; private java.util.Set events; private java.util.Set lessons; private Zlass zlass; public Zlass getZlass(){ return zlass; } public String getStudentId(){ return student_id; } public String getName(){ return name; } public Address getAddress(){ return address; } public java.util.Set getEvents(){ return events; } public java.util.Set getLessons(){ return lessons; } public void setZlass(Zlass zlass){ this.zlass = zlass; } public void setStudentId(String studentId){ this.student_id = studentId; } public void setName(String name){ this.name = name; } public void setAddress(Address address){ this.address = address; } public void setEvents(java.util.Set events){ this.events =events; } public void setLessons(java.util.Set lessons){ this.lessons = lessons; } } //配置文件 //----------Zlass.hbm.xml--------------- <hibernate-mapping package="com.softWork.school"> <class name="Zlass" table="class"> <id name="id" column="id" type="string" length="20"> <generator class="assigned"/> </id> <property name="className" column="class_name" type="string" length="200"/> <set name="students" inverse="false" cascade="all"> <key column="class_id"/> <one-to-many class="Student"/> </set> </class> </hibernate-mapping> //-------------Student.hbm.xml--------------- <hibernate-mapping package="com.softWork.school"> <class name="Student" table="student"> <id name="studentId" column="student_id" type="string" length="20"> <generator class="assigned"/> </id> <property name="name" type="string" length="20"/> <component name="address" class="Address"> <property name="state" column="state" type="string"></property> <property name="city" column="city" type="string"></property> <property name="street" column="street" type="string"></property> </component> <set name="events" inverse="false" cascade="all"> <key column="student_id"></key> <one-to-many class="Event"></one-to-many> </set> <set name="lessons" table="student_lesson"> <key column="student_id"/> <many-to-many class="Lesson" column="lesson_id" /> </set> <many-to-one name="zlass" column="class_id" class="Zlass"/> </class> </hibernate-mapping> 以上使用的是班級一端維護(hù)關(guān)系,并級連操作 3. 使用級連操作數(shù)據(jù) 1) 新增班級 Zlass zlass = new Zlass(); zlass.setId("971002"); zlass.setClassName("機(jī)制97-1班"); session.saveOrUpdate(zlass); 2) 為班級新增學(xué)生 主動端操作: Zlass zlass = (Zlass)session.load(Zlass.class,"971002"); Student student = new Student(); student.setStudentId("005"); student.setName("沒名"); zlass.getStudents().add(student); session.saveOrUpdate(zlass); 被動端操作: Zlass zlass = (Zlass)session.load(Zlass.class,"971002"); Student student = new Student(); student.setStudentId("006"); student.setName("006"); student.setZlass(zlass); session.saveOrUpdate(student); 3) 刪除學(xué)生資料 主動端操作: 主動端除非刪除自己,并設(shè)置了級連才能刪除子對象,否則無法完成 //-----以下代碼將只刪除兩者之間的關(guān)系,即將學(xué)生的class_id設(shè)置為null----- Zlass zlass = (Zlass)session.load(Zlass.class,"971001"); java.util.Iterator iterator = zlass.getStudents().iterator(); if (iterator.hasNext()) zlass.getStudents().remove(iterator.next()); session.saveOrUpdate(zlass); 被動端操作: Student student = (Student)session.load(Student.class,"002"); session.delete(student); 4) 修改學(xué)生資料 通過班級修改學(xué)生資料 Zlass zlass = (Zlass)session.load(Zlass.class,"971002"); java.util.Iterator iterator = zlass.getStudents().iterator(); if (iterator.hasNext()){ Student student = (Student)iterator.next(); student.setName("名字已修改"); } session.saveOrUpdate(zlass); 讀取返回的Set型數(shù)據(jù): java.util.Set set = student.getEvents(); java.util.Iterator iterator = set.iterator(); while(iterator.hasNext()){ evt = (Event)iterator.next(); System.out.println(evt.getContent()); System.out.println(evt.getDate().toString()); } 4. 注意: 如果需要從多放引導(dǎo)到一方,需要在一方配置文件中設(shè)置inverse=”true”參數(shù),以設(shè)定一方到多方的設(shè)定是逆向映射,對關(guān)聯(lián)的逆向端所做的修改將不會被持久化。 Inverse=”true”的表示兩個實(shí)體的關(guān)系由對方去維護(hù)。 5. 推薦配置,一般的將”一”這一端設(shè)置為inverse=”false”,cascade=”all” ,這樣一般的操作只需要在”一”這一端操作,此適合于標(biāo)志性一對多的情況,如銷售單和銷售明細(xì) 如果是非標(biāo)志性一對多,則一般的將inverse=”false”,cascade=”none”,這樣關(guān)系永遠(yuǎn)在主動一端進(jìn)行控制 示范:假如A對B是一對多,當(dāng)A中原來有B1,B2,B3,當(dāng)A中的集合中只包含B1,B2時,那么B中B3的外碼將被設(shè)置為NULL,這個是CASCADE=“FALSE”的情況下的結(jié)果 |
|
來自: dashan_tfsp > 《SSH》