I have a requirement to fetch selected rows from Oracle database based on ids
supplied as an array, something like the SELECT ... FROM table_name WHERE id IN()
query.
In my attempts to do so, I'm trying to use the org.hibernate.setParameterList(String name, Object[] values)
method in my DAO as follows.
@Service
@Transactional(readOnly = true, propagation=Propagation.REQUIRES_NEW)
public final class ProductImageDAO implements ProductImageService {
@SuppressWarnings("unchecked")
public List<Object[]> getFileName(String[] list) {
return sessionFactory
.getCurrentSession()
.createQuery("SELECT prodImageId, prodImage FROM ProductImage WHERE prodImageId=:list")
.setParameterList("list", list).list();
}
}
The parameter of type String[]
in the given method is supplied from the respective Spring controller class.
It causes the following exception to be thrown.
org.hibernate.hql.ast.QuerySyntaxException: unexpected token: , near line 1, column 78 [select prodImageId, prodImage from model.ProductImage where prodImageId=:id0_, :id1_, :id2_, :id3_, :id4_, :id5_]
What is the way to retrieve the selected rows based on list of ids
using Hibernate?
The problem that most people make here is that they still want to use,
query.setParameter("accountIds", accountFilter);
instead of
query.setParameterList("accountIds", accountFilter);
use setParameterList()
when dealing with lists
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