首页 > 代码库 > 使用annotation配置hibernate(3):多对多关系配置
使用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>(); }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。