-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: Problems with session.iterate()!?
PostPosted: Mon Apr 26, 2004 11:09 am 
Pro
Pro

Joined: Wed Oct 08, 2003 10:31 am
Posts: 247
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?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 26, 2004 11:46 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Don't put the % into the query, append/prepend them to the string parameter you set.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 26, 2004 11:59 am 
Pro
Pro

Joined: Wed Oct 08, 2003 10:31 am
Posts: 247
michael wrote:
Don't put the % into the query, append/prepend them to the string parameter you set.


I've tried with the following (I think this is what you meen):

Code:
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();


rather than:

Code:
res_count = (Integer)session.iterate("select count(*) from pt.comseal.arsol.vo.Mp mp where mp.referencia like upper('%?%') and mp.estado = 'A'", ref_padrao, Hibernate.STRING).next();


But gives the same error.


Any ideas please?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 26, 2004 12:00 pm 
Pro
Pro

Joined: Wed Oct 08, 2003 10:31 am
Posts: 247
oops... forget the "package" (pt.comseal...).
It's irrelavant.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 26, 2004 12:03 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Drop the '


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 26, 2004 12:04 pm 
Pro
Pro

Joined: Wed Oct 08, 2003 10:31 am
Posts: 247
michael wrote:
Drop the '


That's it!
It now works.
Thanks very much Michael.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.