一对多双向关联映射:
* 在一一端的集合上使用<key>,在对方表中加入一个外键指向一一端
* 在多一端采用<many-to-one>
注意:<key>标签指定的外键字段必须和<many-to-one>指定的外键字段一致,否则引用字段的错误
inverse="true" 表示将控制权交给另一方
cascade="save-update" 级联操作,例如学生和班级只用save一个就行。
Classes.java
private int id;
private String name;
private Set students;
Student.java
private int id;
private String name;
private Classes classes;
<hibernate-mapping package="com.demo.hibernate">
<class name="Classes" table="t_classes">
<id name="id">
<generator class="native"/>
</id>
<property name="name"/>
<set name="students" inverse="true" cascade="all">
<key column="classesid"/>
<one-to-many class="Student"/>
</set>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="com.demo.hibernate.Student" table="t_student">
<id name="id">
<generator class="native"/>
</id>
<property name="name"/>
<many-to-one name="classes" column="classesid"/>
</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("10");
session.save(student1);
Student student2 = new Student();
student2.setName("祖儿");
session.save(student2);
Set students = new HashSet();
students.add(student1);
students.add(student2);
Classes classes = new Classes();
classes.setName("2012A");
classes.setStudents(students);
//可以正确保存
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();
Classes classes = new Classes();
classes.setName("2012A");
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 testSave3() {
Session session = null;
try {
session = HibernateUtils.getSession();
session.beginTransaction();
Classes classes = new Classes();
classes.setName("2012A");
Student student1 = new Student();
student1.setName("飞飞");
student1.setClasses(classes);
Student student2 = new Student();
student2.setName("菲菲");
student2.setClasses(classes);
Set students = new HashSet();
students.add(student1);
students.add(student2);
classes.setStudents(students);
//可以正确保存
session.save(classes);
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, 1);
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);
}
}
}
hihernate一对多关联映射(双向Classes<----->Student)
如果在”一“一端维护一对多关联关系,hibernate会发出多余的udpate语句,所以我们一般在多
的一端来维护关联关系
关于inverse属性:
inverse主要用在一对多和多对多双向关联上,inverse可以被设置到集合标签<set>上,
默认inverse为false,所以我们可以从”一“一端和”多“一端维护关联关系,
如果设置成inverse为true,则我们只能从多一端来维护关联关系
注意:inverse属性,只影响数据的存储,也就是持久化
在设置了inverse为true 以后,数据时可以insert 进入对应的表,但是,hibernate不会再发出update 更新操作,因此插入数据库中的数据无效,对应的关系没有办法维护,数据是null
关系交给谁维护,那么,存数据时,最后存入数据库
Cascade:all
inverse和cascade
* inverse是关联关系的控制方向
* cascade操作上的连锁反应