hibernate之关于关联映射的综合应用
@Entity @Table(name="c_course") public class Course { private Integer id; private String coursename; private Set<Student> students=new HashSet<Student>(); private Set<Score> scores=new HashSet<Score>(); @OneToMany(mappedBy="course")//必须指定关系由多的一方维护 public Set<Score> getScores() { return scores; } public void setScores(Set<Score> scores) { this.scores = scores; } @ManyToMany//指定中间表是s_score @JoinTable(name="s_score",joinColumns={@JoinColumn(name="course_id")}, inverseJoinColumns={@JoinColumn(name="student_id")}) public Set<Student> getStudents() { return students; } public void setStudents(Set<Student> students) { this.students = students; } @Id @GeneratedValue public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } @Column(name="c_coursename") public String getCoursename() { return coursename; } public void setCoursename(String coursename) { this.coursename = coursename; } }(课程表Course)
@Entity @Table(name="t_student") public class Student { private Integer id; private String name; private Set<Course> courses=new HashSet<Course>(); private Set<Score> scores=new HashSet<Score>(); @OneToMany(mappedBy="student") public Set<Score> getScores() { return scores; } public void setScores(Set<Score> scores) { this.scores = scores; } @ManyToMany @JoinTable(name="s_score",joinColumns={@JoinColumn(name="student_id")}, inverseJoinColumns={@JoinColumn(name="course_id")}) public Set<Course> getCourses() { return courses; } public void setCourses(Set<Course> courses) { this.courses = courses; } @Id @GeneratedValue public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } @Column(name="s_name") public String getName() { return name; } public void setName(String name) { this.name = name; } }(Student学生表)
@Entity @Table(name="s_score") public class Score { private Integer id; private String score; private Student student; private Course course; @ManyToOne(cascade={CascadeType.ALL})//配置级联操作 @JoinColumn(name="student_id") public Student getStudent() { return student; } public void setStudent(Student student) { this.student = student; } @ManyToOne(cascade={CascadeType.ALL}) @JoinColumn(name="course_id") public Course getCourse() { return course; } public void setCourse(Course course) { this.course = course; } @Id @GeneratedValue public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } @Column(name="s_score") public String getScore() { return score; } public void setScore(String score) { this.score = score; } }(分数表,也是中间关联表,Score)
12:11:08,169 DEBUG SchemaExport:415 - create table c_course ( id integer not null auto_increment, c_coursename varchar(255), primary key (id) ) 12:11:08,333 DEBUG SchemaExport:415 - create table s_score ( id integer not null, s_score varchar(255), course_id integer, student_id integer not null auto_increment, primary key (student_id, course_id) ) 12:11:08,455 DEBUG SchemaExport:415 - create table t_student ( id integer not null auto_increment, s_name varchar(255), primary key (id) )
@Test public void add(){ try { Configuration cfg=new Configuration(); cfg.configure(); SessionFactory sessionFactory=cfg.buildSessionFactory(); Session session=sessionFactory.openSession(); session.beginTransaction(); Student student1=new Student(); student1.setName("张三1"); // Student student2=new Student(); // student2.setName("李四"); Course c1=new Course(); c1.setCoursename("数学1"); Course c2=new Course(); c2.setCoursename("语文1"); // session.save(student1); // session.save(c1); // session.save(student2); // session.save(c2); Score score=new Score(); score.setScore("901"); score.setCourse(c1); score.setStudent(student1); //张三数学90分 session.save(score); Score score2=new Score(); score2.setCourse(c2); score2.setScore("100"); score2.setStudent(student1); session.save(score2); session.getTransaction().commit(); session.close(); sessionFactory.close(); } catch (HibernateException e) { e.printStackTrace(); } }
ok,大家可以看出,为什么我们最后只需要保存score就行呢?没有session.save()student和course,为什么也能直接保存数据库中?这就是
cascade={CascadeType.ALL}
public void findTest(){ Configuration cfg=new Configuration(); cfg.configure(); SessionFactory sessionFactory=cfg.buildSessionFactory(); Session s=sessionFactory.openSession(); s.beginTransaction(); Student student=(Student)s.load(Student.class, 2); System.out.println("学生姓名"+student.getName()); for(Course course:student.getCourses()){ System.out.print("--------"+"课程名称"+course.getCoursename()); for(Score score:course.getScores()){ System.out.println("--------"+"课程分数"+score.getScore()); } } s.getTransaction().commit(); s.close(); sessionFactory.close(); }
public void update(){ Configuration cfg=new Configuration(); cfg.configure(); SessionFactory sessionFactory=cfg.buildSessionFactory(); Session s=sessionFactory.openSession(); s.beginTransaction(); Student student=(Student)s.load(Student.class,1); //我们现在把查询出来语文成绩改为0分 for(Course course:student.getCourses()){ System.out.println(course.getCoursename()); if("语文".equals(course.getCoursename())){ for(Score score:course.getScores()){ if(score.getStudent().getId().equals(student.getId())){ score.setScore("0"); s.update(score); } } } } s.getTransaction().commit(); s.close(); sessionFactory.close(); }
@Test public void deleteObj(){ Configuration cfg=new Configuration(); cfg.configure(); SessionFactory sessionFactory=cfg.buildSessionFactory(); Session s=sessionFactory.openSession(); s.beginTransaction(); Student student=(Student)s.load(Student.class,3); s.delete(student); s.getTransaction().commit(); s.close(); sessionFactory.close(); }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。