Skip to content

Instantly share code, notes, and snippets.

@madgrk
Created September 30, 2021 07:41
Show Gist options
  • Save madgrk/e47d3bacc87515689dbc3f6156636643 to your computer and use it in GitHub Desktop.
Save madgrk/e47d3bacc87515689dbc3f6156636643 to your computer and use it in GitHub Desktop.
Selecting all jpa entities as selection query
@PersistenceContext
EntityManager em;
protected Class<T> type;
public List<T> allEntries() {
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<T> cq = cb.createQuery(type);
Root<T> rootEntry = cq.from(type);
CriteriaQuery<T> all = cq.select(rootEntry);
TypedQuery<T> allQuery = em.createQuery(all);
return allQuery.getResultList();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment