一对多 单向
<one-to-many>
通过calss操作student 外键在student表中,所以外键由student维护
<many-to-one>
通过student操作class 外键在student中,所以自动维护student的外键 没有inverse属性

建议用多对一这个方向的操作效率比较高

Student.java

 package cn.itcast.hiberate.sh.domain;

 public class Student {
private Long sid;
private String sname;
private String description;
Classes classes; public Classes getClasses() {
return classes;
}
public void setClasses(Classes classes) {
this.classes = classes;
}
public Long getSid() {
return sid;
}
public void setSid(Long sid) {
this.sid = sid;
}
public Student(String sname, String description) {
super();
this.sname = sname;
this.description = description;
}
public Student() {
// TODO Auto-generated constructor stub
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
} }

Student.hbm.xml

 <?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="cn.itcast.hiberate.sh.domain.Student">
<id name="sid" length="5">
<generator class="increment"></generator>
</id>
<property name="sname" length="20"></property>
<property name="description" length="100"></property>
<!--
多对一
column 外键
-->
<many-to-one name="classes" class="cn.itcast.hiberate.sh.domain.Classes" column="cid" cascade="save-update"></many-to-one>
</class>
</hibernate-mapping>

test.java语句

 package cn.itcast.hibernate.sh.test;

 import java.util.HashSet;
import java.util.Set; import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test; import cn.itcast.hiberate.sh.domain.Classes;
import cn.itcast.hiberate.sh.domain.Student;
import cn.itcast.hibernate.sh.utils.HiberanteUtils; /**
* 3、新建一个班级的时候同时新建一个学生
* 4、已经存在一个班级,新建一个学生,建立学生与班级之间的关系
* 5、已经存在一个学生,新建一个班级,把学生加入到该班级
* 6、把一个学生从一个班级转移到另一个班级
* 7、解析一个班级和一个学生之间的关系
* 8、解除一个班级和一些学生之间的关系
* 9、解除该班级和所有的学生之间的关系
* 10、已经存在一个班级,已经存在一个学生,建立该班级与该学生之间的关系
* 11、已经存在一个班级,已经存在多个学生,建立多个学生与班级之间的关系
* 12、删除学生
* 13、删除班级
* 删除班级的时候同时删除学生
* 在删除班级之前,解除班级和学生之间的关系
* @author Think
*
*/
public class OneToManyTest extends HiberanteUtils{
@Test
public void testSaveStudent_Cascade_Classes_Save(){
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
Student student = new Student();
student.setSname("班秘");
student.setDescription("很神秘的一个工作"); Classes classes = new Classes();
classes.setCname("老毕亲子班");
classes.setDescription("都是老毕的亲戚"); student.setClasses(classes);
// Set<Student> students = new HashSet<Student>();
// students.add(student);
//
// classes.setStudents(students);
session.save(student); transaction.commit();
session.close();
} /**
* 一对多,从多的一端维护关系效率比较高
*/
@Test
public void testSaveStudent_R(){
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction(); Student student = new Student();
student.setSname("这个人");
student.setDescription("很好"); Classes classes = (Classes)session.get(Classes.class, 2L);
student.setClasses(classes); //Set<Student> students = new HashSet<Student>();
//students.add(student); //classes.getStudents().add(student);//在hibernate内部查看的是classes.hbm.xml session.save(student);//在hibernate内部查看的是Student.hbm.xml
transaction.commit();
session.close();
} @Test
public void testRelese_R(){
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
Classes classes = (Classes)session.get(Classes.class, 2L);
Student student7 = (Student)session.get(Student.class, 7L);
Student student8 = (Student)session.get(Student.class, 8L);
student7.setClasses(null);
student8.setClasses(null);       //也可以用以下替代

        Classes classes=(Classes) session.get(Classes.class, 9L);
        Set<Student> stu=classes.getStudents();
        for(Student s:stu)
          s.setClasses(null);

         transaction.commit();
session.close();
}
}
04-29 00:00