adam bien's blog

Selecting All JPA Entities As Criteria Query 📎

You could define a NamedQuery "SELECT c FROM ConfigurationEntry c" or use a type safe criteria variant in JPA 2+:


    @PersistenceContext
    EntityManager em;


 public List<ConfigurationEntry> allEntries() {
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<ConfigurationEntry> cq = cb.createQuery(ConfigurationEntry.class);
        Root<ConfigurationEntry> rootEntry = cq.from(ConfigurationEntry.class);
        CriteriaQuery<ConfigurationEntry> all = cq.select(rootEntry);
        TypedQuery<ConfigurationEntry> allQuery = em.createQuery(all);
        return allQuery.getResultList();
    }

This post is bit selfish: now I have a cheat sheet to lookup the first three lines. I just cannot remember these.

See you at Java EE Workshops at MUC Airport!

The snippet was stolen from: https://github.com/AdamBien/e2ftp