When I do GET request to display the Student data they are retrieved like this:
The data gets overwritten only the last retrieved subject and professor is displayed on all instances.
Here is the method that the GET request is calling:
public Student getStudentById(int id) throws Exception {
Connection connection = null;
Student student = new Student();
Subject subject=new Subject();
student.setSubjectList(new ArrayList<>());
subject.setProfessorList(new ArrayList<>());
Map<String,Subject> subjectMap = 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.first_name,p.last_name,p.title "
" 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"
"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");
if(!subjectMap.containsKey(subjectID)) {
subject.setId(rs.getInt("id"));
subject.setName(rs.getString("name"));
subject.setProfessorList(new ArrayList<>());
subjectMap.put(subjectID, subject);
}
else{
subject = subjectMap.get(subjectID);
}
Professor professor = new Professor();
professor.setFirst_name(rs.getString("first_name"));
professor.setLast_name(rs.getString("last_name"));
professor.setTitle(rs.getString("title"));
subject.getProfessorList().add(professor);
}
} catch (Exception e) {
System.out.println(e "Retrieve not successful");
}
student.getSubjectList().addAll(subjectMap.values());
return student;
}
Student Class:
package com.common.db.domain;
import com.google.gson.annotations.SerializedName;
import java.util.List;
public class Student {
@SerializedName("id")
private String id;
@SerializedName("username")
private String username;
@SerializedName("password")
private String password;
@SerializedName("fullname")
private String fullName;
@SerializedName("email")
private String email;
@SerializedName("subjects")
private List<Subject> subjectList;
public Student() {
}
public Student(String id, String username, String password, String fullName, String email) {
super();
this.id = id;
this.username = username;
this.password = password;
this.fullName = fullName;
this.email = email;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public List<Subject> getSubjectList() {
return subjectList;
}
public void setSubjectList(List<Subject> subjectList) {
this.subjectList = subjectList;
}
}
Subject Class:
package com.common.db.domain;
import com.google.gson.annotations.SerializedName;
import java.util.List;
public class Subject {
@SerializedName("id")
private int id;
@SerializedName("name")
private String name;
@SerializedName("Professor")
private List<Professor> professorList;
public Subject() {
this.id = id;
this.name=name;
}
public void setId(int id)
{
this.id=id;
}
public int getId()
{
return id;
}
public void setName(String name)
{
this.name=name;
}
public String getName()
{
return name;
}
public List<Professor> getProfessorList() {
return professorList;
}
public void setProfessorList(List<Professor> professorList) {
this.professorList = professorList;
}
}
Professor Class:
package com.common.db.domain;
import com.google.gson.annotations.SerializedName;
public class Professor {
@SerializedName("id")
private int professor_id;
@SerializedName("first name")
private String first_name;
@SerializedName("last name")
private String last_name;
@SerializedName("username")
private String username;
@SerializedName("password")
private String password;
@SerializedName("Title")
private String title;
public int getProfessor_id() {
return professor_id;
}
public void setProfessor_id(int professor_id) {
this.professor_id = professor_id;
}
public String getFirst_name() {
return first_name;
}
public void setFirst_name(String first_name) {
this.first_name = first_name;
}
public String getLast_name() {
return last_name;
}
public void setLast_name(String last_name) {
this.last_name = last_name;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
ER DIAGRAM:
Is my Mapping done wrong for this situation of have I made another error in the SQL query
CodePudding user response:
I think that the problem may be that at this point:
if(!subjectMap.containsKey(subjectID)) {
subject.setId(rs.getInt("id"));
subject.setName(rs.getString("name"));
subject.setProfessorList(new ArrayList<>());
subjectMap.put(subjectID, subject);
you add new value to the map but at the same time modify previously added values (if I'm reading this correctly, each time with your setters you modify all the values in the map as they point to the same object "subject"). Try to initialize new Subject instance each time and maybe this will help
CodePudding user response:
You are usig the same Subject instance and you keep mutating it round and and rouns
When subjectMap does not contain the key, create new subject instance and proced
Probably more problems will come after this.