I have a class of Form
and Student
. A student may have 0 or many forms. I set the relation between them as OneToMany
. Now, I need to fetch the forms that are owned by a specific student. I have two questions at that point. How should I fetch those forms in the URL? Let’s say I want to fetch the forms of the student with id=7.
Option 1:
api/v1/forms?studentId=7
Option 2:
api/v1/students/7/forms
Which one of them is most used or more reasonable?
The second problem is that. Normally, when I want to add a new method to the JpaRepo
. I just need to add the method that is served by Jpa such that findAll
, findByStudentId
. Since I have a list of forms in the Student entity/model. How can I fetch all the forms? Which named method should I use? To sum up, I need to know whether I should write the method in FormDataAccess
or StudentDataAccess
. And what should be the method name?
Student
@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; }
Form Model
@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; }
Advertisement
Answer
The solution is really simple.
I just need to write a query in my FormDataAccess
.
@Query(" from Form where student.id = :studentId") List<Form> findFormByStudentId(int studentId);
now I can retrieve the forms using this path
api/v1/forms/student/7
I still wonder how to retrieve the forms from the Student side, not Form side such that
api/v1/student/7/forms