Olá estou com o seguinte problema:
No banco de dados com duas tabelas onde tenho uma entidade em ambas as tabelas.
Um é de Turma e outra de Aluno
entre as duas tenho o id_turma.
Quando eu importo as informações do banco de dados está gerando uma linha no ArrayList nova para cada registro sendo que gostaria de importar apenas uma vez cada turma e os alunos para cada turma correspondente.
Vou colocar aqui o meu DAO para saber como está e as Classes de Turma e Alunos:
public List<Group> listStudentGroupsByIdAsc() {
ArrayList<Group> groups = new ArrayList<Group>();
ArrayList<Student> students = new ArrayList<Student>();
Connection conn = null;
Statement select = null;
ResultSet rs = null;
try {
conn = toConnect();
select = conn.createStatement();
rs = select.executeQuery(
"SELECT student.id_student, student.name_student, student.surname_student, student.id_group, group.name_group, group.code_group FROM school_management.student, school_management.group WHERE student.id_group = group.id_group Order By id_student ASC");
while (rs.next()) {
Long idGroup = rs.getLong("id_group");
String nameGroup = rs.getString("name_group");
String codeGroup = rs.getString("code_group");
Group group = new Group(idGroup, nameGroup, codeGroup);
idGroup = rs.getLong("id_group");
nameGroup = rs.getString("name_group");
codeGroup = rs.getString("code_group");
Long idStudent = rs.getLong("id_student");
String nameStudent = rs.getString("name_student");
String surnameStudent = rs.getString("surname_student");
group = new Group(idGroup, nameGroup, codeGroup);
Student student = new Student(idStudent, nameStudent, surnameStudent, group);
students.add(student);
group.addStudent(student);
groups.add(group);
}
} catch (
Exception e) {
e.printStackTrace();
} finally {
try {
if (rs != null)
rs.close();
if (select != null)
select.close();
if (conn != null)
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return groups;
}
Classe Aluno
package model.student;
import execptions.GroupStudentException;
import execptions.StudentGroupException;
import execptions.StudentIdException;
import execptions.StudentNameException;
import execptions.StudentSurnameException;
import model.group.Group;
public class Student {
private Long id;
private String name;
private String surname;
private Group group;
public Student() {
}
public Student(String name, String surname, Group group) throws StudentNameException, StudentSurnameException,
GroupStudentException, StudentIdException, StudentGroupException {
setName(name);
setSurname(surname);
}
public Student(Long id, String name, String surname, Group group) throws StudentNameException,
StudentSurnameException, GroupStudentException, StudentIdException, StudentGroupException {
setId(id);
setName(name);
setSurname(surname);
}
public Long getId() {
return id;
}
public void setId(Long id) throws StudentIdException {
if (id <= 0) {
throw new StudentIdException("O registro deve ser maior que zero");
}
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) throws StudentNameException {
if (name.isEmpty()) {
throw new StudentNameException("Nome do aluno não deve estar em branco");
}
this.name = name;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) throws StudentSurnameException {
if (surname.isEmpty()) {
throw new StudentSurnameException("Sobrenome do aluno não deve estar em branco");
}
this.surname = surname;
}
public Group getGroup() {
return group;
}
public void setGroup(Group group) {
this.group = group;
}
}
Classe Turma
package model.group;
import java.util.ArrayList;
import java.util.List;
import execptions.GroupCodeException;
import execptions.GroupNameException;
import model.student.Student;
public class Group {
private Long id;
private String name;
private String code;
private List<Student> students = new ArrayList<Student>();
public Group() {
}
public Group(String name, String code) throws GroupNameException, GroupCodeException {
setName(name);
setCode(code);
}
public Group(Long id, String name, String code) throws GroupNameException, GroupCodeException {
setId(id);
setName(name);
setCode(code);
}
/*
* public Group(Long id, String name, String code, ArrayList<Student> students)
* throws GroupNameException, GroupCodeException { setId(id); setName(name);
* setCode(code); }
*/
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) throws GroupNameException {
if (name.isEmpty()) {
throw new GroupNameException("Nome da turma não deve estar em branco");
}
this.name = name;
}
public String getCode() {
return code;
}
public void setCode(String code) throws GroupCodeException {
if (code.isEmpty()) {
throw new GroupCodeException("Código da turma não deve estar em branco");
}
this.code = code;
}
public List<Student> getStudents() {
return students;
}
/*
* public List<Group> getGroup() { return getGroup(); }
*/
public void addStudent(Student student) {
this.students.add(student);
}
public void removeStudent(Student student) {
this.students.remove(student);
}
}
Em meu banco tenho 5 registros, sendo um em uma turma e outros 4 em outra turma, deveria ter apenas dois Registros de ArrayList mas há 5 e ainda os Alunos não estão guardando de qual turma eles pertencem.