I have followed a working JPA example to retrieve Category objects as such:
return (ArrayList<Category>) getEntityManager().createQuery("from Category").getResultList();
The query is very shorthand – and I can’t find the rules for what is optional and what isn’t in any of the guides. Is this brevity acceptable?
Secondly, I want to now implement this in a generic DAO, something such as:
public interface DAO<E, K>
{
List<E> getAll();
}
How can I rewrite the first query to work for all types as I can’t hardcode the “from Category”..?
Yes, the brevity is acceptable. Although I prefer the full syntax because it is more “appealing” to others who have more SQL experience.
You have to add a
Class<E>parameter to your DAO: