Bring all table records with jpa and eclipselink

Asked

Viewed 360 times

0

Guys, I have a question here at JPA. Today I have a method to search 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;
} 

Do you have any way to pull all the bank records without having to go through one by one ? My idea is to present all the records in a table, but I don’t know if I need to consult one by one with find inside a for and add in the Array for example, or if there’s any way in jpa that brings everything to me without needing a go. Any hint will be useful.

1 answer

1


With his em, do:

Query query = em.createQuery("select u from Uf u");
List<Uf> ufs = query.getResultList();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.