使用annotation配置hibernate(3):多对多关系配置
数据库表结构
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>(); }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。