如果下面部分内容有不明白的可以查找:
基于Annotation的关系映射 前期准备:
基于xml的多对多:
Student:
@Entity@Table(name = "student", catalog = "hibernate")public class Student implements java.io.Serializable { private Integer id; private String SName; private Setteachers; public Student() { } @Id @GeneratedValue @Column(name = "id", unique = true, nullable = false) public Integer getId() { return this.id; } public void setId(Integer id) { this.id = id; } @Column(name = "s_name", nullable = false, length = 45) public String getSName() { return this.SName; } public void setSName(String SName) { this.SName = SName; } @ManyToMany(cascade=CascadeType.ALL,targetEntity=cn.framelife.hibernate.entity.Teacher.class) @JoinTable(name="s_t",catalog="hibernate", joinColumns={@JoinColumn(name="student_id")}, inverseJoinColumns={@JoinColumn(name="teacher_id")} )/*映射文件方式比较: */ public Set getTeachers() { return teachers; } public void setTeachers(Set teachers) { this.teachers = teachers; }}
Teacher:
@Entity@Table(name = "teacher", catalog = "hibernate")public class Teacher implements java.io.Serializable { private Integer id; private String TName; private Setstudents; public Teacher() { } @Id @GeneratedValue @Column(name = "id", unique = true, nullable = false) public Integer getId() { return this.id; } public void setId(Integer id) { this.id = id; } @Column(name = "t_name", nullable = false, length = 45) public String getTName() { return this.TName; } public void setTName(String TName) { this.TName = TName; } @ManyToMany(cascade=CascadeType.ALL,targetEntity=cn.framelife.hibernate.entity.Student.class) @JoinTable(name="s_t",catalog="hibernate", joinColumns={@JoinColumn(name="teacher_id")}, inverseJoinColumns={@JoinColumn(name="student_id")} ) public Set getStudents() { return students; } public void setStudents(Set students) { this.students = students; }}