0
I have the following class/method
public class DAO<T>{
EntityManager entityManager = (...)
public List<T> listAll(String jpql){
return entityManager.createQuery(jpql).getResultList();
}
}
works, but he’s not making use of the Generics, the string passed as parameter works as a gambiarra.
How do I get the whole Class past us Generics make a select with the JPQL?
Something like this...
public class DAO<T>{
EntityManager entityManager = (...)
public List<T> listAll(String jpql){
return (List T)entityManager.createQuery("select t from T t).getResultList();
}
}
Error is thrown saying that T
is not mapped, really has no way to know what is the T, but how would then this query using the Generics? Thank you!
You don’t necessarily need to pass JPQL to the method, since you already know which one
T
is being used throughgetClass().getEnclosingClass().getSimpleName()
. You can then create the query within thelistAll
without passing any parameter.– nullptr