Hibernate Student_Course_Score设计
示例:
设计代码,实现在数据库中建student表、course表、和score表,展现三者关系
student表:id、name
course表:id、name
score表:id、score、student_id、course_id
三张表的关联关系如下:
设计思路:
1.首先创建Student实体类和Course实体类
会自动创建中间表,通过@JoinTable注解,设置中间表名为“score”,属性名分别为“student_id”和“course_id”
@ManyToMany @JoinTable(name="score", joinColumns={@JoinColumn(name="student_id")}, inverseJoinColumns={@JoinColumn(name="course_id")})
2.创建实体类Score
private int id;
private int score;
private Student student;
private Course course;
根据步骤1中创建的中间表,通过@Table注解设置表名为“score”,
通过@ManyToOne注解设置属性名分别为“student_id”和“course_id”
3.Junit测试类
@Test public void test() { new SchemaExport(new Configuration().configure()).create(true, true); }
具体实现代码如下:
1.Student类
@Entity public class Student { private int id; private String name; private Set<Course> courses = new HashSet<Course>(); @Id @GeneratedValue public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @ManyToMany @JoinTable(name="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; } }
2.Course类
@Entity public class Course { private int id; private String name; @Id @GeneratedValue public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
3.Score类
@Entity @Table(name="score") //对应中间表名 public class Score { private int id; private int score; private Student student; private Course course; @Id @GeneratedValue public int getId() { return id; } public void setId(int id) { this.id = id; } public int getScore() { return score; } public void setScore(int score) { this.score = score; } @ManyToOne @JoinColumn(name="student_id") //对应属性名 public Student getStudent() { return student; } public void setStudent(Student student) { this.student = student; } @ManyToOne() @JoinColumn(name="course_id") //对应属性名 public Course getCourse() { return course; } public void setCourse(Course course) { this.course = course; } }
4.存储一条数据
@Test public void testsave() { Session session = sf.getCurrentSession(); session.beginTransaction(); Student s = new Student(); s.setName("lisi"); Course c = new Course(); c.setName("c++"); Score score = new Score(); score.setStudent(s); score.setCourse(c); // score.setScore(90); //若不设置,默认为0 session.save(s); session.save(c); session.save(score); session.getTransaction().commit(); }
注意:
create table score (id integer not null, score integer not null, course_id integer, student_id integer not null auto_increment, primary key (student_id, course_id)) 运行程序,会发现通过程序自动建的score表是有问题的。 我们想要的是,id为主键,且自增 而结果是,student_id和course_id为联合主键,且student_id自增 这是hibernate自身的bug,所以应该手动去数据库中,修改表的结构
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。