Amigos,Não entendi muito bem o relacionamento de um método que to estudando da JAdressBook que baixei no jairelton.com… segue a baixo o código… quem puder me dar uma dica de como ele funciona e como se relaciona com os métodos que seguem ficarei grato…
Classe DAO
<blockquote>
protected Object fill(Class clazz, Map<String, String> mapping, ResultSet res) throws DAOException{
Object obj = null;
try{
obj = clazz.newInstance();
for(String field: mapping.keySet()){
Object value = res.getObject(field);
String attribute = mapping.get(field);
attribute = attribute.substring(0, 1).toUpperCase()+attribute.substring(1);
String setterName = "set"+attribute;
Method[] methods = clazz.getMethods();
Method setter = null;
for(Method m:methods){
if(setterName.equals(m.getName())){
setter = m;
break;
}
}
if(setter==null){
throw new DAOException("Can't find setter for attribute '"+attribute+"'");
}
setter.invoke(obj, new Object[]{value});
}
}catch(Exception e){
throw new DAOException(e.getMessage(), e);
}
return obj;
}</blockquote>
Ele se relaciona com os seguinte métodos da classe UserDAO
<blockquote>
public List<User> retrieveAll() throws DAOException{
List<User> all = new ArrayList<User>();
PreparedStatement stmt = null;
ResultSet res = null;
try{
stmt = DAO.getConnection().prepareStatement(“select * from users”);
res = stmt.executeQuery();
while(res.next()){ // AKÍ ESTÁ ELE, NÃO ENTENDI ISSO.
all.add(fill(res));
}
}catch(SQLException sqle){
throw new DAOException(sqle.getMessage(), sqle);
}finally{
try{
res.close();
stmt.close();
}catch(SQLException sqle){
throw new DAOException(sqle.getMessage(), sqle);
}
}
return all;
}</blockquote>
E tb se relaciona com esse outro fill tb da classe UserDAO
<blockquote>
private User fill(ResultSet res) throws DAOException{
Map<String, String> mapping = new HashMap<String, String>();
mapping.put(“id”, “id”);
mapping.put(“name”, “name”);
mapping.put(“login”, “login”);
mapping.put(“password”, “password”);
return (User)super.fill(User.class, mapping, res);
}</blockquote>
É que eu to estudando classes DAO e gostaria de entender melhor esse relacionamento… Sei que tem haver com mapeamento… e confesso ainda não ter entendido muito bem essa parte, quem sabe se alguem me explicar eu possa compreender melhor esses detalhes…
Abraço.