实体关系之@ManyToMany

  1. packagecom.entity;
  2. importjava.io.Serializable;
  3. importjava.util.HashSet;
  4. importjava.util.Set;
  5. importjavax.persistence.Column;
  6. importjavax.persistence.Entity;
  7. importjavax.persistence.GeneratedValue;
  8. importjavax.persistence.Id;
  9. importjavax.persistence.ManyToMany;
  10. importjavax.persistence.Table;
  11. @Entity
  12. @Table(name="Student")
  13. publicclassStudentimplementsSerializable{
  14. privateIntegerstudentid;//学生ID
  15. privateStringstudentName;//学生姓名
  16. privateSet<Teacher>teachers=newHashSet<Teacher>();//对应的教师集合
  17. publicStudent(){
  18. }
  19. publicStudent(StringstudentName){
  20. this.studentName=studentName;
  21. }
  22. @Id
  23. @GeneratedValue
  24. publicIntegergetStudentid(){
  25. returnstudentid;
  26. }
  27. publicvoidsetStudentid(Integerstudentid){
  28. this.studentid=studentid;
  29. }
  30. @Column(nullable=false,length=32)
  31. publicStringgetStudentName(){
  32. returnstudentName;
  33. }
  34. publicvoidsetStudentName(StringstudentName){
  35. this.studentName=studentName;
  36. }
  37. /*
  38. *@ManyToMany注释表示Student是多对多关系的一边,mappedBy属性定义了Student为双向关系的维护端
  39. */
  40. @ManyToMany(mappedBy="students")
  41. publicSet<Teacher>getTeachers(){
  42. returnteachers;
  43. }
  44. publicvoidsetTeachers(Set<Teacher>teachers){
  45. this.teachers=teachers;
  46. }
  47. }



Teacher.java

Java代码 收藏代码
  1. packagecom.entity;
  2. importjava.io.Serializable;
  3. importjava.util.HashSet;
  4. importjava.util.Set;
  5. importjavax.persistence.CascadeType;
  6. importjavax.persistence.Column;
  7. importjavax.persistence.Entity;
  8. importjavax.persistence.FetchType;
  9. importjavax.persistence.GeneratedValue;
  10. importjavax.persistence.Id;
  11. importjavax.persistence.JoinTable;
  12. importjavax.persistence.ManyToMany;
  13. importjavax.persistence.Table;
  14. importjavax.persistence.JoinColumn;
  15. @Entity
  16. @Table
  17. publicclassTeacherimplementsSerializable{
  18. privateIntegerteacherid;//教师ID
  19. privateStringteacherName;//教师姓名
  20. privateSet<Student>students=newHashSet<Student>();//对应的学生集合
  21. publicTeacher(){
  22. }
  23. publicTeacher(StringteacherName){
  24. this.teacherName=teacherName;
  25. }
  26. @Id
  27. @GeneratedValue
  28. publicIntegergetTeacherid(){
  29. returnteacherid;
  30. }
  31. publicvoidsetTeacherid(Integerteacherid){
  32. this.teacherid=teacherid;
  33. }
  34. @Column(nullable=false,length=32)
  35. publicStringgetTeacherName(){
  36. returnteacherName;
  37. }
  38. publicvoidsetTeacherName(StringteacherName){
  39. this.teacherName=teacherName;
  40. }
  41. /*
  42. *@ManyToMany注释表示Teacher是多对多关系的一端。
  43. *@JoinTable描述了多对多关系的数据表关系,name属性指定中间表名称。
  44. *joinColumns定义中间表与Teacher表的外键关系,中间表Teacher_Student的Teacher_ID列是Teacher表的主键列对应的外键列。
  45. *inverseJoinColumns属性定义了中间表与另外一端(Student)的外键关系。
  46. */
  47. @ManyToMany(cascade=CascadeType.PERSIST,fetch=FetchType.LAZY)
  48. @JoinTable(name="Teacher_Student",
  49. joinColumns={@JoinColumn(name="teacher_ID",referencedColumnName="teacherid")},
  50. inverseJoinColumns={@JoinColumn(name="student_ID",referencedColumnName="studentid")
  51. })
  52. publicSet<Student>getStudents(){
  53. returnstudents;
  54. }
  55. publicvoidsetStudents(Set<Student>students){
  56. this.students=students;
  57. }
  58. publicvoidaddStudent(Studentstudent){
  59. if(!this.students.contains(student)){//检测在该散列表中某些键是否映射到指定值,value查找的值。如果某些键映射到该散列表中的值为true,否则false
  60. this.students.add(student);
  61. }
  62. }
  63. publicvoidremoveStudent(Studentstudent){
  64. this.students.remove(student);
  65. }
  66. }
转自:http://lym6520.iteye.com/blog/314065

猜你喜欢

转载自lcpstyle.iteye.com/blog/2244748