Это моя примерная схема, и я создал объекты jpa в eclipse. Я использую репозитории весны jpa. Я хочу знать, нужно ли мне создавать интерфейс репозитория для таблицы курса студента.Join Table и Spring Data Repository
У меня есть сомнения в отношении метода addStudentCourse как для классов ученика, так и для курса. Список studentCourses всегда будет нулевым для нового объекта, как я могу заполнить таблицу курса студента при регистрации информации о студенте в системном i.e сохранении метода на studentRepository.
Student.java
@Entity
@NamedQuery(name="Student.findAll", query="SELECT s FROM Student s")
public class Student implements Serializable {
private static final long serialVersionUID = 1L;
@Id
private long studentid;
private String studentname;
//bi-directional many-to-one association to StudentCourse
@OneToMany(mappedBy="student")
private List<StudentCourse> studentCourses;
........
public StudentCourse addStudentCourse(StudentCourse studentCourse) {
getStudentCourses().add(studentCourse);
studentCourse.setStudent(this);
return studentCourse;
}
public StudentCourse removeStudentCourse(StudentCourse studentCourse) {
getStudentCourses().remove(studentCourse);
studentCours.setStudent(null);
return studentCourse;
}
Course.java
@Entity
@NamedQuery(name="Course.findAll", query="SELECT c FROM Course c")
public class Course implements Serializable {
private static final long serialVersionUID = 1L;
@Id
private long courseid;
private String coursename;
//bi-directional many-to-one association to StudentCourse
@OneToMany(mappedBy="course")
private List<StudentCourse> studentCourses;
public StudentCourse addStudentCourse(StudentCourse studentCourse) {
getStudentCourses().add(studentCourse);
studentCourse.setCourse(this);
return studentCourse;
}
public StudentCourse removeStudentCourse(StudentCourse studentCourse) {
getStudentCourses().remove(studentCourse);
studentCourse.setCourse(null);
return studentCourse;
}
StudentCourse.java
@Entity
@Table(name="STUDENT_COURSE")
@NamedQuery(name="StudentCourse.findAll", query="SELECT s FROM StudentCourse s")
public class StudentCourse implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
private StudentCoursePK id;
private String status;
//bi-directional many-to-one association to Course
@ManyToOne
@JoinColumn(name="COURSEID")
private Course course;
//bi-directional many-to-one association to Student
@ManyToOne
@JoinColumn(name="STUDENTID")
private Student student;
...
}
StudentCoursePK.java
@Embeddable
public class StudentCoursePK implements Serializable {
//default serial version id, required for serializable classes.
private static final long serialVersionUID = 1L;
@Column(insertable=false, updatable=false)
private long studentid;
@Column(insertable=false, updatable=false)
private long courseid;
...
}