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

多对多的双向关联:(非常少用)
通过老师可以知道他教了多少学生,这是单项多对多。而如果同时通过学生知道有多少个老师教他,那么就是多对多双向的关联。


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


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


import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Table;


@Entity
@Table(name="m_student")
public class Student {
	private int id;
	private String name;	
	private Set<Teacher> teachers=new HashSet<Teacher>();
	
	@ManyToMany(mappedBy="students")
	public Set<Teacher> getTeachers() {
		return teachers;
	}
	public void setTeachers(Set<Teacher> teachers) {
		this.teachers = teachers;
	}
	@Id
	@GeneratedValue
	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 javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;


import cn.edu.hpu.many2many.Student;


@Entity
@Table(name="m_teacher")
public class Teacher {
	private int id;
	private String name;
	private Set<Student> students=new HashSet<Student>();
	//选择set的原因是因为,set互相之间不会有重复的
	//跟数据库模型比较匹配
	
	@Id
	@GeneratedValue
	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;
	}
	
	@ManyToMany
	@JoinTable(name="t_s",
		joinColumns={@JoinColumn(name="teacher_id")},//指明的是当前这个实体类的表的名字(加s防止联合主键)	
		inverseJoinColumns={@JoinColumn(name="student_id")}//逆转的、对方的的表名(所对应的另一张表)
	)
	public Set<Student> getStudents() {
		return students;
	}
	public void setStudents(Set<Student> students) {
		this.students = students;
	}
	
}


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

生成的sql语句:
 create table m_student (
        id integer not null auto_increment,
        name varchar(255),
        primary key (id)
    )


    create table m_teacher (
        id integer not null auto_increment,
        name varchar(255),
        primary key (id)
    )


    create table t_s (
        teacher_id integer not null,
        student_id integer not null,
        primary key (teacher_id, student_id)
    )


    alter table t_s 
        add index FK1BF687B5893C2 (teacher_id), 
        add constraint FK1BF687B5893C2 
        foreign key (teacher_id) 
        references m_teacher (id)


    alter table t_s 
        add index FK1BF686ABD4922 (student_id), 
        add constraint FK1BF686ABD4922 
        foreign key (student_id) 
        references m_student (id)


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

你可能感兴趣的:(Hibernate,框架,ssh,多对多双向关联,Annotation实现)