多对多单向
Teacher类中
@ManyToMany
@JoinTable(name="student_teacher",//中间表名
joinColumns=@JoinColumn(name="teacher_id"),//这个类在表中的id
inverseJoinColumns=@JoinColumn(name="student_id")//反转的类在联合表中的id
)
public Set<Student> getStudentSet() {
return studentSet;
}
生成的sql
Hibernate:
create table Student (
id integer not null auto_increment,
name varchar(255),
score integer not null,
primary key (id)
) ENGINE=InnoDB
Hibernate:
create table student_teacher (
teacher_id integer not null,
student_id integer not null,
primary key (teacher_id, student_id)
) ENGINE=InnoDB
Hibernate:
create table Teacher (
id integer not null auto_increment,
name varchar(255),
primary key (id)
) ENGINE=InnoDB
Hibernate:
alter table student_teacher
add constraint FK1iasve8qh9avntdgaxoiguu0u
foreign key (student_id)
references Student (id)
Hibernate:
alter table student_teacher
add constraint FKarpd7go6oi75bga0d1soi9rsq
foreign key (teacher_id)
references Teacher (id)
多对多双向
Teacher类
@ManyToMany
@JoinTable(name="student_teacher",
joinColumns=@JoinColumn(name="teacher_id"),//这个类在表中的id
inverseJoinColumns=@JoinColumn(name="student_id")//反转的类在联合表中的id
)
public Set<Student> getStudentSet() {
return studentSet;
}
Student类
@ManyToMany(mappedBy="studentSet")
public Set<Teacher> getSet() {
return set;
}