hibernate-----many-to-many

hibernate:many-to-many

以学生和老师为例
建表语句:


CREATE DATABASE `manytomany`
CHARACTER SET 'utf8';

USE `manytomany`;

DROP TABLE IF EXISTS `student`;
create table student(
sid int auto_increment primary key,
name varchar(10) not null
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `teacher`;
create table teacher(
tid int auto_increment primary key,
name varchar(10) not null
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `st`;
create table st(
sid int not null,
tid int not null
)ENGINE=InnoDB DEFAULT CHARSET=utf8;

Teacher.java

package com.test.domain;

import java.util.HashSet;
import java.util.Set;

public class Teacher implements java.io.Serializable {

private Integer tid;

private String name;

private Set students = new HashSet();

public Teacher() {
}

public Teacher(String name) {
this.name = name;
}

public Integer getTid() {
return this.tid;
}

public void setTid(Integer tid) {
this.tid = tid;
}

public String getName() {
return this.name;
}

public void setName(String name) {
this.name = name;
}

public Set getStudents() {
return students;
}

public void setStudents(Set students) {
this.students = students;
}



}

Student.java:

package com.test.domain;

import java.util.HashSet;
import java.util.Set;

public class Student implements java.io.Serializable {

private Integer sid;

private String name;

private Set teachers = new HashSet();

public Student() {
}

public Student(String name) {
this.name = name;
}

public Integer getSid() {
return this.sid;
}

public void setSid(Integer sid) {
this.sid = sid;
}

public String getName() {
return this.name;
}

public void setName(String name) {
this.name = name;
}

public Set getTeachers() {
return teachers;
}

public void setTeachers(Set teachers) {
this.teachers = teachers;
}


}


Teacher.hbm.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class name="com.test.domain.Teacher" table="teacher" catalog="manytomany">
<id name="tid" type="java.lang.Integer">
<column name="tid" />
<generator class="native" />
</id>
<property name="name" type="java.lang.String">
<column name="name" length="10" not-null="true" />
</property>
<!--inverse="false": 由teacher操作student -->
<set name="students" table="st" cascade="save-update" inverse="false">
<key column="tid"></key>
<many-to-many class="com.test.domain.Student" column="sid">
</many-to-many>
</set>
</class>
</hibernate-mapping>



Student.hbm.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.test.domain.Student" table="student" catalog="manytomany">
<id name="sid" type="java.lang.Integer">
<column name="sid" />
<generator class="native" />
</id>
<property name="name" type="java.lang.String">
<column name="name" length="10" not-null="true" />
</property>
<!--inverse="true": 由teacher操作student -->
<set name="teachers" table="st" cascade="save-update" inverse="true">
<key column="sid"></key>
<many-to-many class="com.test.domain.Teacher" column="tid">
</many-to-many>
</set>
</class>
</hibernate-mapping>



测试类,Test.java

package com.test.dao;

import org.hibernate.Session;

import com.test.domain.Student;
import com.test.domain.Teacher;

public class Test {

public static void main(String[] args) {
Session session = HibernateSessionFactory.getSession();

Teacher teacher1 = new Teacher();
teacher1.setName("teacher1");

Teacher teacher2 = new Teacher();
teacher2.setName("teacher2");

Teacher teacher3 = new Teacher();
teacher3.setName("teacher3");

Student student1 = new Student();
student1.setName("student1");

Student student2 = new Student();
student2.setName("student2");
//student1.getTeachers().add(teacher1);student1.getTeachers().add(teacher2);student2.getTeachers().add(teacher1);这几句代码可以不要,因为Teacher掌握主动权。所以下面几句代码给注释掉了
// //学生一有2个老师
// student1.getTeachers().add(teacher1);
// student1.getTeachers().add(teacher2);
//
// //学生二有一个老师
// student2.getTeachers().add(teacher1);

//因为老师是inverse="false" ,老师掌握主动权
//第一个老师有2个学生
teacher1.getStudents().add(student1);
teacher1.getStudents().add(student2);

//第二个老师有一个学生
teacher2.getStudents().add(student1);

session.saveOrUpdate(teacher1);
session.saveOrUpdate(teacher2);
session.beginTransaction().commit();
session.close();

/**执行的sql
Hibernate: insert into manytomany.teacher (name) values (?)
Hibernate: insert into manytomany.student (name) values (?)
Hibernate: insert into manytomany.student (name) values (?)
Hibernate: insert into manytomany.teacher (name) values (?)
Hibernate: insert into st (tid, sid) values (?, ?)
Hibernate: insert into st (tid, sid) values (?, ?)
Hibernate: insert into st (tid, sid) values (?, ?)
*/

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值