a) 使用联合主键@Embedded
i. 实现 Serializable 接口
b) 不使用联合主键
i. 并不存在唯一的一种映射关系
表之间的关系:
类之间的关系:
Student 和 Course 是多对多的单向关联,根据需求,一般都是学生包括课程,课程不包含学生(根据需求确定);
Score 中组合了Student和Course类。类图如下:
Course:
1 package com.bjsxt.hibernate; 2 3 import javax.persistence.Entity; 4 import javax.persistence.GeneratedValue; 5 import javax.persistence.Id; 6 import javax.persistence.Table; 7 8 @Entity 9 @Table(name="course") 10 public class Course { 11 private Integer id; 12 13 private String name; 14 15 @Id 16 @GeneratedValue 17 public Integer getId() { 18 return id; 19 } 20 21 public void setId(Integer id) { 22 this.id = id; 23 } 24 25 public String getName() { 26 return name; 27 } 28 29 public void setName(String name) { 30 this.name = name; 31 } 32 }
Student:
1 package com.bjsxt.hibernate; 2 3 import java.util.HashSet; 4 import java.util.Set; 5 6 import javax.persistence.Entity; 7 import javax.persistence.GeneratedValue; 8 import javax.persistence.Id; 9 import javax.persistence.JoinColumn; 10 import javax.persistence.JoinTable; 11 import javax.persistence.ManyToMany; 12 import javax.persistence.Table; 13 14 @Entity 15 @Table(name="student") 16 public class Student { 17 18 private Integer id; 19 20 private String name; 21 22 private Set<Course> courses = new HashSet<Course>(); 23 24 @Id 25 @GeneratedValue 26 public Integer getId() { 27 return id; 28 } 29 30 public void setId(Integer id) { 31 this.id = id; 32 } 33 34 public String getName() { 35 return name; 36 } 37 38 public void setName(String name) { 39 this.name = name; 40 } 41 42 @ManyToMany 43 @JoinTable(name="score", 44 joinColumns=@JoinColumn(name="student_id"), 45 inverseJoinColumns=@JoinColumn(name="course_id") 46 ) 47 public Set<Course> getCourses() { 48 return courses; 49 } 50 51 public void setCourses(Set<Course> courses) { 52 this.courses = courses; 53 } 54 }
Score:
1 package com.bjsxt.hibernate; 2 3 import javax.persistence.Entity; 4 import javax.persistence.GeneratedValue; 5 import javax.persistence.Id; 6 import javax.persistence.JoinColumn; 7 import javax.persistence.ManyToOne; 8 import javax.persistence.Table; 9 10 @Entity 11 @Table(name="score") 12 public class Score { 13 14 private Integer id; 15 16 private Integer score; 17 18 private Student student; 19 20 private Course course; 21 22 @Id 23 @GeneratedValue 24 public Integer getId() { 25 return id; 26 } 27 28 public void setId(Integer id) { 29 this.id = id; 30 } 31 32 public Integer getScore() { 33 return score; 34 } 35 36 public void setScore(Integer score) { 37 this.score = score; 38 } 39 40 @ManyToOne 41 @JoinColumn(name="student_id")//列名 42 public Student getStudent() { 43 return student; 44 } 45 46 public void setStudent(Student student) { 47 this.student = student; 48 } 49 50 @ManyToOne 51 @JoinColumn(name="course_id")//列名 52 public Course getCourse() { 53 return course; 54 } 55 56 public void setCourse(Course course) { 57 this.course = course; 58 } 59 }
添加记录:
1 @Test 2 public void testSave(){ 3 Student s = new Student(); 4 s.setName("zhangsan"); 5 Course c = new Course(); 6 c.setName("java"); 7 Score score = new Score(); 8 score.setCourse(c); 9 score.setStudent(s); 10 11 Session session = sf.getCurrentSession(); 12 session.beginTransaction(); 13 14 session.save(s); 15 session.save(c); 16 session.save(score); 17 18 session.getTransaction().commit(); 19 }
查询:
1 @Test 2 public void testLoad(){ 3 // testSave(); 4 5 Session session = sf.getCurrentSession(); 6 session.beginTransaction(); 7 8 Student s = (Student) session.load(Student.class, 2); 9 for(Course c : s.getCourses()){ 10 System.out.println(c.getName()); 11 } 12 13 session.getTransaction().commit(); 14 }
jar包链接: https://pan.baidu.com/s/1dF26ybf 密码: 4mrf
代码链接: https://pan.baidu.com/s/1dFmrwnZ 密码: ynmp