关于Hibernate“多对多”配置问题,该问题的现实模型是学生按课程查询成绩。
有三个类Student、Course、Score。
Student.java
public class Student { private int id;//学号 private String name;//姓名 private Set<Course> courses;//课程集}
public class Course { private int id;//课程号 private String name;课程名 private Set<Student> students;//学生集}
public class Score { private int score;//分数}
<hibernate-mapping> <class name="com.cn.Student" table="t_student" > <id name="id" type="java.lang.Integer"> <column name="ID" length="4" /> <generator class="native"/> </id> <property name="name" type="java.lang.String"> <column name="NAME" /> </property> <set name="courses" lazy="true" cascade="all" table="t_stu_cou_score"> <key column="SID"></key> <many-to-many class="com.cn.Course" column="CID" /> </set> </class></hibernate-mapping>
<hibernate-mapping> <class name="com.cn.Course" table="t_course"> <id name="id" type="java.lang.Integer"> <column name="ID" length="4" /> <generator class="native"/> </id> <property name="name" type="java.lang.String"> <column name="NAME" /> </property> <set name="students" lazy="true" cascade="all" table="t_stu_cou_score"> <key column="CID"></key> <many-to-many class="com.cn.Student" column="SID" /> </set> </class></hibernate-mapping>