Hibernate註解版設計學生、課程、成績的關係映射

1.實體類Studentjava

package com.hong.pojo;app

import java.util.HashSet;
import java.util.Set;this


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;ci

@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>();get

}it

 

 

 

2.實體類Courseio

package com.hong.pojo;class

 

import java.util.HashSet;
import java.util.Set;import

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;map

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)

相關文章
相關標籤/搜索