课与学生的关系
course
public class Course {
private Integer courseNo;
private String courseName;
private Student student;
}
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.csx.pojo">
<class name="Course" table="Course_table">
<id name="courseNo" column="course_no">
<generator class="identity"></generator>
</id>
<property name="courseName"/>
<set name="students" inverse="true" cascade="save-update,delete">
<key column="student_id"/>
<one-to-many class="Student"/>
</set>
<many-to-one name="student" class="Student" column="student_id"/>
</class>
</hibernate-mapping>
Student
public class Student {
private Integer id;
private String studentNo;
private String name;
private Set<Course> courses = new HashSet<Course>();
}
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.csx.pojo">
<class name="Student" table="student_table">
<id name="id" type="java.lang.Integer">
<generator class="identity"></generator>
</id>
<property name="studentNo"/>
<property name="name"/>
<set name="courses" inverse="true" cascade="save-update,delete">
<key column="student_id"/>
<one-to-many class="Course"/>
</set>
</class>
</hibernate-mapping>
public void TwoWayOneToMany(){
Session session= HibernateUtil.currentSession();
Transaction tx = session.beginTransaction();
Student student = new Student();
student.setName("wwwwwwwwww");
student.setStudentNo("0000");
session.save(student);
Course oracle = new Course();
oracle.setCourseName("苦逼的oracle");
oracle.setStudent(student);
session.persist(oracle);
Course mysql = new Course();
mysql.setCourseName("魅力四射的mysql");
mysql.setStudent(student);
session.persist(mysql);
tx.commit();
HibernateUtil.closeSession();
}
course_table student_table
|