2015-11-07 97 views
1

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

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

    ... 
} 

回答

0

如果我明白你想要做正确你的问题是要能够对学生在StudentRepository保存方法保存,并且这种插入/更新的学生,还插入/更新连接表。

由于学生实体不是自己的一方(它由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元关系或连接具有额外列的表,我总是在@Embeddable类中定义@ManytoOne关系,并且在表示连接表的实体中,我将getter定义为@Transient,以允许访问嵌入的组合标识内的映射对象。

您可以看到一个示例here,以及关于此方法的博客文章here