加入表和Spring Data Repository

时间:2015-11-07 10:00:07

标签: jpa one-to-many spring-data-jpa jointable

这是我的示例架构,我在eclipse中生成了jpa实体。 我正在使用spring jpa存储库。我想知道是否需要为学生课程表创建存储库界面。

我怀疑学生和课程实体课程的addStudentCourse方法。列表studentCourses对于新实体将始终为null,如何在系统中注册学生信息时填写学生课程表,即在studentRepository上保存方法。

enter image description here

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;

    ...
}

1 个答案:

答案 0 :(得分:0)

如果我正确理解你的问题你想要做的是能够从StudentRepository中的save方法保存学生,并且这会插入/更新学生并插入/更新连接表。

由于Student实体不是拥有方(由StudentCourse中的“student”映射),因此保存学生不会触发StudentCourse上的保存。为此,您可以为插入,更新...或仅为所有内容添加列表级联属性:

@OneToMany(mappedBy="student", cascade = CascadeType.ALL)
private List<StudentCourse> studentCourses = new ArrayList<StudentCourse>();

然后你可以在@Service类上看到这样的方法:

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

这也将填充StudentCourse表。

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

如果这不起作用,您可以尝试更改映射。对于n-ary关系或连接具有额外列的表,我总是在@ManytoOne类中定义@Embeddable关系,并且在表示连接表的实体中,我将getter定义为@Transient以允许访问嵌入式复合Id内的映射对象。

您可以看到示例here以及关于此方法的博文here