HIBERNATE
一对多配置实例
1. 环境:假定班级和学生是一对多的关系,班级是一,学生是多,对应表格分别是:zlass ,student
2. 创建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;
}
}
//学生对象
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>
以上使用的是班级一端维护关系,并级连操作
3. 使用级连操作数据
1) 新增班级
Zlass zlass = new Zlass();
zlass.setId("971002");
zlass.setClassName("机制97-1班");
session.saveOrUpdate(zlass);
2) 为班级新增学生
主动端操作:
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) 删除学生资料
主动端操作:
主动端除非删除自己,并设置了级连才能删除子对象,否则无法完成
//-----以下代码将只删除两者之间的关系,即将学生的class_id设置为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) 修改学生资料
通过班级修改学生资料
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型数据:
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. 注意:
如果需要从多放引导到一方,需要在一方配置文件中设置inverse=”true”参数,以设定一方到多方的设定是逆向映射,对关联的逆向端所做的修改将不会被持久化。
Inverse=”true”的表示两个实体的关系由对方去维护。
5. 推荐配置,一般的将”一”这一端设置为inverse=”false”,cascade=”all” ,这样一般的操作只需要在”一”这一端操作,此适合于标志性一对多的情况,如销售单和销售明细
如果是非标志性一对多,则一般的将inverse=”false”,cascade=”none”,这样关系永远在主动一端进行控制
示范:假如A对B是一对多,当A中原来有B1,B2,B3,当A中的集合中只包含B1,B2时,那么B中B3的外码将被设置为NULL,这个是CASCADE=“FALSE”的情况下的结果