Jpa 联接表和Spring数据存储库

Jpa 联接表和Spring数据存储库,jpa,one-to-many,spring-data-jpa,jointable,Jpa,One To Many,Spring Data Jpa,Jointable,这是我的示例模式,我在eclipse中生成了jpa实体。 我正在使用SpringJPA存储库。我想知道是否需要为学生课程表创建存储库接口 我对学生和课程实体类的addStudentCourse方法都有疑问。对于新实体,List studentCourses将始终为空,在系统中注册学生信息时,如何填写学生课程表,即studentRepository上的保存方法 Student.java @Entity @NamedQuery(name="Student.findAll", query="SELE

这是我的示例模式,我在eclipse中生成了jpa实体。 我正在使用SpringJPA存储库。我想知道是否需要为学生课程表创建存储库接口

我对学生和课程实体类的addStudentCourse方法都有疑问。对于新实体,List studentCourses将始终为空,在系统中注册学生信息时,如何填写学生课程表,即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;
    }
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;

    ...
}

如果我正确理解了您的问题,您要做的是能够从StudentRepository中的save方法保存一个学生,这将插入/更新该学生,并插入/更新联接表

由于学生实体不是拥有方(由StudentCourse中的“Student”映射),因此保存学生不会触发对StudentCourse的保存。为此,您可以在列表中添加一个级联属性,用于插入、更新。。。或者只是为了一切:

@OneToMany(mappedBy="student", cascade = CascadeType.ALL)
private List<StudentCourse> studentCourses = new ArrayList<StudentCourse>();
这也将填充StudentCourse表

因此,不需要存储库,尽管如果级联不能按预期工作,您可以创建一个存储库并手动保存StudentCourse实体

如果这不起作用,您可以尝试更改映射。对于n元关系或具有额外列的联接表,我始终在
@embeddeble
类中定义
@manytone
关系,并且在表示联接表的实体中,我将getter定义为
@Transient
,以允许访问嵌入复合Id中的映射对象

您可以看到一个示例和一篇关于此方法的博客文章

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

    ...
}
@OneToMany(mappedBy="student", cascade = CascadeType.ALL)
private List<StudentCourse> studentCourses = new ArrayList<StudentCourse>();
@Transactional
public void enrollInCourse(Student student, Course course) {
    StudentCourse sc = new StudentCourse();
    sc.setStudent(student);
    sc.setCourse(course);
    sc.setStatus("Enrolled");
    student.getStudentCourses().add(sc);

    studentRepository.save(student);
}