这一篇记录的是Hibernate的级联操作,主要是一对多的关系。Hibernate的级联操作可以让我们在保存(或其他的数据库操作)一个对象时,将与其关联的对象同时保存到数据库。假设我们的数据库中有两张表:teacher和student,教师和学生是一对多的关系,即一名教师教多名学生
我们的teacher表的结构如下图所示:
teacher表中有两个字段,id和name
student表的结构如下图所示:
student表中有三个字段,id,学号stu_number和对应的教师id
在MyEclipse中新建一个工程,取名为TestHibernateCascade,项目结构如下图所示:
为项目加入Hibernate支持就不多说了,下面我们新建两个java bean,分别表示教师类和学生类,教师类的代码如下所示:
package com.test.model; import java.util.Set; public class Teacher { private int id; private String name; private Set<Student> students; public Teacher(String name, Set<Student> students) { super(); this.name = name; this.students = students; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Set<Student> getStudents() { return students; } public void setStudents(Set<Student> students) { this.students = students; } }由于一个教师教多名学生,所以我们在教师类Teacher中,定义了一个集合类型的数据,其中装入的是Student类的对象,下面是Student类的代码:
package com.test.model; public class Student { private int id; private String stu_number; private Teacher teacher; public Student(String stu_number, Teacher teacher) { super(); this.stu_number = stu_number; this.teacher = teacher; } public String getStu_number() { return stu_number; } public void setStu_number(String stu_number) { this.stu_number = stu_number; } public int getId() { return id; } public void setId(int id) { this.id = id; } public Teacher getTeacher() { return teacher; } public void setTeacher(Teacher teacher) { this.teacher = teacher; } }Student类中为了跟Teacher类保持多对一的关系,所以在Student中加入了一个Teacher类的对象作为成员变量,下面我们分别为上面的两个类编写hbm文件:
首先是Teacher.hbm.xml,其配置如下代码所示:
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.test.model.Teacher" table="teacher"> <id name="id" column="id" type="int"> <generator class="increment"></generator> </id> <property name="name" type="string"> <column name="name" length="20"></column> </property> <set name="students" cascade="save-update"> <key column="teacher_id"></key> <one-to-many class="com.test.model.Student"/> </set> </class> </hibernate-mapping>上面要注意的是一个set标签,由于一个teacher可以教多名student,所以在Teacher.hbm.xml配置文件中,我们加入了set标签,set标签的name属性值,即为Teacher类中的students成员变量名,cascade属性指明了级联操作的类型,即save和update时,会进行级联操作,在set标签的子标签中,还有key和one-to-many标签,key标签指明了与另一个表关联的外键的字段名,这里是指的student表中的teacher_id字段,one-to-many标签指明了一对多的是哪个类,这里指的Student类
然后是Student.hbm.xml,其配置如下代码所示:
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.test.model.Student" table="student"> <id name="id" column="id" type="int"> <generator class="increment"></generator> </id> <property name="stu_number" type="string"> <column name="stu_number" length="20"></column> </property> <many-to-one name="teacher" class="com.test.model.Teacher" column="teacher_id"> </many-to-one> </class> </hibernate-mapping>这里需要注意的是many-to-one标签,因为多个student对应一个teacher,所以这里的many-to-one标签指明了对应的Teacher类,name属性与Student类中的成员变量teacher对应,column属性则表示与teacher关联的表的字段,为student表中的teacher_id字段
下面用一段代码测试hibernate的级联操作:
package com.test; import java.util.HashSet; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.Transaction; import com.test.model.Student; import com.test.model.Teacher; public class TestMain { private Teacher teacher; private Student stu1; private Student stu2; private void init(){ teacher = new Teacher("zhangsan", new HashSet<Student>()); stu1 = new Student("20150001", teacher); stu2 = new Student("20150002", teacher); teacher.getStudents().add(stu1); teacher.getStudents().add(stu2); } private void test(){ init(); Session session = HibernateSessionFactory.getSession(); Transaction trans = session.beginTransaction(); try { session.save(teacher); trans.commit(); } catch (HibernateException e) { e.printStackTrace(); trans.rollback(); } finally { HibernateSessionFactory.closeSession(); } } public static void main(String[] args) { new TestMain().test(); } }上面的代码很简单,new了一个Teacher类的对象和两个Student类的对象,然后将两个Student关联到Teacher上,最后将Teacher保存到数据库,可以发现控制台的输出如下所示:
很明显,我们只保存了一个Teacher类的对象,但是hibernate框架自动为我们保存了与Teacher类的对象所关联的两个Student类的对象
源码下载点击这里