1 创建数据表
drop table studentm;
create table studentm
(
sid number(5) primary key,
sname varchar2(20) not null
)
drop table teacherm;
create table teacherm
(
tid number(5) primary key,
tname varchar2(20) not null
)
drop table teacherm_studentm;
--组合PK (不需要创建关系)
create table teacherm_studentm
(
msid number(5) not null,
mtid number(5) not null,
primary key(msid,mtid)
)
select * from teacherm;
select * from studentm;
select * from teacherm_studentm
create sequence seq_ssid
start with 1
cache 20;
create sequence seq_tid
start with 1000
cache 20;
2.创建实体类及映射文件
Studentm.java
//学生有多个老师 getXXX and setXXx
private Set<Teacherm> teacherms = new HashSet<Teacherm>();
// Constructors
public Set<Teacherm> getTeacherms() {
return teacherms;
}
public void setTeacherms(Set<Teacherm> teacherms) {
this.teacherms = teacherms;
}
Studentm.hbm.xml
<span style="color:#393939;"> <class name="com.hlx.manytomany.Studentm" table="STUDENTM" schema="SCOTT">
<id name="sid" type="java.lang.Integer">
<column name="SID" precision="5" scale="0" />
<generator class="sequence" >
<param name="sequence">seq_ssid</param>
</generator>
</id>
<property name="sname" type="java.lang.String">
<column name="SNAME" length="20" not-null="true" />
</property>
<!-- many to many table="第三张表(不能映射)" inverse="true" 控制权利交给老师-->
<set name="teacherms" table="teacherm_studentm" inverse="true">
<!-- pk 第三张表对应学生表的fk-->
<key column="msid"/>
<many-to-many class="com.hlx.manytomany.Teacherm" column="mtid"/>
</set>
</class>
Teacherm.java
//老师有多个学生 getXXX and setXXx
private Set<Studentm> studentms = new HashSet<Studentm>();
// Constructors
public Set<Studentm> getStudentms() {
return studentms;
}
public void setStudentms(Set<Studentm> studentms) {
this.studentms = studentms;
}
Teacherm.hbm.xml
<class name="com.hlx.manytomany.Teacherm" table="TEACHERM" schema="SCOTT">
<id name="tid" type="java.lang.Integer">
<column name="TID" precision="5" scale="0" />
<generator class="sequence" >
<param name="sequence">seq_tid</param>
</generator>
</id>
<property name="tname" type="java.lang.String">
<column name="TNAME" length="20" not-null="true" />
</property>
<!-- many to many table="第三张表(不能映射)" cascade="all"级联关系 -->
<set name="studentms" table="teacherm_studentm" cascade="all">
<!-- pk 第三张表对应老师表的fk-->
<key column="mtid"/>
<many-to-many class="com.hlx.manytomany.Studentm" column="msid"/>
</set>
</class>
3.测试添加
@Test
public void save(){
//1)获得会话
Session session = HibernateUtil.currentSession();
//事务对象
Transaction tx = session.beginTransaction();
//学生对象
Studentm stu1 = new Studentm("张三");
Studentm stu2 = new Studentm("沈彦");
Studentm stu3 = new Studentm("王五");
Studentm stu4= new Studentm("刘飞");
//老师对象
Teacherm t1 = new Teacherm("祝一帆");
Teacherm t2 = new Teacherm("许路阳");
//学生包含老师
stu1.getTeacherms().add(t1);
stu2.getTeacherms().add(t1);
stu3.getTeacherms().add(t2);
stu4.getTeacherms().add(t2);
//老师包含学生
t1.getStudentms().add(stu1);
t1.getStudentms().add(stu2);
t2.getStudentms().add(stu3);
t2.getStudentms().add(stu4);
//保存老师
session.save(t1);
session.save(t2);
//提交事务
tx.commit();
//2)关闭会话
HibernateUtil.closeSession();
}
对应的SQL语句:
4.测试删除
@Test
public void del(){
//1)获得会话
Session session = HibernateUtil.currentSession();
//事务对象
Transaction tx = session.beginTransaction();
//加载老师1000
Teacherm t1 =(Teacherm) session.load(Teacherm.class, 1002);
session.delete(t1);
//提交事务
tx.commit();
//2)关闭会话
HibernateUtil.closeSession();
}
对应的SQL语句:
总之:
如果inverse=false,也就是自己控制,为主控方!
两个实体类只进行了老师维护关联关系cascade=all,而学生将控制权交给老师这方来管理inverse=true;
再将Studentm实体对象添加到Teacherm的studentms属性集合中时才能更新维护中间表。也就是只要添加老师,或删除老师就OK了!