6
I’m implementing a simple GenericDAO
as below, but I feel like I’m doing the wrong thing, it works but I feel like there’s something wrong anyway, could help me?
I created an interface like this:
public interface GenericDAO<T, ID> {
public List<T> listaTodos(Class<T> clazz);
public List<T> listaComLimite(Class<T> clazz, Integer limite);
public T porId(Class<T> clazz, ID id);
public void adiciona(T t);
public T grava(T t);
public void remove(Class<T> clazz, ID id);
}
Then I created another interface (more specific), that is with what is not generic, but as we can see this extends
of GenericDAO
previous:
public interface TestDAO extends GenericDAO<Test, Long> {
public Test buscaPorNome(String nome);
}
Below I’m testing how would use:
public void getTest(){
Test teste = testDAO.porId(Test.class, id);
}
It sounds silly, but when I extended the GenericDAO
I’ve passed the target class:
extends GenericDAO<Test, Long>
The question is, because when I’m gonna use it I need to go through it again, like this?
Test teste = testDAO.porId(Test.class, id);
It doesn’t seem wrong?
Digging through the comments on Soen I found the library Typetools capable of solving the types "raw".
– Anthony Accioly
Hello Anthony, Your answer was perfect, I did the test by the constructor and really got less repetitive work. Thank you so much!!! I hope one day to get good in JAVA and OO to help tb... Abs. Marcelo
– Marcelo Ribeiro
Hi Marcelo. I’m glad to have helped.I’m sure you will still contribute to the community. If the answer solved your problem don’t forget to accept it.
– Anthony Accioly