Hibernate - How to fetch all rows from a table with Hibernate?

1

I'm not able to fetch all rows from a table with Hibernate. Does anyone know the name of the method that does this?

    
asked by anonymous 05.07.2018 / 18:53

4 answers

2
Assuming an entity named Pessoa , which is correctly mapped on your system, and a session variable that you have already started a Hibernate session, you want something like this to retrieve all the records:

Query query = session.createQuery("from Pessoa");
List<Pessoa> list = query.list();

Hibernate makes use of the HQL language for queries (it's one of the options, there are others). Learn more about it by taking a look at the documentation . p>     

05.07.2018 / 23:35
2

You can do this:

public <T> List<T> listarTodos(Class<T> tipo) {
    return em.createQuery("FROM " + tipo.getSimpleName(), tipo).getResultList();
}

You would use this like this:

List<Pessoa> todasAsPessoas = listarTodos(Pessoa.class);
List<Empresa> todasAsEmpresas = listarTodos(Empresa.class);

Note that the method is generic. The list it returns has the same type as Class you pass as a parameter.

The em here is EntityManager . If you need something more complex to get an instance of EntityManager , you'll have to adapt that code.

Note that method createQuery(String, Class<?>) " which returns a TypedQuery<T> and has two parameters. This is preferable than using similar method createQuery(String) that returns a Query without type and only has one parameter, because in that case getResultList() would supply a List gross without the generic type.

Just be careful not to use this in a table with millions of records and consume the entire memory building the list of results, remember that this will become a SELECT without WHERE and no pagination. Something similar also happens if the entity you are looking for has a bunch of relationships that are marked with @ManyToOne , @OneToOne and / or fetchType = FetchType.EAGER that almost end up loading the entire database into memory.     

22.07.2018 / 23:35
0

The code that does this is called hql, summarizing would be a Hibernate sql, its syntax would be this:

 Query q = em.createQuery("FROM Item_Pedido As a WHERE a.id_pedido = :para1");

In the case of a list for a dataTable you would do so, already with parameter passing.

public List<Item_Pedido> listaItemPorPedido(Long id) {
      String aux = String.valueOf(id);
    Query q = em.createQuery("FROM Item_Pedido As a WHERE a.id_pedido = :para1");
    q.setParameter("para1", aux);
    return q.getResultList();
}

I hope I have helped.

    
06.07.2018 / 18:01
0

I recommend explicitly specifying SELECT instead of starting the query with only FROM :

String jpql = "SELECT pessoa FROM Pessoa pessoa";
Query query = em.createQuery(jpql);
List<Pessoa> pessoas = em.getResultList();

You can also do this using native query:

String jpql = "SELECT pessoa.id, pessoa.nome FROM Pessoa pessoa";
Query query = em.createNativeQuery(jpql);
List<Object[]> pessoas = em.getResultList();

Why not just use FROM, without SELECT?

I do not recommend doing the query starting with the FROM clause, like this below:

String jpql = "FROM Pessoa";
Query query = em.createQuery(jpql);
List<Pessoa> pessoas = em.getResultList();

The reason? If you resolve to JOIN between two tables in this way, something very common in older versions of Hibernate when entities were not related:

String jpql = "FROM Pessoa pessoa, FROM Cliente cliente WHERE pessoa.cpf = cliente.cpf";

Hibernate will not return a Person, but an array of objects: Object[] . In the first position will have the Person and the second will have the Client. This can happen when you use JOIN FETCH , so I do not recommend skipping SELECT .

Therefore, since then, I recommend making queries by specifying SELECT .

    
15.08.2018 / 19:45