采用一对多双向关联映射的目的主要是为了主要是为了解决一对多单向关联的缺陷而不是需求驱动的
一对多双向关联的映射方式:
* 在一的一端的集合上采用<key>标签,在多的一端加入一个外键
* 在多的一端采用<many-to-one>标签
!!!注意:<key>标签和<many-to-one>标签加入的字段保持一直,否则会产生数据混乱
inverse属性:
* inverse属性可以用在一对多和多对多双向关联上,inverse属性默认为false,为false表示本端可以维护关系,如果inverse为true,则本端不能维护关系,会交给另一端
维护关系,本端失效。所以一对多关联映射我们通常在多的一端维护关系,让一的一端失效,所以在多的一端中设置为inverse为true
inverse和cascade
* inverse是控制方向上的反转,只影响存储
* cascade是操作上的连锁反映
原理图:
对象模型如下:
public class Student {
private int id;
private String name;
private Classes classes;
}
public class Classes {
private int id;
private String name;
private Set students;
}
一的一端Student.hbm.xml
<hibernate-mapping>
<class name="com.bjpowernode.hibernate.Classes" table="t_classes">
<id name="id">
<generator class="native"/>
</id>
<property name="name"/>
<set name="students" inverse="true"><!-- 设置inverse="true"表明现在这端不管关系了,不维护这个字段了,而交给对端进行管理 -->
<!--
<key column="classesid" not-null="true"/>
-->
<key column="classesid"/>
<one-to-many class="com.bjpowernode.hibernate.Student"/>
</set>
</class>
</hibernate-mapping>
多的一端Student.hbm.xml
<hibernate-mapping>
<class name="com.bjpowernode.hibernate.Student" table="t_student">
<id name="id">
<generator class="native"/>
</id>
<property name="name"/>
<many-to-one name="classes" column="classesid"/>//在多的一端加入<many-to-one>标签,并且<key>标签和<many-to-one>标签加入的字段保持一直,
</class>
</hibernate-mapping>
测试如下:
public class One2ManyTest extends TestCase {
public void testSave1() {
Session session = null;
try {
session = HibernateUtils.getSession();
session.beginTransaction();
Student student1 = new Student();
student1.setName("张三");
Student student2 = new Student();
student2.setName("李四");
Classes classes = new Classes();
classes.setName("动力节点");
Set students = new HashSet();
students.add(student1);
students.add(student2);
classes.setStudents(students);
//抛出TransientObjectException
//因为Student不是Persistent状态的对象,它是Transient状态的对象
session.save(classes);
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally {
HibernateUtils.closeSession(session);
}
}
public void testSave2() {
Session session = null;
try {
session = HibernateUtils.getSession();
session.beginTransaction();
Student student1 = new Student();
student1.setName("张三");
session.save(student1);
Student student2 = new Student();
student2.setName("李四");
session.save(student2);
Classes classes = new Classes();
classes.setName("动力节点");
Set students = new HashSet();
students.add(student1);
students.add(student2);
classes.setStudents(students);
//可以成功保存数据
//但是会发出多余的update语句来维持关系
session.save(classes);
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally {
HibernateUtils.closeSession(session);
}
}
public void testSave3() {
Session session = null;
try {
session = HibernateUtils.getSession();
session.beginTransaction();
Classes classes = new Classes();
classes.setName("动力节点");
session.save(classes);
Student student1 = new Student();
student1.setName("张三");
student1.setClasses(classes);
session.save(student1);
//这种做法是正确的做法,从多的一端来进行存,因为已经控制反转给多的一端了
Student student2 = new Student();
student2.setName("李四");
student2.setClasses(classes);
session.save(student2);
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally {
HibernateUtils.closeSession(session);
}
}
public void testLoad1() {
Session session = null;
try {
session = HibernateUtils.getSession();
session.beginTransaction();
Classes classes = (Classes)session.load(Classes.class, 3);
System.out.println("classes.name=" + classes.getName());
Set students = classes.getStudents();
for (Iterator iter=students.iterator(); iter.hasNext();) {
Student student = (Student)iter.next();
System.out.println("student.name=" +student.getName());
}
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally {
HibernateUtils.closeSession(session);
}
}
public void testLoad2() {
Session session = null;
try {
session = HibernateUtils.getSession();
session.beginTransaction();
Student student = (Student)session.load(Student.class, 1);
System.out.println("student.name=" + student.getName());
System.out.println("student.classes.name=" + student.getClasses().getName());
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally {
HibernateUtils.closeSession(session);
}
}
}