in my project I have this POJO class:
CourseBean.java:
@Expose
private Integer id;
@Expose
private String title = "";
@Expose
private String description = "";
@Expose
ArrayList<SubjectBean> alSubjects = new ArrayList();
It contains the columns of the Course table and an arraylist that would have anothers pojos representing the subjects of this course.
To fill this pojo the procedure that Im using is the following:
Is possible to optimize that procedure? maybe with a single SQL sentence?.
----------------------------------EDIT------------------------------------
I will use the statement that @Patrick Piatkowski sugested
SELECT * FROM Course
JOIN Subject
ON Course.id = Subject.course_id
WHERE /* Your filters here */
But now this leads me to another question. When I iterate the resulset, the columns of the course are the same in every row.
this.setId(oResultSetCourse.getInt("id")); this.setTitle(oResultSetCourse.getString("title")); this.setDescription(oResultSetCourse.getString("description"));
Those should be filled only once meanwhile the pojo of the subjects should be filled every iteration
What is the best method to handle this?
You could use a join:
SELECT * FROM Course
JOIN Subject
ON Course.id = Subject.course_id
WHERE /* Your filters here */
Or alternatively
SELECT * FROM Subject
WHERE course_id IN
(SELECT course_id FROM Course
WHERE /* Your filters here */)
The advantage of the second approach is that you only get columns from Subject while you also get the columns from Course by using the first statement.