Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select all items from a list in hibernate

I have a list of product ids and I want to get all the products from my db with a hibernate query. How can I do this?

List<Integer> list = custumor.getCart();
Query query = query("select product from Product product where product.id =: list");

I know this isn't the best way to solve this, but I just want to know how I can test for all the values in a list.

like image 877
fibera Avatar asked Jun 15 '11 16:06

fibera


People also ask

How can I get all data from a table in Hibernate?

JPQL provides a simple and straightforward way to get all entities from a table. Our Hibernate session's createQuery() method receives a typed query string as the first argument and the entity's type as the second. We execute the query with a call to the getResultList() method which returns the results as a typed List.

Which of the following method is used to retrieve records from table in Hibernate?

Query Object Query objects use SQL or Hibernate Query Language (HQL) string to retrieve data from the database and create objects.

How can I get single record in Hibernate?

The Criteria. uniqueResult() method make it easier to query a single instance of a persistence object. When no persistence found this method will return a null value.


1 Answers

There are two things you'll need to do... The first is to change your HQL to the following (making use of IN), and the second is to bind your parameter list:

Query query = query("select product from Product product where product.id IN :list")
    .setParameterList("list", list);
like image 172
stevevls Avatar answered Sep 28 '22 03:09

stevevls