日期:2014-05-20 浏览次数:20760 次
public class Student { private Integer studentId; private String studentName; private Classes classes; }
public class Classes{ private Integer classId; private String className; private Set<Student> students = new HashSet<Student>(); }
<hibernate-mapping package="wxm.beans"> <class name="Student" table="student"> <id name="studentId" type="integer" column="student_id"> <generator class="identity" /> </id> <property name="studentName" length="20" not-null="true" column="student_name" /> <property name="perform" /> <many-to-one name="classes" column="class_id" outer-join="true" class="wxm.beans.Classes" /> </class> </hibernate-mapping>
<hibernate-mapping package="wxm.beans"> <class name="Classes" table="t_class"> <id name="classId" type="integer" column="class_id"> <generator class="identity" /> </id> <property name="className" length="20" not-null="true" column="class_name" /> <set name="students" outer-join="true" inverse="true"> <key column="class_id" /> <one-to-many class="wxm.beans.Student" /> </set> </class> </hibernate-mapping>
@Test public void save() { Classes claz = new Classes("英语班"); classService.addClass(claz); Student student = new Student("王小二"); student.setClasses(classService.getClass(1)); student.setPerform(55.5f); studentService.addStudent(student); }
@Test public void delClass() { classService.getClass(1).getStudents().remove( studentService.getStudent(1)); classService.delClass(1); }