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.
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.
Query Object Query objects use SQL or Hibernate Query Language (HQL) string to retrieve data from the database and create objects.
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With