create table Student (id integer not null auto_increment, name varchar(255), primary key (id)) create table Teacher (id integer not null auto_increment, name varchar(255), primary key (id)) create table t_s (teacherId integer not null, studentId integer not null, primary key (teacherId, studentId)) alter table t_s add index FK1BF6883117471 (teacherId), add constraint FK1BF6883117471 foreign key (teacherId) references Teacher (id) alter table t_s add index FK1BF68E56AEE3 (studentId), add constraint FK1BF68E56AEE3 foreign key (studentId) references Student (id)
如果不加@JoinTable,其实也没关系。结果如下:
create table Student (id integer not null auto_increment, name varchar(255), primary key (id)) create table Teacher (id integer not null auto_increment, name varchar(255), primary key (id)) create table Teacher_Student (Teacher_id integer not null, students_id integer not null, primary key (Teacher_id, students_id)) alter table Teacher_Student add index FK55FA429E1052676F (students_id), add constraint FK55FA429E1052676F foreign key (students_id) references Student (id) alter table Teacher_Student add index FK55FA429E57018E8C (Teacher_id), add constraint FK55FA429E57018E8C foreign key (Teacher_id) references Teacher (id)
差别并不是很明显,只是一个名称上的区别:students_id(我们想象应该是student_id)和studentId(我们自定义的)。
其实还有几种没写,全是双向的,我只是写出了一对一的双向,其他的也差不多。