获取属于特定学生的表格列表
Fetch list of forms that are belong to a specific student
我有 Form
和 Student
的 class。一个学生可能有 0 个或多个表格。我将它们之间的关系设置为OneToMany
。现在,我需要获取特定学生拥有的表格。那时我有两个问题。我应该如何获取 URL 中的那些表格?假设我想获取 id=7 的学生的表格。
选项 1:
api/v1/forms?studentId=7
选项 2:
api/v1/students/7/forms
其中哪一个用得最多或者比较合理?
第二个问题是。通常,当我想向 JpaRepo
添加一个新方法时。我只需要添加 Jpa 提供的方法,使得 findAll
、findByStudentId
。因为我在 Student entity/model 中有一个表格列表。我怎样才能获取所有表格?我应该使用哪个命名方法?综上所述,我需要知道我应该把方法写在FormDataAccess
还是StudentDataAccess
。方法名称应该是什么?
学生
@Data
@Entity
@Table(name = "student")
public class Student {
@Id
@Column(name = "Student_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column(name = "Student_name")
private String name;
@Column(name = "Course_id")
private int courseID;
@Column(name="Oasis_id")
private Long oasisID;
@OneToMany(mappedBy = "student",fetch = FetchType.LAZY)
@JsonIgnore
private List<Form> forms;
}
表单模型
@Data
@Entity
@Table(name="form")
public class Form {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@ManyToOne
@JoinColumn(name = "Student_id", nullable = false)
private Student student;
@ManyToOne
@JoinColumn(name = "Attending_id", nullable = false)
private AttendingPhysician attending;
@Column(name = "Kayit_no", nullable = false)
private String kayitNo;
@Column(name = "Staj_turu", nullable = false)
private String stajTuru;
@Column(name = "Yas", nullable = false)
private int yas;
@Column(name = "Cinsiyet", nullable = false)
private String cinsiyet;
@Column(name = "Sikayet", nullable = false)
private String sikayet;
@Column(name = "Ayirici_tani", nullable = false)
private String ayiriciTani;
@Column(name = "Kesin_tani", nullable = false)
private String kesinTani;
@Column(name = "Tedavi_yontemi", nullable = false)
private String tedaviYontemi;
@Column(name = "Etkilesim_turu", nullable = false)
private String etkilesimTuru;
@Column(name = "Kapsam", nullable = false)
private String kapsam;
@Column(name = "Gerceklestigi_ortam", nullable = false)
private String gerceklestigiOrtam;
@Column(name = "Status", nullable = false)
private String status;
@Column(name="Date_sent", nullable = false) // Convert to timestamp using java Date @CreationTimestamp, @UpdateTimestamp
private String dateSent;
}
解决方法很简单。
我只需要在 FormDataAccess
中写一个查询。
@Query(" from Form where student.id = :studentId")
List<Form> findFormByStudentId(int studentId);
现在我可以使用此路径检索表单
api/v1/forms/student/7
我仍然想知道如何从学生端而不是表单端检索表格,这样
api/v1/student/7/forms
我有 Form
和 Student
的 class。一个学生可能有 0 个或多个表格。我将它们之间的关系设置为OneToMany
。现在,我需要获取特定学生拥有的表格。那时我有两个问题。我应该如何获取 URL 中的那些表格?假设我想获取 id=7 的学生的表格。
选项 1:
api/v1/forms?studentId=7
选项 2:
api/v1/students/7/forms
其中哪一个用得最多或者比较合理?
第二个问题是。通常,当我想向 JpaRepo
添加一个新方法时。我只需要添加 Jpa 提供的方法,使得 findAll
、findByStudentId
。因为我在 Student entity/model 中有一个表格列表。我怎样才能获取所有表格?我应该使用哪个命名方法?综上所述,我需要知道我应该把方法写在FormDataAccess
还是StudentDataAccess
。方法名称应该是什么?
学生
@Data
@Entity
@Table(name = "student")
public class Student {
@Id
@Column(name = "Student_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column(name = "Student_name")
private String name;
@Column(name = "Course_id")
private int courseID;
@Column(name="Oasis_id")
private Long oasisID;
@OneToMany(mappedBy = "student",fetch = FetchType.LAZY)
@JsonIgnore
private List<Form> forms;
}
表单模型
@Data
@Entity
@Table(name="form")
public class Form {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@ManyToOne
@JoinColumn(name = "Student_id", nullable = false)
private Student student;
@ManyToOne
@JoinColumn(name = "Attending_id", nullable = false)
private AttendingPhysician attending;
@Column(name = "Kayit_no", nullable = false)
private String kayitNo;
@Column(name = "Staj_turu", nullable = false)
private String stajTuru;
@Column(name = "Yas", nullable = false)
private int yas;
@Column(name = "Cinsiyet", nullable = false)
private String cinsiyet;
@Column(name = "Sikayet", nullable = false)
private String sikayet;
@Column(name = "Ayirici_tani", nullable = false)
private String ayiriciTani;
@Column(name = "Kesin_tani", nullable = false)
private String kesinTani;
@Column(name = "Tedavi_yontemi", nullable = false)
private String tedaviYontemi;
@Column(name = "Etkilesim_turu", nullable = false)
private String etkilesimTuru;
@Column(name = "Kapsam", nullable = false)
private String kapsam;
@Column(name = "Gerceklestigi_ortam", nullable = false)
private String gerceklestigiOrtam;
@Column(name = "Status", nullable = false)
private String status;
@Column(name="Date_sent", nullable = false) // Convert to timestamp using java Date @CreationTimestamp, @UpdateTimestamp
private String dateSent;
}
解决方法很简单。
我只需要在 FormDataAccess
中写一个查询。
@Query(" from Form where student.id = :studentId")
List<Form> findFormByStudentId(int studentId);
现在我可以使用此路径检索表单
api/v1/forms/student/7
我仍然想知道如何从学生端而不是表单端检索表格,这样
api/v1/student/7/forms