原文地址 http://yifanxiang.blog.163.com/blog/static/5000083420105270522523/
hibernate学习和各种关系总结。
@Entity 定一个类向表的映射关系
@Id 定义此属性为主键
@GeneratedValue 属性为自增长
@OneToOne 一对一的关联
@JoinColumn(name="wifeid") 设定实体属性的名字(属性必需是一个实体) 属性为实体时才可以设置
@OneToOne(mappedBy="wife") (mappedBy="wife") 表示里面的映射关系由属性来关联(关联到自身的主键)
@ManyToOne(cascade={CascadeType.ALL},fetch=FetchType.LAZY) cascade={CascadeType.ALL}当dml时,会对相互关联的也进行操作
fetch=FetchType.LAZY
当执行select操作时,延迟加载
@OneToMany
(mappedBy="school",cascade={CascadeType.ALL},fetch=FetchType.LAZY) 一的一边要加mappedBy="school" CascadeType.PERSIST:级联新增
CascadeType.MERGE:级联合并
CascadeType.REFRESH:级联刷新
CascadeType.REMOVE:级联删除
CascadeType.ALL:以上四种都是
@Transient 指明该属性不被读入数据库中
@Basic 可以不写(即一般属性)
??@Lob 表示被持久化为Blob或Clob类型 ,具体类型由属性的类型决定 char[]-->Clob
byte[]--->Blob
@Colum(updatable=false,name="s_id",nullable=false,lentgh=50,insertable=false,
unique=true) 说明该列,不可以更新,列名为“s_id”,不可以为空,长度为50,;不可以做为insert语句的一个列;
唯一
知识点:
有双向关联时必设mappedBy="";
单向的一对一外键关联:
Husband里有一个老婆,一个老婆有一个husband:
Wife(实体属性) @Entity
id int @Id
@GeneratedValue
name String
Husband(实体属性) @Entity
id int @Id
@GeneratedValue
name String
wife Wife @OneToOne @JoinColumn(name="wifeid")//设定属性名为wifeid(默认为wife_id);还可以通过它加入其它属性。如:约束,长度呀什么的
表名和属性 类型 写入的注释,方法1 另外可加
方法1产生的表信息:
等同:
<hibernate-mapping>
<class name="com.bjsxt.hibernate.Student" dynamic-update="true">
<id name="id">
<generator class="native"></generator>
</id>
<property name="name"></property>
<property name="age" />
<property name="sex" />
<property name="good" type="yes_no"></property>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="com.bjsxt.hibernate.StuIdCard">
<id name="id">
<generator class="native"></generator>
</id>
<property name="num"/>
<many-to-one name="student" column="studentId" unique="true"></many-to-one>
</class>
</hibernate-mapping>
----------------------------------------------------------------------
CREATE TABLE `husband` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(255) default NULL,
`wife_id` int(11) default NULL,
PRIMARY KEY (`id`),
KEY `FKAEEA401B8F955A0A` (`wife_id`),
CONSTRAINT `FKAEEA401B8F955A0A` FOREIGN KEY (`wife_id`) REFERENCES `wife` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk
---------------------------------------------------------------------------------------
CREATE TABLE `wife` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(255) default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk
--------------------------------------加入@JoinColumn(name="wifeid")---------------------------------------------
CREATE TABLE `husband` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(255) default NULL,
`wifeid` int(11) default NULL,
PRIMARY KEY (`id`),
KEY `FKAEEA401B109E78ED` (`wifeid`),
CONSTRAINT `FKAEEA401B109E78ED` FOREIGN KEY (`wifeid`) REFERENCES `wife` (`id`),
) ENGINE=InnoDB DEFAULT CHARSET=gbk
----------------------------------------------------------------------------------------------------------------------------------------------
双向的一对一外键关联:
Husband里有一个老婆:
Wife(实体属性) @Entity @Entity
id int @Id
@GeneratedValue @Id
@GeneratedValue
name String
husband Husband @OneToOne @OneToOne(mappedBy="wife")
Husband(实体属性) @Entity @Entity
id int @Id
@GeneratedValue @Id
@GeneratedValue
name String
wife Wife @OneToOne @OneToOne
@JoinColumn(name="wifeid")
注意mappedBy="wife"而不是
mappedBy="wifeid"
表名和属性 类型 写入的注释,方法1 方法2
方法1产生的表信息:
<hibernate-mapping>
<class name="com.bjsxt.hibernate.Student" dynamic-update="true">
<id name="id">
<generator class="native"></generator>
</id>
<property name="name"></property>
<property name="age" />
<property name="sex" />
<property name="good" type="yes_no"></property>
<one-to-one name="stuIdCard" property-ref="student"></one-to-one>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="com.bjsxt.hibernate.StuIdCard">
<id name="id">
<generator class="native"></generator>
</id>
<property name="num"/>
<many-to-one name="student" column="studentId" unique="true"></many-to-one>
</class>
</hibernate-mapping>
----------------------------------------------------------------------
CREATE TABLE `husband` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(255) default NULL,
`wifeid` int(11) default NULL,
PRIMARY KEY (`id`),
KEY `FKAEEA401B109E78ED` (`wifeid`),
CONSTRAINT `FKAEEA401B109E78ED` FOREIGN KEY (`wifeid`) REFERENCES `wife` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk
---------------------------------------------------------------------------------------
CREATE TABLE `wife` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(255) default NULL,
`husband_id` int(11) default NULL,
PRIMARY KEY (`id`),
KEY `FK2923313F42AFEA` (`husband_id`),
CONSTRAINT `FK2923313F42AFEA` FOREIGN KEY (`husband_id`) REFERENCES `husband` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk
-------------------------------------------------------------------------------------------------------
方法2创建的表:
-----------------------------------------------------------------------------------------------------
CREATE TABLE `husband` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(255) default NULL,
`wifeid` int(11) default NULL,
PRIMARY KEY (`id`),
KEY `FKAEEA401B109E78ED` (`wifeid`),
CONSTRAINT `FKAEEA401B109E78ED` FOREIGN KEY (`wifeid`) REFERENCES `wife` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk
可以看出谁有mappedBy="<T>" 谁有外键 ,并关键到<T>
-----------------------------------------------------------------------------------------------------
CREATE TABLE `wife` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(255) default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk
--------------------------------------------------------------------------------------------------------------------
单向的一对一主键关联()用得不多:
Husband里有一个老婆:
Wife(实体属性) @Entity
id int @Id
@GeneratedValue
name String
Husband(实体属性) @Entity
id int @Id
@GeneratedValue
name String
wife Wife @OneToOne
@PrimaryKeyJoinColumn
表名和属性 类型 写入的注释,方法1 另外可加
方法1产生的表信息:
主键之间有关联:
<hibernate-mapping>
<class name="com.bjsxt.hibernate.Student" dynamic-update="true">
<id name="id">
<generator class="native"></generator>
</id>
<property name="name"></property>
<property name="age" />
<property name="sex" />
<property name="good" type="yes_no"></property>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="com.bjsxt.hibernate.StuIdCard">
<id name="id">
<generator class="foreign">
<param name="property">student</param>
</generator>
</id>
<property name="num"/>
<one-to-one name="student" constrained="true"></one-to-one>
</class>
</hibernate-mapping>
----------------------------------------------------------------------
CREATE TABLE `husband` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(255) default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk
---------------------------------------------------------------------------------------
CREATE TABLE `wife` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(255) default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk
-------------------------------------------------------------------------------------------------------
双向的一对一主键关联(用得不多)
Husband里有一个老婆,wife里有一个husband:
Wife(实体属性) @Entity
id int @Id
@GeneratedValue
name String
husband Husband @OneToOne
@PrimaryKeyJoinColumn
Husband(实体属性) @Entity
id int @Id
@GeneratedValue
name String
wife Wife @OneToOne
@PrimaryKeyJoinColumn
表名和属性 类型 写入的注释,方法1 另外可加
方法1产生的表信息:
主键之间有关联
<hibernate-mapping>
<class name="com.bjsxt.hibernate.Student" dynamic-update="true">
<id name="id">
<generator class="native"></generator>
</id>
<property name="name"></property>
<property name="age" />
<property name="sex" />
<property name="good" type="yes_no"></property>
<one-to-one name="stuIdCard" property-ref="student"></one-to-one>
</class>
</hibernate-mapping>
property-ref="person" 这个属性是对应你要指定到那个字段的引用,不设置时,默认为引用表的主键。
<hibernate-mapping>
<class name="com.bjsxt.hibernate.StuIdCard">
<id name="id">
<generator class="foreign">
<param name="property">student</param>
</generator>
</id>
<property name="num"/>
<one-to-one name="student" constrained="true"></one-to-one>
</class>
</hibernate-mapping>
------------------------------------------------------------------------------------------------------------
CREATE TABLE `husband` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(255) default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk
---------------------------------------------------------------------------------------
CREATE TABLE `wife` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(255) default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk
-------------------------------------------------------------------------------------------------------
双向的一对一主键关联(用得不多)
Husband里有一个老婆,wife里有一个husband,在外面有一个联合的表WifePK:
Wife(实体属性) @Entity
@IdClass(WifePK.class)
id int @Id
name String @Id
husband Husband @OneToOne
@PrimaryKeyJoinColumn
Husband(实体属性) @Entity
id int @Id
@GeneratedValue
name String
wife Wife @OneToOne
@JoinColumns(
{
@JoinColumn(name="wifeId", referencedColumnName="id"),
@JoinColumn(name="wifeName", referencedColumnName="name")
}
)
WifePK(类)
implements Serializable
id 没有
name 没有
表名和属性 类型 写入的注释,方法1 另外可加
方法1产生的表信息:
主键之间有关联
------------------------------------------------------------------------------------------------------------
CREATE TABLE `husband` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(255) default NULL,
`wifeId` int(11) default NULL,
`wifeName` varchar(255) default NULL,
PRIMARY KEY (`id`),
KEY `FKAEEA401BC089B209` (`wifeId`,`wifeName`),
CONSTRAINT `FKAEEA401BC089B209` FOREIGN KEY (`wifeId`, `wifeName`) REFERENCES `wife` (`id`, `name`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk
---------------------------------------------------------------------------------------
CREATE TABLE `wife` (
`id` int(11) NOT NULL,
`name` varchar(255) NOT NULL,
`age` int(11) NOT NULL,
PRIMARY KEY (`id`,`name`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk
-------------------------------------------------------------------------------------------------------
组件映射
Husband里有一个老婆,wife里有一个husband,放在一张表里:
Wife(实体属性) @Entity
id int @Id
@GeneratedValue
name String
husband Husband @Embedded
Husband(实体属性)
age int
wifeName String
表名和属性 类型 写入的注释,方法1 另外可加
方法1产生的表信息:
主键之间有关联
<hibernate-mapping>
<class name="com.bjsxt.hibernate.Husband" >
<id name="id">
<generator class="native"></generator>
</id>
<property name="name"></property>
<component name="wife">
<property name="wifeName"></property>
<property name="age"></property>
</component>
</class>
</hibernate-mapping>
------------------------------------------------------------------------------------------------------------
CREATE TABLE `husband` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(255) default NULL,
`age` int(11) NOT NULL,
`wifeName` varchar(255) default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk
-------------------------------------------------------------------------------------------------------
多对一单向映射
多对一关系一般是在(多的一方加外键)
一个组Group有多个用户User:
Group(实体属性) @Entity
@Table(name="t_group")
id int @Id
@GeneratedValue
name String
User(实体属性)
id int @Id
@GeneratedValue
name String
group Group @ManyToOne
@JoinColumn(name="groupid")
表名和属性 类型 写入的注释,方法1 另外可加
方法1产生的表信息:
<hibernate-mapping>
<class name="com.bjsxt.hibernate.User" table="t_user">
<id name="id">
<generator class="native"></generator>
</id>
<property name="name"></property>
<many-to-one name="group" column="groupId" />
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="com.bjsxt.hibernate.Group" table="t_group">
<id name="id">
<generator class="native"></generator>
</id>
<property name="name"></property>
</class>
</hibernate-mapping>
可以看出@JoinColumn必需在实体属性下(即复杂类型)
外键最好加在多的一方
------------------------------------------------------------------------------------------------------------
CREATE TABLE `t_group` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(255) default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk
-------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------
CREATE TABLE `t_user` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(255) default NULL,
`groupId` int(11) default NULL,
PRIMARY KEY (`id`),
KEY `FKCB63CCB6C3D18669` (`groupId`),
CONSTRAINT `FKCB63CCB6C3D18669` FOREIGN KEY (`groupId`) REFERENCES `t_group` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk
-------------------------------------------------------------------------------------------------------
一对多单向映射
一对多关系一般是在(多的一方加外键)
一个组Group有多个用户User:
Group(实体属性) @Entity
@Table(name="t_group") @Entity
@Table(name="t_group")
id int @Id
@GeneratedValue @Id
@GeneratedValue
name String
users HashSet<User> @OneToMany
@JoinColumn(name="groupId") @OneToMany
User(实体属性) @Entity
@Table(name="t_user") @Entity
@Table(name="t_user")
id int @Id
@GeneratedValue @Id
@GeneratedValue
name String
表名和属性 类型 写入的注释,方法1 另外可加 方法2
方法1产生的表信息:
<hibernate-mapping>
<class name="com.bjsxt.hibernate.User" table="t_user">
<id name="id">
<generator class="native"></generator>
</id>
<property name="name"></property>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="com.bjsxt.hibernate.Group" table="t_group">
<id name="id">
<generator class="native"></generator>
</id>
<property name="name"></property>
<set name="users">
<key column="groupId"></key>
<one-to-many class="com.bjsxt.hibernate.User"/>
</set>
</class>
</hibernate-mapping>
可以看出@JoinColumn必需在实体属性下(即复杂类型)
外键最好加在多的一方
可以看出:@JoinColumn(name="groupId")的列创建的外键是User方(多的一方)。
------------------------------------------------------------------------------------------------------------
CREATE TABLE `t_group` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(255) default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk
-------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------
CREATE TABLE `t_user` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(255) default NULL,
`groupId` int(11) default NULL,
PRIMARY KEY (`id`),
KEY `FKCB63CCB6C3D18669` (`groupId`),
CONSTRAINT `FKCB63CCB6C3D18669` FOREIGN KEY (`groupId`) REFERENCES `t_group` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk
-------------------------------------------------------------------------------------------------------
方法2产生的表信息:
不加@JoinColumn后,会生成3张表;说明@OneToMany在不加@JoinColumn(name="groupId")后会生成一张关联表
------------------------------------------------------------------------------------------------------------
CREATE TABLE `t_group` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(255) default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk
-------------------------------------------------------------------------------------------------------
CREATE TABLE `t_user` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(255) default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk
------------------------------------------------------------------------------------------------------------
CREATE TABLE `t_group_t_user` (
`t_group_id` int(11) NOT NULL,
`users_id` int(11) NOT NULL,
PRIMARY KEY (`t_group_id`,`users_id`),
UNIQUE KEY `users_id` (`users_id`),
KEY `FKCFE61C61872F10D5` (`t_group_id`),
KEY `FKCFE61C612FC1406D` (`users_id`),
CONSTRAINT `FKCFE61C612FC1406D` FOREIGN KEY (`users_id`) REFERENCES `t_user` (`id`),
CONSTRAINT `FKCFE61C61872F10D5` FOREIGN KEY (`t_group_id`) REFERENCES `t_group` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk
-------------------------------------------------------------------------------------------------------
多对一(一对多)双向映射
多对一关系一般是在(多的一方加外键)
一个组Group有多个用户User:
Group(实体属性) @Entity
@Table(name="t_group")
id int @Id
@GeneratedValue
name String
users HashSet<User> @OneToMany
(mappedBy="group") 不加
(mappedBy="group")
User(实体属性)
id int @Id
@GeneratedValue
name String
group Group @ManyToOne
@JoinColumn(name="groupid")
表名和属性 类型 写入的注释,方法1 另外可加 方法2
方法1产生的表信息:
<hibernate-mapping>
<class name="com.bjsxt.hibernate.User" table="t_user">
<id name="id">
<generator class="native"></generator>
</id>
<property name="name"></property>
<many-to-one name="group" column="groupId"></many-to-one>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="com.bjsxt.hibernate.Group" table="t_group">
<id name="id">
<generator class="native"></generator>
</id>
<property name="name"></property>
<set name="users">
<key column="groupId"></key>
<one-to-many class="com.bjsxt.hibernate.User"/>
</set>
</class>
</hibernate-mapping>
主要就是在@OneToMany后加
(mappedBy="group"),
外键最好加在多的一方
------------------------------------------------------------------------------------------------------------
CREATE TABLE `t_group` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(255) default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk
-------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------
CREATE TABLE `t_user` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(255) default NULL,
`groupId` int(11) default NULL,
PRIMARY KEY (`id`),
KEY `FKCB63CCB6C3D18669` (`groupId`),
CONSTRAINT `FKCB63CCB6C3D18669` FOREIGN KEY (`groupId`) REFERENCES `t_group` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk
-------------------------------------------------------------------------------------------------------
证明不加,也不会出错:(在两个表里这样不会出错,但在一个表时最好就会出错(自关联的一对多时))
多对多,单向映射
学生和老师的关系(一个学生有多个老师,一个老师有多个学生):
Student(实体属性) @Entity
@Table(name="s_student")
id int @Id
@GeneratedValue
name String
Teacher(实体属性)
id int @Id
@GeneratedValue
name String
Set<Student> students Group @ManyToMany
表名和属性 类型 写入的注释,方法1 另外可加 方法2
xml写法:
<hibernate-mapping>
<class name="com.bjsxt.hibernate.Student">
<id name="id">
<generator class="native"></generator>
</id>
<property name="name"></property>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="com.bjsxt.hibernate.Teacher">
<id name="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="com.bjsxt.hibernate.Student" column="student_id"/>
</set>
</class>
</hibernate-mapping>
------------------------------------------------------------------------------------------------------------
CREATE TABLE `student` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(255) default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk
-------------------------------------------------------------------------------------------------------
CREATE TABLE `teacher` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(255) default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk
------------------------------------------------------------------------------------------------------------
CREATE TABLE `teacher_student` (
`Teacher_id` int(11) NOT NULL,
`students_id` int(11) NOT NULL,
PRIMARY KEY (`Teacher_id`,`students_id`),
KEY `FK55FA429E78C8936D` (`students_id`),
KEY `FK55FA429EBF77BA8A` (`Teacher_id`),
CONSTRAINT `FK55FA429EBF77BA8A` FOREIGN KEY (`Teacher_id`) REFERENCES `teacher` (`id`),
CONSTRAINT `FK55FA429E78C8936D` FOREIGN KEY (`students_id`) REFERENCES `student` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk
-------------------------------------------------------------------------------------------------------
多对多,双向映射
学生和老师的关系(一个学生有多个老师,一个老师有多个学生):
Student(实体属性) @Entity
@Table(name="s_student")
id int @Id
@GeneratedValue
name String
Set<Teacher> teachers @ManyToMany(mappedBy="students")
Teacher(实体属性)
id int @Id
@GeneratedValue
name String
Set<Student> students Group @ManyToMany
@ManyToMany
@JoinTable(name="t_s",
joinColumns={@JoinColumn(name="teacher_id")},
inverseJoinColumns={@JoinColumn(name="student_id")}
)
表名和属性 类型 写入的注释,方法1 另外可加 方法2
xml写法:
<hibernate-mapping>
<class name="com.bjsxt.hibernate.Student">
<id name="id">
<generator class="native"></generator>
</id>
<property name="name"></property>
<set name="teachers" table="t_s">
<key column="student_id"></key>
<many-to-many class="com.bjsxt.hibernate.Teacher" column="teacher_id"/>
</set>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="com.bjsxt.hibernate.Teacher">
<id name="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="com.bjsxt.hibernate.Student" column="student_id"/>
</set>
</class>
</hibernate-mapping>
------------------------------------------------------------------------------------------------------------
CREATE TABLE `student` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(255) default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk
-------------------------------------------------------------------------------------------------------
CREATE TABLE `teacher` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(255) default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk
------------------------------------------------------------------------------------------------------------
CREATE TABLE `t_s` (
`teacher_id` int(11) NOT NULL,
`student_id` int(11) NOT NULL,
PRIMARY KEY (`student_id`,`teacher_id`),
KEY `FK1BF68BF77BA8A` (`teacher_id`),
KEY `FK1BF68AEDC6FEA` (`student_id`),
CONSTRAINT `FK1BF68AEDC6FEA` FOREIGN KEY (`student_id`) REFERENCES `student` (`id`),
CONSTRAINT `FK1BF68BF77BA8A` FOREIGN KEY (`teacher_id`) REFERENCES `teacher` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk
-------------------------------------------------------------------------------------------------------