数据库表结构
Student.java
1 @Entity 2 @Table(name = "student") 3 public class Student { 4 @Id 5 @Column(name = "id") 6 @GenericGenerator(name = "generator",strategy = "native") 7 @GeneratedValue(generator = "generator") 8 private int id; 9 10 @Column(name = "name") 11 private String name; 12 13 14 @ManyToMany(cascade = CascadeType.ALL,fetch = FetchType.LAZY) 15 @JoinTable(name = "student_course", 16 joinColumns= {@JoinColumn(name = "student_id")}, 17 inverseJoinColumns = {@JoinColumn(name = "course_id")}) 18 private Set<Course> courses = new HashSet<Course>(); 19 20 }
Course.java
@Entity @Table(name = "course") public class Course { @Id @Column(name = "id") @GenericGenerator(name = "generator",strategy = "native") @GeneratedValue(generator = "generator") private int id; @Column(name = "coursename") private String courseName; @ManyToMany(cascade = CascadeType.ALL,fetch = FetchType.LAZY,mappedBy = "courses") private Set<Student> students = new HashSet<Student>(); }