GET REQUEST not returning student grade properly

Viewed 41

On all the subjects of the student the same grade is retrieved, the grade of the first subject is repeated in the other subject grades. I know the problem is in the hashmap that I have defined for subjects so what changes do I need to do there for this problem to be fixed.

Here is what it looks like:

enter image description here

Here is the method that the GET REQUEST calls:

  public Student getStudentById(int id) throws Exception {
        Connection connection = null;

        Student student = new Student();


        student.setSubjectList(new ArrayList<>());


        Map<String,Subject> subjectMap = new HashMap<>();
        Map<String,StudentMark> markMap = new HashMap<>();

        try {
            connection = new MysqlDbConnectionService().getConnection();


            String select = "SELECT s.user_id, s.username, s.password,  s.fullname,  s.email,subj.id, subj.name, f.fid, f.university_id," +
                    " f.fname,p.professor_id, p.first_name,p.last_name,p.title, g.grade_id, g.mark, g.description " +
                    " FROM student s " +
                    "         INNER JOIN student_faculty sf ON sf.student_id=s.user_id\n" +
                    "         INNER JOIN faculty f ON sf.faculty_id=f.fid\n" +
                    "         INNER JOIN faculty_subject fs ON f.fid = fs.faculty_id\n" +
                    "         INNER JOIN subject subj ON fs.subject_id = subj.id\n" +
                    "         INNER JOIN professor_subject ps ON ps.subject_id = subj.id\n" +
                    "         INNER JOIN professor p ON ps.prof_id = p.professor_id\n" +
                    "         INNER JOIN student_subject_marks sm ON sm.student_id = s.user_id\n" +
                    "         INNER JOIN student_subject_marks ds ON ds.subject_id = subj.id\n" +
                    "         INNER JOIN grade g ON sm.grade_id = g.grade_id\n" +
                    "WHERE user_id = ?";

            PreparedStatement ps = connection.prepareStatement(select);
            ps.setInt(1, id);
            ResultSet rs = ps.executeQuery();


            while (rs.next()) {

                if(student.getId()==null) {

                    student.setId(rs.getString("user_id"));
                    student.setUsername(rs.getString("username"));
                    student.setPassword(rs.getString("password"));
                    student.setFullName(rs.getString("fullname"));
                    student.setEmail(rs.getString("email"));
                }

                String subjectID=rs.getString("id");

                Subject subject =null;
                Professor professor=null;
                StudentMark mark = null;

                if(!subjectMap.containsKey(subjectID)) {
                   subject=new Subject();
                    subject.setId(rs.getInt("id"));
                    subject.setName(rs.getString("name"));
                    subject.setProfessorList(new ArrayList<>());
                    subject.setMarkList(new ArrayList<>());
                    subjectMap.put(subjectID, subject);

                    professor = new Professor();
                    professor.setProfessor_id(rs.getInt("professor_id"));
                    professor.setFirst_name(rs.getString("first_name"));
                    professor.setLast_name(rs.getString("last_name"));
                    professor.setTitle(rs.getString("title"));

                    mark = new StudentMark();
                    mark.setGrade_id(rs.getInt("grade_id"));
                    mark.setMark(rs.getInt("mark"));
                    mark.setDescription(rs.getString("description"));

                    subject.getProfessorList().add(professor);
                    subject.getMarkList().add(mark);

                }
                else{
                    subject = subjectMap.get(subjectID);
                }
//


            }
        } catch (Exception e) {
            System.out.println(e + "Retrieve not successful");

        }
        student.getSubjectList().addAll(subjectMap.values());


        return student;



    }

Here is the ER DIAGRAM:

enter image description here

0 Answers
Related