Using Java generics for JPA findAll() query with WHERE clause

前端 未结 4 1272
庸人自扰
庸人自扰 2020-12-14 02:50

So, After a 10+ year break I\'m coming back to Java and trying out stuff with JPA and Java generics. I\'ve created a generics based findAll(other) JPA query tha

4条回答
  •  死守一世寂寞
    2020-12-14 03:29

    Hat tip to Adam Bien if you don't want to use createQuery with a String and want type safety:

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

    http://www.adam-bien.com/roller/abien/entry/selecting_all_jpa_entities_as

提交回复
热议问题