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