Hibernate - How to search all rows of a table with Hibernate?

Asked

Viewed 1,245 times

1

I am not able to find all rows of a table with Hibernate. Does anyone know the name of the method that does this?

  • Hello Gabriel! Would you have some code to show us? Your doubt is unclear as there are different ways to do this: Criteria, JPQL or native query. If you are using a framework (like Spring), more options appear.

4 answers

2

You can do it:

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

You’d wear it 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 the Class that you pass as parameter.

The em here is the EntityManager. If you need something more complex to get an instance of EntityManager, will have to adapt this code.

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

Just be careful not to use this in a table with millions of records and consume all the memory building the list of results, remember that this will then turn into a SELECT without WHERE or paging. Something similar also happens if the searched entity has a lot of relationships that are marked with @ManyToOne, @OneToOne and/or fetchType = FetchType.EAGER which end up practically causing the entire database to be loaded into the memory.

2

Assuming an entity called Pessoa, which is correctly mapped into your system, and a variable session in which you have already started a Hibernate session, you want something like this to recover 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 documentation.

0

I recommend to clarify the SELECT instead of starting the query only with 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 making the consultation starting from the claúsula FROM, like this below:

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

The reason? If you decide to make a JOIN between two tables in this way, something very common in the old versions of Hibernate when the 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 you will have the Person and in the second you will have the Customer. This can happen when you use JOIN FETCH, so I don’t recommend omitting the SELECT.

Therefore, since then, I recommend making consultations explicit the SELECT.

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 passing parameters.

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’ve helped.

Browser other questions tagged

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