Hibernate Annotation 多对多 双向 示例

来源:互联网 发布:神话崔文子 知乎 编辑:程序博客网 时间:2024/05/16 04:40

学生和老师的关系

学生同时有多个老师,老师同时也有多个学生

 

Student.java  学生实体类

package com.model;//包名import java.util.HashSet;import java.util.Set;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;import javax.persistence.ManyToMany;@Entitypublic class Student {private int id;private String name;private Set<Teacher> teachers = new HashSet<Teacher>();//持有对方的存放(关系)对象。类型必须是“Set”接口类型@Transient//数据库中没有的字段,需要在这个字段中加上@Transient注释@ManyToMany(mappedBy="students")// 映射对应类的关系持有对象(Set对象名),双向关系必须设置 “mappedBy”public Set<Teacher> getTeachers() {return teachers;}public void setTeahcers(Set<Teacher> teachers) {this.teachers = teachers;}@Id@GeneratedValuepublic 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 com.model;//包名import java.util.HashSet;import java.util.Set;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;import javax.persistence.*;@Entitypublic class Teacher {private int id;private String name;private Set<Student> students = new HashSet<Student>();//持有对方的存放(关系)对象。类型必须是“Set”接口类型@Id@GeneratedValuepublic 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//两个实体类都为@ManyToMany@JoinTable(//设置中间表name="t_s",//中间表的表名joinColumns={@JoinColumn(name="teacher_id")},/*修改中间表中参照着本实体的外键的字段名  */inverseJoinColumns={@JoinColumn(name="student_id")} /*修改参照本实体对应的实体的外键的字段名  */)public Set<Student> getStudents() {return students;}public void setStudents(Set<Student> students) {this.students = students;}}


=============================================================================

JUnit 测试类

import org.hibernate.SessionFactory;import org.hibernate.cfg.AnnotationConfiguration;import org.hibernate.tool.hbm2ddl.SchemaExport;import org.junit.Test;public class T_STest {@Testpublic void testSchemaExport() {new SchemaExport(new AnnotationConfiguration().configure()).create(false,true);}}

==========================================================================

生成SQL语句

create table Student (        id integer not null auto_increment,        name varchar(255),        primary key (id)    )     create table 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 FK1BF6843F43EE8 (teacher_id),         add constraint FK1BF6843F43EE8         foreign key (teacher_id)         references Teacher (id)     alter table t_s         add index FK1BF683358F448 (student_id),         add constraint FK1BF683358F448         foreign key (student_id)         references Student (id)


 

 

 

 

原创粉丝点击