- packagecom.entity;
- importjava.io.Serializable;
- importjava.util.HashSet;
- importjava.util.Set;
- importjavax.persistence.Column;
- importjavax.persistence.Entity;
- importjavax.persistence.GeneratedValue;
- importjavax.persistence.Id;
- importjavax.persistence.ManyToMany;
- importjavax.persistence.Table;
- @Entity
- @Table(name="Student")
- publicclassStudentimplementsSerializable{
- privateIntegerstudentid;//学生ID
- privateStringstudentName;//学生姓名
- privateSet<Teacher>teachers=newHashSet<Teacher>();//对应的教师集合
- publicStudent(){
- }
- publicStudent(StringstudentName){
- this.studentName=studentName;
- }
- @Id
- @GeneratedValue
- publicIntegergetStudentid(){
- returnstudentid;
- }
- publicvoidsetStudentid(Integerstudentid){
- this.studentid=studentid;
- }
- @Column(nullable=false,length=32)
- publicStringgetStudentName(){
- returnstudentName;
- }
- publicvoidsetStudentName(StringstudentName){
- this.studentName=studentName;
- }
- /*
- *@ManyToMany注释表示Student是多对多关系的一边,mappedBy属性定义了Student为双向关系的维护端
- */
- @ManyToMany(mappedBy="students")
- publicSet<Teacher>getTeachers(){
- returnteachers;
- }
- publicvoidsetTeachers(Set<Teacher>teachers){
- this.teachers=teachers;
- }
- }
Teacher.java
- packagecom.entity;
- importjava.io.Serializable;
- importjava.util.HashSet;
- importjava.util.Set;
- importjavax.persistence.CascadeType;
- importjavax.persistence.Column;
- importjavax.persistence.Entity;
- importjavax.persistence.FetchType;
- importjavax.persistence.GeneratedValue;
- importjavax.persistence.Id;
- importjavax.persistence.JoinTable;
- importjavax.persistence.ManyToMany;
- importjavax.persistence.Table;
- importjavax.persistence.JoinColumn;
- @Entity
- @Table
- publicclassTeacherimplementsSerializable{
- privateIntegerteacherid;//教师ID
- privateStringteacherName;//教师姓名
- privateSet<Student>students=newHashSet<Student>();//对应的学生集合
- publicTeacher(){
- }
- publicTeacher(StringteacherName){
- this.teacherName=teacherName;
- }
- @Id
- @GeneratedValue
- publicIntegergetTeacherid(){
- returnteacherid;
- }
- publicvoidsetTeacherid(Integerteacherid){
- this.teacherid=teacherid;
- }
- @Column(nullable=false,length=32)
- publicStringgetTeacherName(){
- returnteacherName;
- }
- publicvoidsetTeacherName(StringteacherName){
- this.teacherName=teacherName;
- }
- /*
- *@ManyToMany注释表示Teacher是多对多关系的一端。
- *@JoinTable描述了多对多关系的数据表关系,name属性指定中间表名称。
- *joinColumns定义中间表与Teacher表的外键关系,中间表Teacher_Student的Teacher_ID列是Teacher表的主键列对应的外键列。
- *inverseJoinColumns属性定义了中间表与另外一端(Student)的外键关系。
- */
- @ManyToMany(cascade=CascadeType.PERSIST,fetch=FetchType.LAZY)
- @JoinTable(name="Teacher_Student",
- joinColumns={@JoinColumn(name="teacher_ID",referencedColumnName="teacherid")},
- inverseJoinColumns={@JoinColumn(name="student_ID",referencedColumnName="studentid")
- })
- publicSet<Student>getStudents(){
- returnstudents;
- }
- publicvoidsetStudents(Set<Student>students){
- this.students=students;
- }
- publicvoidaddStudent(Studentstudent){
- if(!this.students.contains(student)){//检测在该散列表中某些键是否映射到指定值,value查找的值。如果某些键映射到该散列表中的值为true,否则false
- this.students.add(student);
- }
- }
- publicvoidremoveStudent(Studentstudent){
- this.students.remove(student);
- }
- }
转自:http://lym6520.iteye.com/blog/314065