我有一个名为StudentLesson的表,带有一个主复合键StudentLessonPk,该表由Student_id和Lesson_id组成。我想通过id获得学生课程,但是我当然不能将对象传递给path变量。 我对JPA春季课程非常陌生,非常感谢您的帮助。我的代码供参考:
@Embeddable
public class StudentLessonPK implements Serializable{
private static final long serialVersionUID = 5611658036387185008L;
@Column(name = "Student_Id")
private String Student_Id;
@Column(name = "Lesson_Id")
private String Lesson_Id;
//GETTERS AND SETTERS
public String getStudent_Id() {
return Student_Id;
}
public void setStudent_Id(String student_Id) {
Student_Id = student_Id;
}
public String getLesson_Id() {
return Lesson_Id;
}
public void setLesson_Id(String lesson_Id) {
Lesson_Id = lesson_Id;
}
//CONSTRUCTOR
public StudentLessonPK(String student_Id, String lesson_Id) {
super();
Student_Id = student_Id;
Lesson_Id = lesson_Id;
}
public StudentLessonPK(){
}
}
我在控制器中的获取请求不正确:我想实现类似/ studentslesson / {studentid} / {lessonid}的功能
@RequestMapping(value="/studentslesson/{StudentLessonPK}", method = RequestMethod.GET)
public StudentLesson StudentLessonById(@PathVariable StudentLessonPK id) {
return StudentLessonService.getStudentLesson(id);
}
我的学生课程服务课程:
public StudentLesson getStudentLesson(StudentLessonPK id) {
Optional<StudentLesson> optionalStudentLesson = studentLessonRepository.findById(id);
if(optionalStudentLesson.isPresent()) {
return optionalStudentLesson.get();
}
return null;
}
我的存储库:
public interface StudentLessonRepository extends CrudRepository<StudentLesson, StudentLessonPK> {
}
答案 0 :(得分:0)
在您的控制器中,您可以将StudentId和lessonId定义为两个不同的路径参数,从这两个参数构造一个StudentLessonPK对象,然后将其传递给StudentLessonService。
@RequestMapping(value="/studentslesson/{studentId}/{lessonId}", method = RequestMethod.GET)
public StudentLesson StudentLessonById(@PathVariable String studentId, @PathVariable String lessonId) {
StudentLessonPK id = new StudentLessonPK(studentId, lessonId);
return StudentLessonService.getStudentLesson(id);
}