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+:

    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 =;
        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:


This is really too verbose

Posted by Solerman Kaplon on August 08, 2013 at 07:44 PM CEST #

Perfect! Now we just have to get back to be paid in lines of code… :)

Posted by Oliver Gierke on August 09, 2013 at 03:28 PM CEST #

This is why CriteriaQuery need some simplify...

Posted by airborn on August 11, 2013 at 02:43 AM CEST #

Exactly what I needed.
Thanks for sharing.

put this method in an abstract Facade of your Entities and let your EntityFacade inherit.

Type once and never again.
I don't even have to remember this post, Adam ;-)

Posted by superwese on November 09, 2013 at 04:12 AM CET #

Is there anything wrong with:

CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Party> cq = cb.createQuery(Party.class);
TypedQuery<Party> allQuery = em.createQuery(cq);
List<Party> parties = allQuery.getResultList();

Posted by Karl on December 02, 2013 at 05:04 AM CET #

Post a Comment:
  • HTML Syntax: NOT allowed
...the last 150 posts
...the last 10 comments