I'm using Hibernate 2.1.3.
I trying to use (as in the documentation):
Code:
((Integer) session.iterate("select count(*) from ....").next()).intValue();
to count the number of query results without actually returning them.
I want to have a method that returns the number of query results or a paginated result, based on a flag parameter.
Here's my code:
Code:
( ... )
if(Character.toUpperCase(tarefa) == 'C') {
res_count = (Integer)session.iterate("select count(*) from vo.Mp mp where mp.referencia like upper('%?%') and mp.estado = 'A'", ref_padrao, Hibernate.STRING).next(); //-- (A)
}
else{
Query query = session.createQuery("from vo.Mp mp where mp.referencia like upper(:ref_padrao) and mp.estado = 'A' order by mp.referencia asc");
query.setParameter("ref_padrao", "%" + ref_padrao + "%");
query.setFirstResult(inicio);
query.setMaxResults(registos);
res_list = query.list();
}
( ... )
The above code gives me the following error:
Code:
WARN : SQL Error: 0, SQLState: 22023
ERROR: Parameter index out of range.
WARN : SQL Error: 0, SQLState: 22023
ERROR: Parameter index out of range.
ERROR: Could not execute query
org.postgresql.util.PSQLException: Parameter index out of range.
at org.postgresql.jdbc1.AbstractJdbc1Statement.bind(AbstractJdbc1Statement.java:2059)
at org.postgresql.jdbc1.AbstractJdbc1Statement.setString(AbstractJdbc1Statement.java:1128)
at org.postgresql.jdbc1.AbstractJdbc1Statement.setString(AbstractJdbc1Statement.java:1109)
at net.sf.hibernate.type.StringType.set(StringType.java:26)
at net.sf.hibernate.type.NullableType.nullSafeSet(NullableType.java:48)
( ... )
I think it's a sintaxe problem because if I use at
(A):
Code:
res_count = (Integer)session.iterate("select count(*) from vo.Mp mp where mp.referencia like upper('%" + ref_padrao + "%') and mp.estado = 'A'").next();
It works great. But I don't like the second situation because of the security hole (possible sql injection).
Any help please?