jpa - 联接表和 Spring 数据存储库

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

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

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

enter image description here

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

类(class).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;
    }

学生类(class).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;

       ...
}

学生类(class)PK.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 方法保存学生,并且这会插入/更新学生并插入/更新连接表。

由于 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 元关系或带有额外列的连接表,我总是定义 @ManytoOne @Embeddable 内部的关系类,在表示连接表的实体中,我将 getter 定义为 @Transient允许访问嵌入复合 ID 内的映射对象。

你可以看到一个例子 here ,以及有关此方法的博客文章 here .

关于jpa - 联接表和 Spring 数据存储库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33581429/

相关文章:

java - JPA 原生连接抓取

java - @DataJpaTest 无法排除响应式(Reactive) mongo 存储库

java - 与 Collection<Integer> 的一对多

php - 对学说的@OneToMany ArrayCollection 进行排序

java - Spring-Data-Jpa 中一对多映射的 JSON 结果错误

java - 错误: Data too long for column 'imagen' at row 1 with column 'MEDIUMBLOB' in MySQL

java - 如何在 Java 的 JPA 查询中使用 IN 子句?

java - Spring Boot REST - 如何实现需要 Java 对象的搜索?

java - 如何在eclipse中使用JPA工具为smallint类型列生成短变量?

python - 属性错误 : 'int' object has no attribute '_sa_instance_state'