Bring all table records with jpa and eclipselink

0

Well, I have a question here at JPA. Today I have a method to fetch the UFs by ID, as in the example below:

public Uf consulta(Integer id) {
    EntityManager em = getEM();
    Uf uf = new Uf();
    try {
        em.getTransaction().begin();
        uf = em.find(Uf.class, id);
        em.getTransaction().commit();
    } catch (Exception e) {
        em.getTransaction().rollback();
    } finally {
        em.close(); //fecha o EntityManager        
    }
    return uf;
} 

Is there a way to search all bank records without having to go through them one by one? My idea is to present all the records in a table, but I do not know if I need to consult one by one with find inside a for and add in Array for example, or if there is any way in jpa that brings everything to me without needing of a for. Any hint will be helpful.

    
asked by anonymous 15.07.2017 / 02:34

1 answer

1

With your em , do:

Query query = em.createQuery("select u from Uf u");
List<Uf> ufs = query.getResultList();
    
16.07.2017 / 17:36