package com.xj.model;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.*;
import org.hibernate.annotations.BatchSize;
@Entity
@BatchSize(size=5)
public class Student {
private int id;
private String name;
private Set<Course> courses=new HashSet<Course>();
@ManyToMany
@JoinTable(
name="score",
joinColumns=@JoinColumn(name="student_id", referencedColumnName="id"),
inverseJoinColumns=@JoinColumn(name="course_id", referencedColumnName="id")
)
public Set<Course> getCourses() {
return courses;
}
public void setCourses(Set<Course> courses) {
this.courses = courses;
}
@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;
}
}
package com.xj.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import org.hibernate.annotations.BatchSize;
@Entity
@BatchSize(size=5)
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;
}
}
package com.xj.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import org.hibernate.annotations.BatchSize;
@Entity
@BatchSize(size=5)
@Table(name="score")
public class Score {
private int id;
private int s;
private Course c;
private Student t;
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@ManyToOne
@JoinColumn(name="course_id")
public Course getC() {
return c;
}
public void setC(Course c) {
this.c = c;
}
@ManyToOne
@JoinColumn(name="student_id")
public Student getT() {
return t;
}
public void setT(Student t) {
this.t = t;
}
public int getS() {
return s;
}
public void setS(int s) {
this.s = s;
}
}
@Test
public void testSaveSCT() {
Student s1=new Student();
s1.setName("xj");
Student s2=new Student();
s2.setName("hs");
Course c1=new Course();
c1.setName("java");
Course c2=new Course();
c2.setName("english");
/*s1.getCourses().add(c1);
s1.getCourses().add(c2);
s2.getCourses().add(c1);
s2.getCourses().add(c2);*/
Score sc1=new Score();
sc1.setT(s1);
sc1.setC(c1);
sc1.setS(90);
Score sc2=new Score();
sc2.setT(s1);
sc2.setC(c2);
sc2.setS(95);
Score sc3=new Score();
sc3.setT(s2);
sc3.setC(c1);
sc3.setS(70);
Score sc4=new Score();
sc4.setT(s2);
sc4.setC(c2);
sc4.setS(60);
Session session = sf.getCurrentSession();
session.beginTransaction();
session.save(s1);
session.save(s2);
session.save(c1);
session.save(c2);
session.save(sc1);
session.save(sc2);