【问题标题】:Join Table and Spring Data Repository连接表和 Spring 数据存储库
【发布时间】:2016-02-08 11:08:16
【问题描述】:

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

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

学生.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;
    }

课程.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;

    ...
}

【问题讨论】:

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


    【解决方案1】:

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

    由于 Student 实体不是拥有方(它由 StudentCourse 中的“student”映射),保存 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 元关系或连接表,我总是在 @Embeddable 类中定义 @ManytoOne 关系,在表示连接表的实体中,我将 getter 定义为 @Transient 以允许访问映射对象位于嵌入的复合 ID 内。

    您可以查看示例 here,以及有关此方法的博客文章 here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-04
      • 1970-01-01
      • 1970-01-01
      • 2015-06-13
      • 1970-01-01
      相关资源
      最近更新 更多