-->
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.  [ 2 posts ] 
Author Message
 Post subject: Suggested fix for HQL - MySQL setMaxResults issue
PostPosted: Thu Nov 02, 2006 7:05 am 
Newbie

Joined: Thu Oct 12, 2006 9:25 am
Posts: 6
Hi,

After some experimentation, we'd like to suggest an easy fix to a problem we encountered.

When using HQL with the Hibernate 3 entity manager to limit the number of records returned ( at the database level, via generated SQL, not in memory) we were getting an exception due to the query builder trying to attach a parameter to the prepared statement that was out of bounds :
Code:
by: java.sql.SQLException: Parameter index out of range (4 > number of parameters, which is 3).
   at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:910)
   at com.mysql.jdbc.PreparedStatement.setInternal(PreparedStatement.java:2556)
   at com.mysql.jdbc.PreparedStatement.setInternal(PreparedStatement.java:2584)
   at com.mysql.jdbc.PreparedStatement.setInt(PreparedStatement.java:2540)
   at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.setInt(NewProxyPreparedStatement.java:677)
   at org.hibernate.loader.Loader.bindLimitParameters(Loader.java:1616)
   at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1560)
   at org.hibernate.loader.Loader.doQuery(Loader.java:661)
   at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
   at org.hibernate.loader.Loader.doList(Loader.java:2156)
   ... 10 more


On looking at the code in the method bindParameterValues in org.hibernate.loader.hql.QueryLoader ( line 491 ) the method returns the next parameter position that needs to be set, not the amount of parameters it has added, which is expected in Loader.java on the line
Code:
         col += bindParameterValues( st, queryParameters, col, session );


This causes the parameter count to get out of sync and thus throw the expcetion saying that it can't add a parameter out of range.

A simple fix for this is to change the return line in QueryLoader to be
Code:
return position - startPosition;

which will return the amount of parameters this method added.

We've tested this on our mysql implementation and it works, and run the unit tests to check that no extra errors are generated. If this seems like a good fix then i'll submit it as a patch via the JIRA, but would like to hear any thoughts first?

Kind regards,

TobyM - MindCandyDesign London


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 02, 2006 1:40 pm 
Newbie

Joined: Thu Oct 12, 2006 9:25 am
Posts: 6
I'm bumping this back up as I feel it needs to be noticed!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.