1
I have a java object that looks like this:
public class Objeto1 {
private int id;
private int nome;
private ArrayList<Objeto2> lista;
//construtor e Getters e Setters
}
How do I get this list from the database? Can be done in the same query or need to use a query for the main object and another to fill the list?
Here we use normal JDBC, we do not use any JPA library
To get the Object from the bank, we use something like this:
public Objeto1 selecionar(int identificador){
Objeto1 obj1 = new Objeto1();
try{
if(conexao.isClosed()) abreConexao();
PreparedStatement ps = conexao.prepareStatement("SP_BUSCA_OBJETO_1_IDTFD ?");
ps.setInt(1, identificador);
ResultSet rs = ps.executeQuery();
while(rs.next()){
obj1.setId = (rs.getInt("id")) ;
obj1.setNome = (rs.getString("nome"));
}
} catch (Exception e){
obj1 = null;
} finally {
fechaConexao();
}
return obj1;
}
How would a cleaner way to get the "list" attribute from the database?
You want to know how to get the list or refactor this code by getting the list ?
– Ricardo Lucas
I wanted to know the best way to get the list. If you can do it in a single query, or if I would have to do it later (A query to popular Object1, another query to popular the list within Object1)
– hjunior96