欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

【hibernate框架】多对多双向关联(XML实现)

程序员文章站 2022-04-24 14:19:02
...
多对多的双向关联:(非常少用)
通过老师可以知道他教了多少学生,这是单项多对多。而如果同时通过学生知道有多少个老师教他,那么就是多对多双向的关联。


XML实现:
Student.java:
package cn.edu.hpu.many2many;


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


public class Student {
	private int id;
	private String name;	
	private Set<Teacher> teachers=new HashSet<Teacher>();
	
	public Set<Teacher> getTeachers() {
		return teachers;
	}
	public void setTeachers(Set<Teacher> teachers) {
		this.teachers = teachers;
	}


	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;
	}
	
}


配置文件:
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> 
        <set name="teachers" table="t_s">
           <key column="student_id"></key><!-- 指定关联字段的名字叫什么 -->
           <many-to-many class="cn.edu.hpu.many2many.Teacher" column="teacher_id"/>
        </set>
    </class>
</hibernate-mapping>

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>

在hibernate.cfg.xml中配置:
<mapping resource="cn/edu/hpu/many2many/Student.hbm.xml"/> 
<mapping resource="cn/edu/hpu/many2many/Teacher.hbm.xml"/>

生成的sql语句:
 create table t_s (
        student_id integer not null,
        teacher_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)


转载请注明出处:http://blog.csdn.net/acmman