什么情况下是多对多?
举个例子:一个老师可以教多个学生,每个学生也可以被多个老师教。这就是多对多的情况。
在数据库表中这种关系该怎么设计?很简单,加一张中间表。
设计模型:
table_student id<int> <pk>
table_teacher id<int> <pk>
中间表:
T_S t_id<int> <pk,fk2> s_id<int> <pk,fk1>
比如老师2教学生1,在中间表中就存2,1
老师2教学生3,在中间表中就存2,3
这就可以表示多对多的关系了。
hibernate中XML配置文件的实现:
Student.java:
Teacher.java:
配置文件:
Teacher.hbm.xml:
Student.hbm.xml:
在hibernate.cfg.xml中配置:
<mapping resource="cn/edu/hpu/many2many/Student.hbm.xml"/>
<mapping resource="cn/edu/hpu/many2many/Teacher.hbm.xml"/>
生成的建表语句:
create table t_s (
teacher_id integer not null,
student_id integer not null,
primary key (teacher_id, student_id)
)
create table xm_student (
id integer not null auto_increment,
name varchar(255),
primary key (id)
)
create table xm_teacher (
id integer not null auto_increment,
name varchar(255),
primary key (id)
)
alter table t_s
add index FK1BF687B5893C2 (teacher_id),
add constraint FK1BF687B5893C2
foreign key (teacher_id)
references xm_teacher (id)
alter table t_s
add index FK1BF686ABD4922 (student_id),
add constraint FK1BF686ABD4922
foreign key (student_id)
references xm_student (id)
举个例子:一个老师可以教多个学生,每个学生也可以被多个老师教。这就是多对多的情况。
在数据库表中这种关系该怎么设计?很简单,加一张中间表。
设计模型:
table_student id<int> <pk>
table_teacher id<int> <pk>
中间表:
T_S t_id<int> <pk,fk2> s_id<int> <pk,fk1>
比如老师2教学生1,在中间表中就存2,1
老师2教学生3,在中间表中就存2,3
这就可以表示多对多的关系了。
hibernate中XML配置文件的实现:
Student.java:
package cn.edu.hpu.many2many;
public class Student {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Teacher.java:
package cn.edu.hpu.many2many;
import java.util.HashSet;
import java.util.Set;
import cn.edu.hpu.many2many.Student;
public class Teacher {
private int id;
private String name;
private Set<Student> students=new HashSet<Student>();
//选择set的原因是因为,set互相之间不会有重复的
//跟数据库模型比较匹配
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<Student> getStudents() {
return students;
}
public void setStudents(Set<Student> students) {
this.students = students;
}
}
配置文件:
Teacher.hbm.xml:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="cn.edu.hpu.many2many">
<class name="Teacher" table="xm_teacher" dynamic-update="true">
<!-- 主键 这里的主键名字刚好也是'id'-->
<id name="id" column="id">
<generator class="native"></generator>
</id>
<property name="name"></property>
<set name="students" table="t_s">
<key column="teacher_id"></key><!-- 指定关联字段的名字叫什么 -->
<many-to-many class="cn.edu.hpu.many2many.Student" column="student_id"/>
</set>
</class>
</hibernate-mapping>
Student.hbm.xml:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="cn.edu.hpu.many2many">
<class name="Student" table="xm_student" dynamic-update="true">
<!-- 主键 这里的主键名字刚好也是'id'-->
<id name="id" column="id">
<generator class="native"></generator>
</id>
<property name="name"></property>
</class>
</hibernate-mapping>
在hibernate.cfg.xml中配置:
<mapping resource="cn/edu/hpu/many2many/Student.hbm.xml"/>
<mapping resource="cn/edu/hpu/many2many/Teacher.hbm.xml"/>
生成的建表语句:
create table t_s (
teacher_id integer not null,
student_id integer not null,
primary key (teacher_id, student_id)
)
create table xm_student (
id integer not null auto_increment,
name varchar(255),
primary key (id)
)
create table xm_teacher (
id integer not null auto_increment,
name varchar(255),
primary key (id)
)
alter table t_s
add index FK1BF687B5893C2 (teacher_id),
add constraint FK1BF687B5893C2
foreign key (teacher_id)
references xm_teacher (id)
alter table t_s
add index FK1BF686ABD4922 (student_id),
add constraint FK1BF686ABD4922
foreign key (student_id)
references xm_student (id)
除了生成了xm_student和xm_teacher两张表之外,还生成了一张中间表t_s
转载请注明出处:http://blog.csdn.net/acmman