JPA – jpql query select optional general Dao selection
•
Java
I have retrieved the category object according to a valid JPA example:
return (ArrayList<Category>) getEntityManager().createQuery("from Category").getResultList();
The query is very short - I can't find any optional and unavailable rules in the guide Is this simplicity acceptable?
Secondly, I want to implement it in a general Dao, for example:
public interface DAO<E,K> { List<E> getAll(); }
How do I rewrite the first query to apply to all types because I can't hard code "from category"?
Solution
>Yes, brevity is acceptable Although I prefer complete syntax because it is more "attractive" to those who have more SQL experience
public List<E> getAll(Class<E> entityClass) { Query query = enittyManager.createQuery("from " + entityClass.getName()); query.getResultList(); }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码