-->
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: Problem with hibernate pagination in 3.0
PostPosted: Wed Aug 24, 2005 8:44 am 
Newbie

Joined: Thu Aug 04, 2005 8:20 am
Posts: 6
I tried to use the hibernate pagination from Gavin's weblog using the setFirstResults and setMaxResults API of Query. It works fine with Hibernate 2.1 but with Hibernate 3.0, it gives an exception:
ERROR [main ] 2005-08-24 18:06:07,794 : JDBCExceptionReporter.logExceptions : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?, ?' at line 1

Looks like it puts ? for limit for first result and max result, but does not replace the ? with the actual values in 3.0

Please help.

Hibernate version:
3.0

Mapping documents:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class name="Test" table="test">
<id name="id"
type="int"
unsaved-value="0">
<generator class="increment"/>
</id>
<property name="name"/>
<property name="age"/>
<property name="company"/>
</class>

<query name="sample.test.age.equal">
from Test t
where t.age = ?
</query>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():

try{
Session session=HibernateSession.currentSession();

Query q=session.createQuery("from Test");
q.setFirstResult(1);
q.setMaxResults(10);
List result=q.list();
Iterator i=result.iterator();
while(i.hasNext())
{
Test t=(Test)i.next();
System.out.println(t.getName());
}
HibernateSession.closeSession();
}catch(HibernateException he)
{
System.out.println("Exception ---- "+he.getMessage());
}


Full stack trace of any exception that occurs:

INFO [main ] 2005-08-24 18:06:07,075 : SessionFactoryImpl.<init> : building session factory
WARN [main ] 2005-08-24 18:06:07,090 : Configurator.configure : No configuration found. Configuring ehcache from ehcache-failsafe.xml found in the classpath: jar:file:/C:/amruta/Jar%20files/lib/ehcache.jar!/ehcache-failsafe.xml
INFO [main ] 2005-08-24 18:06:07,481 : SessionFactoryObjectFactory.addInstance : Not binding factory to JNDI, no JNDI name configured
INFO [main ] 2005-08-24 18:06:07,481 : SessionFactoryImpl.checkNamedQueries : Checking 1 named queries
INFO [main ] 2005-08-24 18:06:07,684 : HibernateSession.<clinit> : session factory build
INFO [main ] 2005-08-24 18:06:07,715 : HibernateSession.currentSession : session opened------ in currentSession()
WARN [main ] 2005-08-24 18:06:07,778 : JDBCExceptionReporter.logExceptions : SQL Error: 1064, SQLState: 42000
ERROR [main ] 2005-08-24 18:06:07,778 : JDBCExceptionReporter.logExceptions : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?, ?' at line 1
WARN [main ] 2005-08-24 18:06:07,794 : JDBCExceptionReporter.logExceptions : SQL Error: 1064, SQLState: 42000
ERROR [main ] 2005-08-24 18:06:07,794 : JDBCExceptionReporter.logExceptions : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?, ?' at line 1


Name and version of the database you are using:

Database :- SQLyog v4.01

The generated SQL (show_sql=true):
Hibernate: select test0_.id as id, test0_.name as name0_, test0_.age as age0_, test0_.company as company0_ from test test0_ limit ?, ?


Top
 Profile  
 
 Post subject: values
PostPosted: Thu Aug 25, 2005 12:01 pm 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
H replaces ? by values OK, it does not show it in the log.

Try sending SQL like this to DB manually and see what will happen
select test0_.id as id, test0_.name as name0_, test0_.age as age0_, test0_.company as company0_ from test test0_ limit 1, 10

Maybe your RDBMS does not support such SQL.

_________________
--------------
Konstantin

SourceLabs - dependable OpenSource systems


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 26, 2005 2:29 am 
Newbie

Joined: Thu Aug 04, 2005 8:20 am
Posts: 6
Thanks kgignatyev for your reply.

But the code works fine with Hibernate 2.0 if I do not specify the dialect property.
The query works fine while on the SQL console on MySQL 4.0.1.

I have to specify the dialect property when using hibernate 3.0 and after that the error that we get however with hibernate 3.0 is:

Hibernate: select test0_.id as id, test0_.name as name0_, test0_.age as age0_, test0_.company as company0_ from test test0_ limit ?, ?

Exception ---- could not execute query
org.hibernate.exception.SQLGrammarException: could not execute query
at org.hibernate.exception.ErrorCodeConverter.convert(ErrorCodeConverter.java:70)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.loader.Loader.doList(Loader.java:1502)
at org.hibernate.loader.Loader.list(Loader.java:1482)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:365)
at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:268)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:782)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:74)
at PageTest.testPage(PageTest.java:29)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:478)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:344)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Caused by: java.sql.SQLException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?, ?' at line 1
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2851)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1534)
at com.mysql.jdbc.ServerPreparedStatement.serverPrepare(ServerPreparedStatement.java:1485)
at com.mysql.jdbc.ServerPreparedStatement.<init>(ServerPreparedStatement.java:151)
at com.mysql.jdbc.Connection.prepareStatement(Connection.java:1309)
at com.mysql.jdbc.Connection.prepareStatement(Connection.java:1281)
at org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:351)
at org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:302)
at org.hibernate.jdbc.AbstractBatcher.prepareQueryStatement(AbstractBatcher.java:85)
at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1090)
at org.hibernate.loader.Loader.doQuery(Loader.java:362)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:203)
at org.hibernate.loader.Loader.doList(Loader.java:1499)
... 21 more


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 26, 2005 2:44 am 
Regular
Regular

Joined: Sun May 08, 2005 2:48 am
Posts: 118
Location: United Kingdom
yash wrote:
Hibernate: select test0_.id as id, test0_.name as name0_, test0_.age as age0_, test0_.company as company0_ from test test0_ limit ?, ?


I don't believe MySQL supports using "?" parameter substituion in the LIMIT clause, my JDBC code has a special case to always provided an unquoted integer at this point.

Maybe the Hibernate Dialect code used to do the right thing? Maybe 4.1 or 5.0 support the question mark but my 4.0 does not when used from the C or Perl APIs.

Can you run Hibernate 2 and confirm the sql output shows a question mark or a number there ?

Maybe you can write a raw JDBC test case to confirm ?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 26, 2005 3:20 am 
Newbie

Joined: Thu Aug 04, 2005 8:20 am
Posts: 6
Hi dlmiles and kgignatyev for your inputs

I tried using the GenericDialect and it worked with 3.0. i.e. the hibernate.cfg.xml appears like this now:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/sample</property>
<property name="hibernate.dialect">org.hibernate.dialect.GenericDialect</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.log">org.hibernate.sql</property>
<mapping resource="test.hbm.xml"/>
</session-factory>
</hibernate-configuration>

I do not know though whether I might have any further problems as I am using a generic dialect rather than the specific to MySQL one.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 26, 2005 3:44 am 
Regular
Regular

Joined: Sun May 08, 2005 2:48 am
Posts: 118
Location: United Kingdom
I've just stuck a Query.setMaxResults() in and my raw SQL says "limit ?" and I've no error, the SQL was processed fine.

JDK5
Hibernate 3.0.5
MySQL Server 4.0.13
mysql-connector-java-3.1.10
hibernate.properties: hibernate.dialect=org.hibernate.dialect.MySQLMyISAMDialect

This is while my Perl code using the C API fails for prepared statements with "LIMIT ?". I guess they must handle substitutions differently.

Certainly LIMIT '1' (with escaping apostrophe) is not valid from mysql shell client.

I used to use mysql-connector-java-3.1.7 and hit problems with that a few weeks ago, cant remember might be worth checking versions on everything balance with mine.


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.