hibernate 多对多 配备
hibernate 多对多 配置
1. 创建表Teacher和Student和中间表teacher_student
CREATE TABLE `teacher` ( `tid` int(11) NOT NULL auto_increment, `name` varchar(50) NOT NULL, PRIMARY KEY (`tid`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; CREATE TABLE `student` ( `sid` int(11) NOT NULL auto_increment, `name` varchar(50) NOT NULL, PRIMARY KEY (`sid`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; create table teacher_student( tid int(11) not null, sid int(11) not null )ENGINE=InnoDB DEFAULT CHARSET=utf8;
2. 创建实体类 Teacher 和 Student
public class Student implements java.io.Serializable { // Fields private Integer sid; private String name; private Set<Teacher> teachers; // Constructors public Set<Teacher> getTeachers() { return teachers; } public void setTeachers(Set<Teacher> teachers) { this.teachers = teachers; } /** default constructor */ public Student() { } /** full constructor */ public Student(String name) { this.name = name; } // Property accessors 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 class Teacher implements java.io.Serializable { // Fields private Integer tid; private String name; private Set<Student> stus; // Constructors public Set<Student> getStus() { return stus; } public void setStus(Set<Student> stus) { this.stus = stus; } /** default constructor */ public Teacher() { } /** full constructor */ public Teacher(String name) { this.name = name; } // Property accessors 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; } }
3. 配置Teacher.hbm.xml 和 student.hbm.xml配置文件
Teacher.hbm.xml <hibernate-mapping> <class name="ssh.pojo.Teacher" table="teacher" catalog="zf"> <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="50" not-null="true" /> </property> <set name="stus" table="teacher_student" cascade="save-update" inverse="true"> <key column="tid"></key> <many-to-many column="sid" class="ssh.pojo.Student"></many-to-many> </set> </class> </hibernate-mapping>
Student.hbm.xml <hibernate-mapping> <class name="ssh.pojo.Student" table="student" catalog="zf"> <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="50" not-null="true" /> </property> <set name="teachers" table="teacher_student" cascade="save-update"> <key column="sid"></key> <many-to-many column="tid" class="ssh.pojo.Teacher"></many-to-many> </set> </class> </hibernate-mapping>
4. 测试
Session session = null; // 一级缓存 TblUser user = null; try{ session = HibernateSessionFactory.getSession(); session.beginTransaction(); Set<Teacher> teachers = new HashSet<Teacher>(); Set<Student> students = new HashSet<Student>(); // 初始化老师 Teacher tWang = new Teacher(); tWang.setName("王"); Teacher tZhang = new Teacher(); tZhang.setName("张"); teachers.add(tWang); teachers.add(tZhang); // 初始化学生 Student s1 = new Student(); Student s2 = new Student(); Student s3 = new Student(); s1.setName("刘另一"); s2.setName("任汉卿"); s3.setName("付博文"); students.add(s1); students.add(s2); students.add(s3); // teacher--->stu tWang.setStus(students); tZhang.setStus(students); // stu---->teacher s1.setTeachers(teachers); s2.setTeachers(teachers); s3.setTeachers(teachers); // save to db session.save(tWang); session.save(tZhang); session.save(s1); session.save(s2); session.save(s3); session.getTransaction().commit(); }catch(Exception e){ e.printStackTrace(); session.getTransaction().rollback(); } 结果: Hibernate: insert into zf.teacher (name) values (?) Hibernate: insert into zf.student (name) values (?) Hibernate: insert into zf.teacher (name) values (?) Hibernate: insert into zf.student (name) values (?) Hibernate: insert into zf.student (name) values (?) Hibernate: insert into teacher_student (sid, tid) values (?, ?) Hibernate: insert into teacher_student (sid, tid) values (?, ?) Hibernate: insert into teacher_student (sid, tid) values (?, ?) Hibernate: insert into teacher_student (sid, tid) values (?, ?) Hibernate: insert into teacher_student (sid, tid) values (?, ?) Hibernate: insert into teacher_student (sid, tid) values (?, ?)