The following designs are available:
There are table structures such as student tables, curriculums, and score tables.
We can design, a student corresponding to many courses, a course corresponding to multiple students, they are many-to-many relationship. We can set up an intermediate table to correlate them, and there happens to be a score table to help me with the 2 table's intermediate table relationship.
Let's analyze the correspondence between the scores and the students and the course. A specific score is a score under a student, a course, and is identified by a unique student ID and a unique course ID.
So, we can set the Federated primary key (STUDENT_ID and course_id) on the score table, but the Federated primary key has its unnecessary complexity, and we can do it in the following ways to facilitate his design.
We design a primary key ID in the score table and design a foreign key student_id and course_id. The diagram is as follows
To build a table statement:
CREATE TABLE ' student ' ( ' id ' int (one) not null, ' name ' varchar () default NULL, PRIMARY KEY (' id ')) EN Gine=innodb DEFAULT Charset=utf8 pack_keys=0;
CREATE TABLE ' Course ' ( ' id ' int (one) not null, ' name ' varchar () default NULL, PRIMARY KEY (' id ')) engin E=innodb DEFAULT Charset=utf8 pack_keys=0;
CREATE TABLE ' score ' ( ' id ' int (one) not null, ' student_id ' int (one) default NULL, ' course_id ' int (one) default N ULL, ' score ' int (3) default NULL, key ' student_id ' (' student_id '), key ' course_id ' (' course_id '), CONSTRAINT ' Score_fk2 ' FOREIGN key (' course_id ') REFERENCES ' course ' (' id '), CONSTRAINT ' score_fk1 ' FOREIGN key (' St udent_id ') REFERENCES ' student ' (' ID ')) engine=innodb DEFAULT Charset=utf8 pack_keys=0;
The corresponding class file statements are as follows:
Import Java.util.hashset;import java.util.set;import Javax.persistence.cascadetype;import javax.persistence.Column ; Import Javax.persistence.entity;import Javax.persistence.fetchtype;import Javax.persistence.id;import Javax.persistence.onetomany;import javax.persistence.table;/** * Student entity. @author myeclipse Persistence Tools */@Entity @table (name = "Student", Catalog = "Hibernate") public class student implement s java.io.Serializable {//fields/** * */private static final long serialversionuid = 1l;private Integer id;private strin G name;private set<score> scores = new hashset<score> (0);//constructors/** default constructor */public Stud ENT () {}/** minimal constructor */public Student (Integer id) {this.id = ID;} /** Full constructor */public Student (Integer ID, String name, set<score> scores) {this.id = Id;this.name = Name;thi S.scores = scores;} Property Accessors@id@column (name = "Id", unique = true, Nullable = False) the public Integer getId () {return this.id;} PubLIC void SetId (Integer id) {this.id = ID;} @Column (name = "Name", length =) public String getName () {return this.name;} public void SetName (String name) {this.name = name;} @OneToMany (cascade = cascadetype.all, fetch = fetchtype.lazy, Mappedby = "student") public set<score> Getscores () {R Eturn This.scores;} public void Setscores (set<score> scores) {this.scores = scores;}}
Import Java.util.hashset;import java.util.set;import Javax.persistence.cascadetype;import javax.persistence.Column ; Import Javax.persistence.entity;import Javax.persistence.fetchtype;import Javax.persistence.id;import Javax.persistence.onetomany;import javax.persistence.table;/** * Course entity. @author myeclipse Persistence Tools */@Entity @table (name = "Course", Catalog = "Hibernate") public class course implements java.io.Serializable {//fields/** * */private static final long serialversionuid = 1l;private Integer id;private String Name;private set<score> scores = new hashset<score> (0);//constructors/** default constructor */public Course () {}/** minimal constructor */public Course (Integer id) {this.id = ID;} /** Full constructor */public Course (Integer ID, String name, set<score> scores) {this.id = Id;this.name = Name;this . scores = scores;} Property Accessors@id@column (name = "Id", unique = true, Nullable = False) the public Integer getId () {return this.id;} Public VOID setId (Integer id) {this.id = ID;} @Column (name = "Name", length =) public String getName () {return this.name;} public void SetName (String name) {this.name = name;} @OneToMany (cascade = cascadetype.all, fetch = fetchtype.lazy, Mappedby = "course") public set<score> Getscores () {RE Turn this.scores;} public void Setscores (set<score> scores) {this.scores = scores;}}
Import Javax.persistence.attributeoverride;import Javax.persistence.attributeoverrides;import Javax.persistence.column;import Javax.persistence.embeddedid;import Javax.persistence.entity;import Javax.persistence.fetchtype;import Javax.persistence.joincolumn;import Javax.persistence.manytoone;import javax.persistence.table;/** * Score entity. @author myeclipse Persistence Tools */@Entity @table (name = "Score", Catalog = "Hibernate") public class score implements JA va.io.Serializable {//fields/** * */private static final long serialversionuid = 1l;private Scoreid id;private Course Co Urse;private Student student;//constructors/** default constructor */public score () {}/** minimal constructor */public Sc Ore (Scoreid ID) {this.id = ID;} /** Full constructor */public score (Scoreid ID, Course Course, Student Student) {this.id = Id;this.course = Course;this.st Udent = student;} Property Accessors@embeddedid@attributeoverrides ({@AttributeOverride (name = "id", column = @Column (name = "id", NULLable = false), @AttributeOverride (name = "StudentID", column = @Column (name = "student_id")), @AttributeOverride (name = " CourseID ", column = @Column (name =" course_id "), @AttributeOverride (name =" Score ", column = @Column (name =" Score "))}) p Ublic Scoreid getId () {return this.id;} public void SetId (Scoreid id) {this.id = ID;} @ManyToOne (fetch = fetchtype.lazy) @JoinColumn (name = "course_id", insertable = false, updatable = false) Public course GetC Ourse () {return this.course;} public void Setcourse (Course Course) {this.course = Course;} @ManyToOne (fetch = fetchtype.lazy) @JoinColumn (name = "student_id", insertable = false, updatable = false) public student GE Tstudent () {return this.student;} public void Setstudent (Student Student) {this.student = Student;}}
A design pattern of alternate federated primary Key in Hibernate