I am dealing with a legacy code that is 'not changeable' (no way to move to criteria api) and I have a little trouble with proper parameters binding. The query looks like this (MS SQL
):
SELECT BRAND AS b FROM CAR WHERE (NAME LIKE :phrase OR MODEL LIKE :phrase ) AND AGE NOT IN(1997, 1998) AND (:mileage IS NULL OR MILEAGE LIKE :mileage) ORDER BY BRAND
(...)
query.setParameter("phrase", "%" + phrase + "%");
query.setParameter("mileage", mileage);
phrase
is actually required, but because the mileage
parameter is optional, it's done in wierd way presented above.
The problem is that with both phrase
and mileage
provided, It keeps giving me following error : java.sql.SQLException: ResultSet may only be accessed in a forward direction.
. It works wihtout mileage
parameter provided. Why I am getting this error ?
EDITED:
- Running this query on db gives me no results.
- I am using
query.setResultTransformer(Transformers.aliasToBean(type))
as well as settingfirst
andmax
result on mySQLQuery query
object. - An error is when calling
query.list()
(used intellijevaluate expression
)
shouldn't query.list()
return an empty result ?
Probable answer (in that case):
It looks like setting FirstResult
on the query cause that problem because - by mistake - it's a negative number.