Hibernate注解版设计学生、课程、成绩的关系映射
1.实体类Student
package com.hong.pojo;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name="z_student")
public class Student {
@ManyToMany
@JoinTable(name="z_score",joinColumns={@JoinColumn(name="sid")},inverseJoinColumns={@JoinColumn(name="cid")})
public Set<Course> getCourse() {
return course;
}
public void setCourse(Set<Course> course) {
this.course = course;
}
@OneToMany(mappedBy="student")
public Set<Score> getScores() {
return scores;
}
public void setScores(Set<Score> scores) {
this.scores = scores;
}
@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;
}
private int id;
private String name;
private Set<Score> scores = new HashSet<Score>();
private Set<Course> course = new HashSet<Course>();
}
2.实体类Course
package com.hong.pojo;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name="z_course")
public class Course {
@ManyToMany(mappedBy="course")
public Set<Student> getStudent() {
return student;
}
public void setStudent(Set<Student> student) {
this.student = student;
}
@OneToMany(mappedBy="course")
public Set<Score> getScores() {
return scores;
}
public void setScores(Set<Score> scores) {
this.scores = scores;
}
@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;
}
private int id;
private String name;
private Set<Student> student = new HashSet<Student>();
private Set<Score> scores = new HashSet<Score>();
}
3.实体类Score
package com.hong.pojo;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name="z_score")
public class Score {
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getScores() {
return scores;
}
public void setScores(int scores) {
this.scores = scores;
}
@ManyToOne
@JoinColumn(name="sid")
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
@ManyToOne
@JoinColumn(name="cid")
public Course getCourse() {
return course;
}
public void setCourse(Course course) {
this.course = course;
}
private int id;
private int scores;
private Student student;
private Course course;
}
注意:
学生与成绩是多对多的关系
成绩与学生是一对多的关系
成绩与课程是一对多的关系
创建出的z_score表就是生成的中间表。
字段有id,score,sid,cid.
需要手动将原来的primary key(sid,cid),改为primary key(id)