数据库表多对多问题(跪求)
教师可以教多门专业, 专业可以被多个教师教,其中数据库设计为三张表,分别为教师表、专业表、教师和专业的关联表,其中关联表中包含教师号和专业号,分别参照教师表和专业表,现在想实现的功能是加入教师信息和专业信息,可以保存教师教的专业是什么,该如何向关联表插入数据,将教师和专业关联起来?
[解决办法]
教师表 TID 教师信息
专业表 CID 专业信息
关联表 TID CID
[解决办法]
加个中间表,将多对多的主键串起来...同意一楼
[解决办法]
在教师表增加set<专业>,在专业增加set<教师> hibernate配置成多对多。
[解决办法]
教师表 T_TEACHER 专业表T_MAJOR 教师和专业的关联表 T_TEACHER_MAJOR
T_TEACHER(TID,TNAME,TAGE....,TS);
T_MAJOR(MID,MNAME,MDESC....,TS);
T_TEACHER_MAJOR(TID,MID,TS);
教师教的专业是什么:
SELECT T_TEACHER_MAJOR.TID,T_TEACHER.TNAME,T_TEACHER_MAJOR.MID,T_MAJOR.MNAME
FROM T_TEACHER_MAJOR,T_TEACHER,T_MAJOR
WHERE T_TEACHER_MAJOR.TID = T_TEACHER.TID
AND T_TEACHER_MAJOR.MID = T_MAJOR.MID;
<class name="Teacher">
<id name="id" column="TID">
<generator class="native"/>
</id>
<set name="majors" table="Teacher_Major">
<key column="TeacherId"/>
<many-to-many column="MajorId"
class="Major"/>
</set>
</class>
<class name="Major">
<id name="id" column="MID">
<generator class="native"/>
</id>
<set name="techers" inverse="true" table="Teacher_Major">
<key column="MajorId"/>
<many-to-many column="TeacherId"
class="Teacher"/>
</set>
</class>
<class name="Teacher">
<id name="id" column="TID">
<generator class="native"/> </id>
<set name="majors" table="Teacher_Major" inverse="false">
<key column="TeacherId"/>
<many-to-many column="MajorId"
class="Major"/>
</set></class>
public class Teacher {
private int TID;
private List<Major> majors = new ArrayList<Major>();
}
public class Major {
private int MID;
private List<Major> majors = new ArrayList<Major>();
}
public class Teacher {
private int TID;
private List<Major> majors ;}
public class Major {
private int MID;
private List<Major> ;}